Python: How to Setup a Django Application on Apache 2.0 with mod_python on Ubuntu Server 7.04 Fiesty

For web developers who don’t have much system administration experience, deploying applications to production is the last hurdle that just doesn’t happen smoothly either because of a lack of experience or because of a lack of planning beforehand. The following is a how to guide based on my first experience of deploying a Django application to an environment running the following:

  • Ubuntu Server 7.04 Fiesty
  • Apache 2
  • mod_python
  • Django Framework
  • The Python database adapter or API that your application uses. In my case, it was ‘psycopg’.
  • The Database your application uses. I’m using PostgreSQL 8.2 for this deployment

Other things you will need in order to a install Django application include:

  • Subversion 1.2+
  • FTP Client
  • Administrative access (‘sudo’).
  • An application using Django Framework
  • Access to the virtual host files in the /etc/apache2/sites-available’
  • A website setup using Apache 2.0

I assume the user already has Ubuntu, Apache 2, and a database installed on the target host. If you haven’t already installed mod_python for Apache 2 and Subversion, you can do so by SSH’ing or logging into your server and type the following:

sudo apt-get install libapache2-mod-python subversion

Next we need to setup the directories that will be holding our Django project files and the Django framework. I usually place these files in a subdirectory in the user’s home directory. For my last deployment, I made a root directory called DJango with two subdirectories: django_src (stores framework) and django_projects (stores your django applications). I did this by typing:

mkdir /home/alex/django
mkdir /home/alex/django/django_src
mkdir /home/alex/django/django_projects

We should now download the Django framework. We can get the latest copy using Subversion. I did this by typing:

sudo svn co http://code.djangoproject.com/svn/django/trunk /home/alex/django/django_src

Next, we need to add the Django framework to the Python interpreter. I did this by adding a symbolic link to Python’s site-packages directory. In this environment, I am using Python 2.5. Type:

sudo ln -s /home/alex/django/django_src/django /usr/lib/python2.5/site-packages/django

In the next following paragraphs, I’m going to go into detail on how I organized and setup my Django application files on the server. When developing a Django application, there are two separate types of files being deploy: static files that are not processed by the Apache mod_python module, such as CSS, Django templates, javascript, and images, and Python files that need to be processed by the Apache mod_python module (.py files).

I prefixed all references to static files with ‘/assets/’. This is going to be the root directory in our website that will hold all static files. On a side note, your source control directories should have separate directories for holding Django python files and static files used by your Django application.

In the target website folder on the host, I created a subdirectory called ‘/assets/’. I did this by typing:

sudo mkdir /home/alex/websites/www.alexkuo.info/assets

Copy your static files into the assets directory.

Django applications commonly use the admin tool. In my projects, I specify the path to the static admin tool files by adding a symbolic link called ‘/admin_media/’. You can do this by typing:

sudo ln -s /home/alex/django/django_src/django/contrib/admin/media /home/alex/websites/www.alexkuo.info/admin_media

Next we are going to copy your Django application files to the server. To do this, create a Django folder named after your application. It is important that you name the directory the same as your application name. As a result, the name of the directory will be case sensitive. To find out your application’s name, open the ‘settings.py’ file and look for the variable ‘ROOT_URLCONF’. The application name can be found by looking at its value. For example, ‘APPLICATION_NAME.urls’.

I created the folder by typing:

mkdir /home/alex/django/django_projects/your_django_project_application_name

Before copying your application files over, I recommend editing the settings.py so that it will work properly with your host environment. The variables that need to be set that are specific to deployments are :

  • Database variables
  • MEDIA_ROOT
  • MEDIA_URL
  • ADMIN_MEDIA_PREFIX
  • TEMPLATE_DIRS

 The following is a portion of my settings.py file:

....
....
....

DATABASE_ENGINE = 'postgresql'

DATABASE_NAME = 'alexkuoinfo'
DATABASE_USER = 'alex'
DATABASE_PASSWORD = '******'
DATABASE_HOST = 'db.alexkuo.info'

DATABASE_PORT = '5432'

MEDIA_ROOT = '/home/alex/websites/www.alexkuo.info/assets/'

MEDIA_URL = 'http://www.alexkuo.info/assets/'

ADMIN_MEDIA_PREFIX = '/admin_media/'

TEMPLATE_DIRS = (

"/home/alex/websites/www.alexkuo.info/assets",

)

.....
.....
.....

Copy your django application files to ‘/home/user/django/django_projects/your_django_application_name’.

Now we need to setup Apache to process requests using mod_python. This requires the user to edit the virtual host file for the site. On my server, I am using a unique virtual host file for each site. These files are located in the ‘/etc/apache2/sites-available/’ directory. If you would like more information on adding virtual hosts to your Apache installation, consult this tutorial.

Edit the your virtual hosts file for the site, to do this I typed:

cd /etc/apache2/sites-available
sudo vi www.alexkuo.info

After editing my virtual host file, it looked like this:

<VirtualHost *> 
ServerAdmin me@alexkuo.info
ServerName www.alexkuo.info

DocumentRoot /home/alex/websites/www.alexkuo.info/

<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE ALEXKUO.settings
PythonDebug On
PythonPath "['/home/webdeploy/django/django_projects'] + ['/home/webdeploy/django/django_projects/ALEXKUO'] + sys.path"
</Location>
<Location "/admin_media/">
SetHandler None
</Location>
<Location "/assets/">
SetHandler None
</Location>
ErrorLog /var/log/apache2/error.www.alexkuo.info.log
</VirtualHost>

After saving your changes to the virtual host file, you should restart Apache. Type:

sudo a2ensite www.alexkuo.info
sudo /etc/init.d/apache2 restart

The settings in the virtual host file unique to a Django application are the Location elements. The element ‘<Location “/”>….</Location> ‘ tells Apache that all requests received from the root directory and subdirectories should use mod_python to interpret the request. The environment variable, ‘DJANGO_SETTINGS_MODULE’ specifies the settings module used for Django’s PythonHandler. The PythonPath references your Django application’s directory. You have to specify the root and subdirectory paths for your Django application or else import commands will fail when your application is compiled and run.

The next two Location elements specify that files in “/admin_media/” and “/assets/” directories should not be handled by mod_python.

If you’re working with a development machine and would like to see changes in your application files immediately, it is convenient to force Apache2 to reload your Django application per request. To do this, add the following in your ‘/etc/apache2/httpd.conf’ file and restart the Apache webserver.

MaxRequestsPerChild 1

Another alternative to reloading your Django application is to change the ‘settings.py’ file for your application. This is useful if you have other websites or applications running on your server. I usually just change the DEBUG variable from False to True.

That’s it. Since this is my first deployment, I’m open to any comments, suggestions, questions, or critiques.

For more information about deploying Django applications, please consult the following links:

Leave a Reply