Apache and mod wsgi

From Django In Production

Jump to: navigation, search

Serving Django with mod_wsgi is an increasingly popular approach. It provides some performance (speed and memory usage) benefits over mod_python (data?), as well as alleviates some concern over mod_python's apparent lack of development.

The following is a list of required software:

Create a WSGI Script

Django comes with the django.core.handlers.wsgi.WSGIHandler() callable which will create a WSGI application for a Django application.

You need to create a WSGI script file to bridge WSGI and Django. Create a new file, named wsgi_handler.py in your Django project, the same directory as settings.py.

       import sys
       import os
       sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
       os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
       
       import django.core.handlers.wsgi
       application = django.core.handlers.wsgi.WSGIHandler()

The above script adds your project to the python path and then creates the application by calling the WSGI handler that Django provides.

Configuring Apache

Once you have created the WSGI Script, you must configure your apache conf to point to the new file.

       WSGIScriptAlias / /path/to/mysite/wsgi_handler.py
           
       WSGIDaemonProcess mysite user=user group=user processes=5 threads=1
       WSGIProcessGroup mysite

This will enable the mod_wsgi daemon mode, running in a single threaded but multi-process configuration, as currently recommended for Django. See Prefork MPM or worker MPM for more information.

Personal tools