« DjangoCon 2008 | DjangoCon September 6-7, at Google! »
I was just convinced to setup mod_wsgi on my server instead of mod_python, and I'm going to write up how I did it. All of the documentation I found on the internet was really hard to follow, so I'm going to distill it here the best that I can.
This is assuming Ubuntu 8.04 Server Edition.
Update: Take note, this is installing mod_wsgi 1.3. The latest version of the package is 2.3. If you want to get the latest version from apt, you should use the Debian 2.3 package
Step 1:
apt-get install libapache2-mod-wsgi
This should automatically install mod_wsgi into your apache instance and install it.
Step 2: Create an apache directory on your filesystem, presumably inside of your Django project. I keep my code in ~/Python/Project, so I did:
mkdir ~/Python/PROJECT/apache
vim ~/Python/PROJECT/apache/django.wsgi
Then in that file you need to copy this code:
import os, sys
sys.path.append('/home/eric/Python/PROJECT')
os.environ['DJANGO_SETTINGS_MODULE'] = 'PROJECT.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
This creates an interface between Django and WSGI, as far as I can tell. If you start getting errors about not seeing your project or modules, try adjusting and/or adding some things to your sys.path.
Step 3:
Inside your /etc/apache2/ directoy, you will find the directory sites-available/. This is where you are going to put your configuration for your server. Presumably it will have a file called default in it that you will edit. So:
In /etc/apache2/sites-available/default:
<VirtualHost *:80>
ServerAdmin eric@ericholscher.com
ServerName ericholscher.com
ServerAlias www.ericholscher.com
DocumentRoot /var/www/
LogLevel warn
WSGIDaemonProcess ericholscher processes=2 maximum-requests=500 threads=1
WSGIProcessGroup ericholscher
WSGIScriptAlias / /home/eric/Python/PROJECT/apache/django.wsgi
Alias /media /var/www/media/
</VirtualHost>
The last 3 lines of WSGI stuff if what you want to pay attention to. You are pointing WSGIScriptAlias to the file we created in Step 2. The other two WSGI prompts aren't necessary unless you are running multiple sites on your server. The Alias is so that the /media URLs on your site continue to work, it should point to where ever you have your media files stored.
Hopefully this will get you started along the way to setting up mod_wsgi on Apache with Django. If not, feel free to leave comments or email me
EDIT: Someone in the comments pointed out this website on the mod_wsgi wiki is also helpful: Integration with Django
There appears to be a page on the Django WIki as well if you need more pointers.
Comments have been close for this post.
Posted at 11:11 p.m. on July 8, 2008
Comments: 8
Tags: deployment , django , mod_wsgi , tutorial
The role of designers in the Django community
2 days, 13 hours Ago (Comments: 6)
Large Problems in Django, Mostly Solved: Documentation
3 days, 22 hours Ago (Comments: 4)
1 month Ago (Comments: 0)
Correct way to handle default model fields.
2 months, 2 weeks Ago (Comments: 8)
2 months, 3 weeks Ago (Comments: 2)
I may not have gone where I intended to go, but I think I have ended up where I intended to be.
- Douglas Adams


Comments
1 says...
Why were you convinced to use mod_wsgi instead of mod_python?
Posted at 2:41 p.m. on July 25, 2008
2 says...
I've been running apache + ubuntu server 8.04 + postgres + mod_wsgi for some time too, and really loving it.
Some thing I did additionally:
Make sure you don't install MOD PHP also, or it automatically installs the prefork version of apache and uninstalls the worker version. If you also have to run PHP on your server, use PHP under FastCGI instead, so the rest of Apache doesn't have to suffer from PHP's flaws (non thread safe) and can still run in worker mode.
Create aliases for the /media folder, /admin/media folder (depending how you have your server setup) and /favicon.ico
Something like:
Alias /media/ "/home/user/site/media/" <Directory "/home/user/site/media/"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory>
Alias /admin/media/ "/usr/lib/python2.5/site-packages/django/contrib/admin/media/" <Directory "/usr/lib/python2.5/site-packages/django/contrib/admin/media/"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory>
Alias /favicon.ico "/home/user/site/media/favicon.ico"
Posted at 2:42 p.m. on July 25, 2008
3 says...
@patrick: mod_python aparently isn't maintained anymore, and mod_wsgi is the wave of the future. Also, it is "faster" for what it's worth with all my trafiic :)
@robvdl: Great! It's working well here too. Thanks for the tips. Sorry about it messing up the formatting of the code snippets, I'll see if I can fix that in the styles.
Posted at 2:43 p.m. on July 25, 2008
4 says...
Haven't you read this excelent documentation?
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
Posted at 2:43 p.m. on July 25, 2008
5 says...
There's also a Django wiki-page: http://code.djangoproject.com/wiki/django_apache_and_mod_...
@Robvdl: If you are using Django you should be careful about running the worker MPM, because it is not considered fully thread safe either. See: http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango (as mentioned, just before the comments start) and http://code.djangoproject.com/wiki/DjangoSpecifications/C...
Posted at 2:44 p.m. on July 25, 2008
6 says...
Thanks for all the links to the documentation, I did indeed read them, and they were what helped me setup my box. I thought it could use a little distilling down just for people who want to get it done without too much effort. I went ahead and threw in the links you all posted above, thanks for pointing them out. They are indeed a great source of information!
Posted at 2:44 p.m. on July 25, 2008
7 says...
It isn't strictly true that mod_python isn't maintained anymore. Some changes are still being made and committed to repository, but is slow and not clear when next release may be.
Posted at 12:27 p.m. on August 6, 2008
8 Vernon says...
Christian Joergensen also wrote up pretty much the same post on Django and mod_wsgi, apparently after your post but I happened to see it earlier in the Django community blogroll:
http://www.technobabble.dk/2008/aug/25/django-mod-wsgi-pe...
He also gives a few reasons for using mod_wsgi over mod_python.
Posted at 1:41 p.m. on August 27, 2008