mod_wsgi and virtualenv for multiple django versions

I'm converting a mod_python environment to a mod_wsgi one. I was using the debian packages for both django-tagging and django itself. This has proved to be unacceptable both for upgrades and for keeping sites running. I don't want an apt-get dist-upgrade to break all my django sites (as it did yesterday).

One solution to this is to use virtualenv to keep separate python environments for each site, and then upgrade them individually as necessary. It wasn't clear how mod_python would work with virtualenv (I tried), so I moved to using mod_wsgi which is also reputed to be more efficient. To begin, install both virutalenv and mod_wsgi:

apt-get install python-virtualenv libapache2-mod-wsgi

Next, set up virtualenv. In the project directory (described previously), run:

virtualenv --no-site-packages --unzip-setuptools ENV

Next, grab django and whatever other packages you might want. I move these to a directory with the SVN revision after the package name.

cd ~/libs/ svn co http://code.djangoproject.com/svn/django/trunk/ django mv django django-10645 svn checkout http://django-tagging.googlecode.com/svn/trunk/ tagging mv tagging tagging-154

Symlink the packages in to the virtual environment.

cd ~/libs/django-10645 ln -s `pwd`/django /home/username/blog/ENV/lib/python2.5/site-packages/ cd ~/libs/tagging-154 ln -s `pwd`/tagging /home/username/blog/ENV/lib/python2.5/site-packages/

Create the wsgi config file. This goes in a separate directory from everything else so that the permissions that apache has to read and serve it don't affect anything else.

mkdir ~/blog/apache vi ~/blog/apache/blog.wsgi

The wsgi file should look like the following. Bolded are the parts which will be changed for each project.

ALLDIRS = ['/home/username/blog/ENV/lib/python2.5/site-packages/'] import os, sys, site prev_sys_path = list(sys.path) for directory in ALLDIRS: site.addsitedir(directory) new_sys_path = [] for item in list(sys.path): if item not in prev_sys_path: new_sys_path.append(item) sys.path.remove(item) sys.path[:0] = new_sys_path sys.path.append('/home/username/blog/projects/') os.environ['PYTHON_EGG_CACHE'] = '/home/username/.python-eggs' os.environ['DJANGO_SETTINGS_MODULE'] = 'blogsite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()

My version seems to differ slightly from other versions I've seen. In particular my "main site" app is in a subdirectory of projects, so I have appname.settings for the DJANGO_SETTINGS_MODULE. I'm not sure that my way has any advantage over any others, it's just the way it is.

Finally, set up the virtual host:

<VirtualHost XXX.XXX.XXX.XXX> ServerAdmin admin@example.com ServerName blog.example.com CustomLog /var/log/apache2/access.log combined Alias /adminmedia /home/username/blog/ENV/lib/python2.5/site-packages/django/contrib/admin/media Alias /media /home/username/blog/media <Directory /home/username/blog/apache/> Order deny,allow Allow from all </Directory> WSGIDaemonProcess blog.example.com user=www-data group=www-data threads=25 WSGIProcessGroup blog.example.com WSGIScriptAlias / /home/username/blog/apache/blog.wsgi </VirtualHost>

First, two aliases are set, one for the normal media (this is unchanged from a mod_python installation) and the other for the admin media. This should point to the virtualenv version of the admin media. Next, the Directory section allows apache access to the wsgi handler. And finally, wsgi is configured and pointed to the handler in the apache directory. Do a:

apache2ctl restart

And it's done!