RkBlog

Hardware, programming and astronomy tutorials and reviews.

mod_wsgi

mod_wsgi is a new and efficient way to host Python applications like Django under Apache web server

mod_wsgi is an Apache module that provides a WSGI compliant interface for hosting Python based web applications within Apache. The adapter is written completely in C code against the Apache C runtime and for hosting WSGI applications within Apache. Check out the documentation for more details.

Testing mod_wsgi

Create hello.py file with the code:
def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]
In apache configuration set:
LoadModule wsgi_module               modules/mod_wsgi.so
WSGIScriptAlias / /path/to/hello.py
Now when you start the server you should see on http://localhost/ the "Hello World!" message.

Example Django configuration

Here is a configuration for a django project:
Alias /site_media/ "/path/to/djangoproject/site_media/"
LoadModule wsgi_module               modules/mod_wsgi.so


<Directory /path/to/djangoproject/site_media>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / /path/to/djangoproject/mysite.wsgi

<Directory /path/to/djangoproject>
Order deny,allow
Allow from all
</Directory>
mysite.wsgi:
import os, sys
sys.path.append('/path/to/djangoproject/')
# may be required :)
#sys.path.append('/path/to/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi

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

Benchmarks (very old!)

I've made simple Siege tests for Apache 2.0.59, Python 2.4.4 and mod_python 3.3.1 under AMD64 Gentoo using my site (Django) as the test application. The charts show the results. mod_wsgi is bit faster than mod_python even now. Transaction rate for mod_python and mod_wsgi: 16,79 and 18,63 transactions per second (3 Siege tests, standard deviation: 1,89 and 0,4). Throughput 0,11 and 0,12 MB per second (standard deviation: 0,02 and 0) and average response time: 0,37 and 0,3 seconds (standard deviation: 0,07 and 0,03)
wsgi_en_1
wsgi_en_2
wsgi_en_3
RkBlog

14 July 2008;

Comment article