Ubuntu - Using mod_python to serve your application


If you have followed the previous article (Ubuntu - Mod_python installation), you should have Apache and mod_python ready to serve an app for you.

In this article we create a basic Django app and setup the virtual host that will allow Apache and mod_python to work their magic.

Create the Django Application

First, move to your home directory and go into your public_html/domain1.com directory (if you don't have one, create one and give it the name of your domain rather than domain1.com):

cd ~/public_html/domain1.com

Next, create a Django project which is done with the django-admin.py tool. In this case we will call it 'testproject':

django-admin.py startproject testproject

Create the Virtual Host

For Apache to be able to serve a Django application, it needs to know that it should hand off certain requests to mod_python. To accomplish this, we set up a virtual host that takes care of letting Apache know what to do in certain situations.

This example is basic but it will get you going:

sudo nano /etc/apache2/sites-available/domain1.com

Then type or copy the following virtual host definition:

        ServerName domain1.com
        ServerAlias www.domain1.com

        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE testproject.settings
        PythonPath "['/home/demo/public_html/domain1.com'] + sys.path"
        PythonDebug On

Once you've saved the virtual host, you need to enable it and reload Apache:

sudo a2ensite domain1.com
sudo /etc/init.d/apache2 reload

Provided everything went as expected, you should be able to visit your domain (or slice IP) in your browser and get your newly created Django application.

Note: If you get any port and NameVirtualHost errors upon reloading Apache, please ensure you read the Apache Virtual Hosts article.

Static Content

There is a caveat to the virtual host definition provided: it does not allow serving of static content. No document root was specified and there is nothing to indicate that static files are not to be handled by mod_python and Django.

The Django team recommends you use a secondary web server to serve static content. However, you can make a few tweaks in your testproject/settings.py file and use the following virtual host definition:

        ServerName domain1.com
        ServerAlias www.domain1.com

        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE testproject.settings
        PythonPath "['/home/demo/public_html/domain1.com'] + sys.path"
        PythonDebug On

        DocumentRoot /home/demo/public_html/domain1.com
        <Location "/static/">
            SetHandler none
            Options -Indexes
        

The Location block tells Apache to not let Django handle anything that is located under /static/ on your site. You can set this to be anything you like, but you will need to make the appropriate directory available under /home/demo/public_html/domain1.com. In this example the directory would be called "static".

Tweaks for the settings.py file involve setting the MEDIA_URL and MEDIA_ROOT settings appropriately:

nano /home/demo/public_html/domain1.com/testproject/settings.py

Find the following two settings and edit them like so:

MEDIA_ROOT = '/home/demo/public_html/domain1.com/static/'
MEDIA_URL = '/static/'

Reload Apache now to make the updates take effect:

sudo /etc/init.d/apache2 reload

Now any items placed in /home/demo/public_html/domain1.com/static can be accessed via http://domain1.com/static/path/to/file.

Changes to your Django Application

When you update your python code or templates in a Django application with mod_python, you'll usually need to give Apache a reload to see the changes. So it's good to get into the habit of reloading Apache after making any changes to your project.

At this point you should now be able to successfully build out a Django application and have mod_python and Apache serve it up for you.



Was this content helpful?




© 2014 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER