School is a twelve-year jail sentence where bad habits are the only curriculum truly learned.
John Taylor Gatto
New forum topics
Recent comments
- I haven't explored Puppet
4 weeks 14 hours ago - Wouldn't puppet be more
4 weeks 1 day ago - I agree with the previous
10 weeks 1 day ago - how did this go when you
11 weeks 2 days ago - First off let me say thank
13 weeks 22 hours ago - You are my hero. :) Worked
17 weeks 1 day ago - If you are use this in Django
19 weeks 18 hours ago - You might have gotten this
20 weeks 1 day ago - THANKSSSSSSSSSSSSSS
20 weeks 2 days ago - The valid html suggestion
20 weeks 3 days ago
One more post about my experience coding a Django application for Facebook. Let's discuss a little about correctly setting up Apache and mod_python.
Most of the steps are very well described on Django's documentation but you may need a few more details. Let's see.
The importance of PYTHONPATH
According to the official documentation this is what you need on your Apache's site configuration to setup Django:
<Location "/mysite/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonOption django.root /mysite
PythonPath "['/path/to/project'] + sys.path"
PythonDebug On
</Location>
Obviously PythonDebug On should not appear, or should be set to Off, on a production environment.
The lines above assume that your Django project is called mysite and the site is on http://example.com/mysite. And even if the documentation mentions how to handle PYTHONPATH via mod_python I didn't have a clear picture of the whole thing after running my first failed tests.
I decided to setup a site dedicated just to my Django project, you can use name based or IP based virtual hosts on Apache, and I want to access it from the root: http://example.com/. Also, I store all my Python related stuff in /home/alexis/python-work hence the project will be at /home/alexis/python-work/project and its only application below it: /home/alexis/python-work/project/app.
I know there are better ways to organize reusable applications on Django, as James Bennett suggests, but for my project a traditional project/app structure was enough. Anyway, the following ideas can be applied to any setup. Let's see all the lines I use on Apache to setup a Django site:
<VirtualHost 192.168.0.180>
ServerName example.com
ServerAdmin alexis@example.com
<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE project.settings
PythonDebug On
PythonPath "['/home/alexis/python-work', '/home/alexis/python-work/project'] + sys.path"
</Location>
</VirtualHost>
The IP on VirtualHost, 192.168.0.180, is the private one I'll be using for this site. I've also added 192.168.0.180 example.com in /etc/hosts to avoid changes on the DNS server, something you should do on production.
project.settings refers to the settings.py file inside the /home/alexis/python-work/project directory.
I've also removed PythonOption django.root /mysite because this site sits on root, Location "/", and there's no need to specify a subdirectory.
And the most important part:
PythonPath "['/home/alexis/python-work', '/home/alexis/python-work/project'] + sys.path"
This is how you add paths to PYTHONPATH via mod_python. Notice we have two paths, one for the parent directory and another for the project, both are needed to get all your imports right. Django's documentation highlights this:
Remember: the parent directories of anything you import directly must be on the Python path.
After adding these changes you should restart Apache, or possibly just reload, and be able to access your Django site at http://example.com/.
And everything else?
I haven't touched yet on how to serve static files on a production environment, something I'm doing with a combination of Lighty, mod_proxy and Apache, or the initial setup of a Linux server, Ubuntu in my case, with Apache and mod_python, so there's a lot more coming about Django applications deployment. Stay tuned.
Related content
Also visit
Other Resources
- Cloud based Task Management Software
- Top rated project scheduling software
- Daniel E Straus Aveta

Join the conversation
Hmm... I tried altering my
Hmm... I tried altering my configuration file like this and I can't get to work. I'm trying to set up a server on FreeBSD 6.3 with Apache2.2.11, mod_python 3.3. I had a set up but I couldn't get the admin application to read its CSS . The normal application works. So I tried your setup. Now the admin and the normal application don't read their CSS.
hey, i have installed the
hey,
i have installed the postgresql, python, django, mod_python on ubuntu.
but some unicode error occurs. and i can not run the app.
ImportError: cannot import name smart_unicode
and i tried docs.djangoproject.com/en/1.2/howto/deployment/modpython :
/etc/apache2/envvars
export LANG='en_US.utf8'
export LC_ALL='en_US.utf8'
but it still doesn't work ...
please help, what is the problem