Installing mod_python and Django on Apache

I've recently started learning Django for doing web development, so as part of that, I needed to also learn how to set up Django within mod_python so I could deploy my new Django applications on my server.

In this post, I'll be sharing the method I used to install mod_python and Django on my CentOS 5, Apache-based web server. So without any further ado, let's get started on the process!

Starting Off

I'll be running the latest SVN release of Django, which provides the latest features. Despite not officially being stable, I've found it to be fine, and doesn't restrict me to having to develop to the older spec of the 0.9x releases. That siad, you should regularly update your SVN copy to keep updated with the latest security and bug fixes.

Apparently:

"We improve Django almost every day and are pretty good about keeping the code stable. Thus, using the latest development code is a safe and easy way to get access to new features as they're added."

Before we get started downloading Django, however, we first need to grab mod_python (the plumbing between Apache and Django) for the installed Apache. The easiest way to do this is through yum:

# yum install mod_python

This should automatically add to your httpd.conf Modules, but if it doesn't, then you should manually do so with the line:

Module python_module modules/mod_python.so

Getting and Installing Django

Now let's go and get the latest version of Django. For the whole of this tutorial, I'll assume you are putting Django in /opt, as I am here. As root:

# mkdir /opt/django
# cd /opt/django
# svn co http://code.djangoproject.com/svn/django/trunk/

Once the SVN checkout completes, you have Django downloaded. Next, we need to get it installed into Python, by symlinking this new Django directory into Python's site-packages directory.

# ln -s /opt/django/trunk/django /usr/lib/python2.4/site-packages

Also, we should at this point add django-admin.py to your PATH, so you can use it to create new Django projects from anywhere on your system.

# ln -s /opt/django/trunk/django/bin/django-admin.py /usr/local/bin/django-admin.py

To test that the Django installation worked, run the following. If it works, Python shouldn't give you any error message and go silently back to its prompt. If you get ImportError: No module named django, you have a problem. Check that symlink to /opt/django/trunk/django is in Python's site-packages directory.

# python
>>> import django

Getting a Database Library

You need a Python module for the database you plan to use with Django installed, so that Django can talk to the database. Most people will be using MySQL, so that's what I'll cover installing here.

Right now, the version of connector module MySQL-python in the CentOS repositories is too old to use with the SVN release of Django. Instead, get the latest tar.gz release from here.

Once that's downloaded, do this in the directory where you downloaded it:

# tar xzvf MySQL-python-1.2.2.tar.gz
# cd MySQL-python-1.2.2
# python setup.py build
# python setup.py install

Making Application and Template Directories

Django applications aren't deployed like PHP, for example. Your application's source files and templates are kept outside of your web server's document root (much better for security), and you instead use Django's URL resolvers to build a logical URL structure. This also has the disadvantage of meaning you have to put your media files for a project somewhere separately, but I'll cover that later.

For now, we need to make both an application directory, where our application code will reside, and a template directory. In CentOS, the document root by default is /var/www/html, meaning we have the whole of /var/www/ to use for other web server stuff that needs to remain outside the web server's normal root. Perfect for this.

# mkdir /var/www/djangoapps
# chown apache /var/www/djangoapps
# mkdir /var/www/djangotemplates
# chown apache /var/www/djangotemplates

Make an Egg Cache

We're almost done in the preparation stage, but we also have to add a Python egg cache that the web server can write to. I found the easiest way to do this was to add a new directory in /var/cache for the purpose.

# mkdir -p /var/cache/www/pythoneggs
# chown apache /var/cache/www/pythoneggs

Serving up Media Files

Media files have to be done separately. If you're into Vhosts, you could set up a subdomain for serving media, but the simplest way is to just make a /media directory in /var/www/html and place your Django application's media in a subfolder of that (then updating settings.py for each app to reflect the location).

If you're planning on using the Django admin interface, I recommend symlinking the Django admin media directory in here too, like so:

# ln -s /opt/django/trunk/django/contrib/admin/media /var/www/html/media/admin

Now, set ADMIN_MEDIA to this path in the settings.py file for each app you deploy.

Deploying an Application

Finally, you need to actually deploy an application. Here, I'll assume you're working with an application named myapp. Initially, you should drop the application source code in /var/www/djangoapps/myapp, the templates in /var/www/djangotemplates/myapp and your media in the media location you set up earlier.

Make sure at this point you now go back to /var/www/djangoapps/myapp/settings.py and tweak the project's settings, including Debug mode, media URL, admin media URL and anything else relevant to your new deployment. Also, don't forget to check your database settings if you're working with a different database server or instance.

You also need to go into urls.py and update the URL patterns with a prefix that you want to use for your application (in this case, /myapp), or your links will be broken and URLs won't resolve once installed.

In httpd.conf, add a Location tag to specify your new application's root:

<Location /myapp>
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE myapp.settings
SetEnv PYTHON_EGG_CACHE "/var/cache/www/pythoneggs"
PythonDebug Off
PythonPath "['/var/www/djangoapps'] + sys.path"
</Location>

Save httpd.conf, and restart Apache:

# /etc/init.d/httpd restart

Also remember that any changes to source code will likely require a restart of Apache to read them in.

Avatar for peter Peter Upfold - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Installing mod_python and Django on Apache

  1. Mauricio Q (guest)

    # Posted on 26 May 2008 at 07:43 PM

    Great article, specially because the lack of good Django guides. However I found a couple of issues, first is under Ubuntu (8.04 my case) the user apache doesn't exists, is it ok? I give ownership to user "www-data" and works, again is this ok?, need to try without it too. The second thing is that when I browse http://localhost/djangoapps/ I can actually watch the files and can download the code.

    Thanks for any further guidance.



  2. Peter (guest)

    # Posted on 26 May 2008 at 08:19 PM

    First of all - yep, <strong>www-data</strong> is the user you need to substitute in for Ubuntu.

    Secondly, the source code is exposed because of directory structure differences between CentOS (the platform I wrote this tutorial for) and Ubuntu. If we review this line:

    <em>"In CentOS, the document root by default is /var/www/html, meaning we have the whole of /var/www/ to use for other web server stuff that needs to remain outside the web server’s normal root."</em>

    Ubuntu's web server root is /var/www, therefore djangoapps and djangotemplates become exposed to clients.

    Therefore, you should move /var/www/djangoapps and /var/www/djangotemplates somewhere different. Perhaps make /var/django/apps and /var/django/templates, move the files and update configuration files accordingly.

    Hope these tips help.



  3. # Posted on 20 November 2008 at 06:13 PM

    [...] http://fosswire.com/2008/05/19/installing-mod_python-and-django-on-apache/ [...]



  4. Michael Kumm (guest)

    # Posted on 16 January 2009 at 09:25 PM

    Really helpful - best guide I could find - thanks



  5. adam (guest)

    # Posted on 20 January 2009 at 12:38 AM

    THANKS! Although I had it kinda "working", I had to kludge my PythonPath in the apache conf to get similar behavior between the django-supplied dev web server and my apache server. Also, your explanation helped me correct my configuration of admin-media so my admin site no longer looks like crap :D

    Very helpful!



  6. Jonathan (guest)

    # Posted on 30 March 2009 at 08:46 AM

    This may be a stupid question, but can the location tag be added via a .htaccess file?



  7. # Posted on 01 April 2009 at 10:41 AM

    Jonathan said:

    This may be a stupid question, but can the location tag be added via a .htaccess file?

    In theory, yes, this should work, but I'm not entirely sure. I have a feeling it has to be in httpd.conf for it to actually pick up that it's a mod_python application.

    By all means try it and see.



  8. Arash Fazeli (guest)

    # Posted on 07 June 2009 at 09:48 AM

    tnx! you're a life saver.. best guide I've found!!



  9. Dave (guest)

    # Posted on 25 July 2011 at 07:30 PM

    Very grateful for this guide. I'm working on some older Centos boxes and was pulling my hair out, what with mod_python's age & MySQL-python version too old, etc.

    Thanks a million.



Home » Articles » Installing mod_python and Django on Apache

Sign In