Friday 2 April 2010

Using mod_wsgi to support Django development via Apache

I'm building a Django shop which integrates PHP components in the form of a blog (Wordpress) and needs testing with SSL. As discussed in the last blog entry- that means I can't well test holistically using only the Django dev server. On production I am using mod_python at the moment, which is inappropriate for development as I would need to restart the server each time I make a change.

I'm looking to follow a recipe, using mod_wsgi for two purposes:
  • To have a dev server running via apache, to integrate PHP and SSL components, as well as having "hot code replacement" to support development.
  • To evaluate mod_wsgi on the basis of the claim: "for hosting WSGI applications in conjunction with Apache it has a lower memory overhead and performs better than mod_python".

Step 1 will be to deploying using mod_wsgi on my dev laptop per the mod_wsgi docs.
  • sudo apt-get install libapache2-mod-wsgi [ubuntu]
  • restart apache
Step 2 is a standard integration with Apache. Per http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
  • create appropriate .wsgi file
  • edit apache.conf with directive (see below)
Step 3 is getting it to then run in daemon mode. This is a little like what mod_jk or ruby passenger / mod_rails does. It brokers requests off to a separate sub process which forks and brokers off to individual Python Django scripts.

Step 4, finally per: http://blog.dscpl.com.au/2008/12/using-modwsgi-when-developing-django.html, is to add a monitor.py (the source of which is here) and reference it in the .wsgi file.

Here are example apache / httpd.conf:


And dev.wsgi (based of putting a monitor.py in the root of the django app):

No comments:

Post a Comment