Monday, June 15, 2009

Serving Django on Apache

As an update on the previous post, looking into current Django docs, the Apache production server method of using mod_python to serve Django has been superseded by mod_wsgi. I had partially implemented the former method, so now I'm bringing it up to the latest spec.

According to google.code on Python WSGI, Apache 1.3, 2.0 or 2.2 are compatible. Apache on a MediaTemple dv account shows

httpd -v

Server version: Apache/2.2.3
Server built: Nov 12 2008 10:41:27

Python needs to be 2.3 through 2.5 and the vers on (mt) shows

Python 2.4.3 (#1, May 24 2008, 13:47:28)


So far, so what. Now to install wsgi...

wget http://modwsgi.googlecode.com/files/mod_wsgi-2.5.tar.gz
tar xvfz mod_wsgi-2.5.tar.gz
configure
make
make install

finally edit

httpd.conf

located in

/etc/httpd/conf.

Adding the lines:

LoadModule wsgi_module modules/mod_wsgi.so

and


<virtualhost *>
ServerAdmin jasonthewolf@gmail.com
ServerName www.unplugtheinternet.com
DocumentRoot /var/www/vhosts/unplugtheinternet.com
WSGIScriptAlias / /django/test01/apache/django.wsgi
</virtualhost>

In the above, WSGIScriptAlias deserves some explanation:
Description : WSGI maps a URL to a filesystem location and designates the target as a WSGI script.
Syntax : WSGIScriptAlias URL-path file-path|directory-path
Context : server config, virtual host
Module : mod_wsgi.c

A request for http://www.example.com/wsgi-scripts/name in this case would cause the server to run the WSGI application defined in /web/wsgi-scripts/name. This configuration is essentially equivalent to:

and restart apache with

apachectl restart

All reports are good from Apache so far. Now that the installation of wsgi is complete, back to Django implementation.

I once again edited httpd.conf, this time adding
WSGIScriptAlias / /path/to/mysite/apache/django.wsgi

which for my site and requirements was effectively:

WSGIScriptAlias /django/test01 /var/www/vhosts/unplugtheinternet.com/apache/django.wsgi

This is the folder I have chosen for my python tests. If they work out, I'll move them to the root of some url or another. Following the convention established in the example, I created directory apache and the used vi to create the textfile django.wsgi as:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

No comments: