Version 7 (modified by anonymous, 16 years ago) ( diff )

--

How to use django with mod_wsgi

This is a very simple recipe to deploy a django application with mod_wsgi. This procedure has been tested on Windows but should work on any operating system supported by apache, mod_wsgi, and django.

Installation

mod_wsgi can be downloaded from there: http://code.google.com/p/modwsgi/. There is an excellent procedure for installing it on windows there: http://code.google.com/p/modwsgi/wiki/InstallationOnWindows.
On that page you will find a link to precompiled windows binaries.

Configuration of Apache and mod_wsgi

In this section I will take you trough an example, the django application is called dj_survey. This application is part of a project called "dj_project".

The urls.py I used to serve the application with the Django built-in development server:

from django.conf.urls.defaults import *
from django.contrib import databrowse
import registration 


urlpatterns = patterns('',
    # Example:
    # (r'^dj_project/', include('dj_project.foo.urls')),

    # Uncomment this for admin:
    (r'^admin/', include('django.contrib.admin.urls')),
    (r'^dj_survey/', include('dj_project.dj_survey.urls')),
    (r'^databrowse/(.*)', databrowse.site.root),
    (r'^accounts/', include('registration.urls')),
    (r'^yui/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '../../svn_views/yui/build','show_indexes': True}),
    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': './media','show_indexes': True}),
)

In httpd.conf you should load the mod_wsgi and include the file containing the configuration of your django application. The trap there is all the path should use "/" and not "\".

httpd.conf

#This should be included somewhere at the top of this file
LoadModule wsgi_module modules/mod_wsgi.so

#somewhere at the bottom
Include "<PATH TO YOUR DJANGO PROJECT>/apache/apache_django_wsgi.conf"

This suppose that you have created a folder named apache in your django project in this folder your should add the following files:

08/16/2007  04:12 PM             1,082 apache_django_wsgi.conf
08/16/2007  04:31 PM               557 dj_survey.wsgi
08/16/2007  04:31 PM             4,362 settings_production.py
08/16/2007  04:09 PM               712 urls_production.py
08/16/2007  04:33 PM                 0 __init__.py

Note: There is a file called __init__.py in <PATH TO YOUR DJANGO PROJECT>/apache. In this case <PATH TO YOUR DJANGO PROJECT> is equal to c:\<LONG PATH>\workspace\dj_project

Like in the httpd.conf you should pay attention to the separator. You should use "/" and not "\"

apache_django_wsgi.conf

Alias /site_media/ "<PATH TO YOUR DJANGO PROJECT>/media/"
<Directory "<PATH TO YOUR DJANGO PROJECT>/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

Alias /yui/ "<PATH TO YOUR YUI>/build/"
<Directory "<PATH TO YOUR YUI>/build">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

Alias /media/ "<PATH TO YOUR DJANGO SRC>/trunk/django/contrib/admin/media/"
<Directory "<PATH TO YOUR DJANGO SRC>/trunk/django/contrib/admin/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>


WSGIScriptAlias / "<PATH TO YOUR DJANGO PROJECT>/apache/dj_survey.wsgi"

<Directory "<PATH TO YOUR DJANGO PROJECT>/apache">
Allow from all
</Directory>

Now here it is the core of the wsgi application. The path there should use "
"as separator.

dj_survey.wsgi

import os, sys

#Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace) 

#Add the path to 3rd party django application and to django itself.
sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\svn_views\\django_src\\trunk')
sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\svn_views\\django-registration')

os.environ['DJANGO_SETTINGS_MODULE'] = 'dj_project.apache.settings_production'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Note: If you need to write something in error.log located in the following folder <Your Apache Installation>\Apache2.2\logs you can insert the line below in your WSGI file. In our example this file is called dj_survey.wsgi. This method is very convenient to get the PYTHONPATH correct:

print >> sys.stderr, sys.path 

In this file you are defining the settings that will be used by your Django application, in our example this file is called: dj_project.apache.settings_production (Note: ".py" is implicit and should not be added). There is nothing special in that file except that I am pointing to a special urls.py which reflect the configuration I am using in production.

ROOT_URLCONF = 'dj_project.apache.urls_production'

urls_production.py

from django.conf.urls.defaults import *
from django.contrib import databrowse
import registration 


urlpatterns = patterns('',
    # Example:
    # (r'^dj_project/', include('dj_project.foo.urls')),

    # Uncomment this for admin:
    (r'^admin/', include('django.contrib.admin.urls')),
    (r'^dj_survey/', include('dj_project.dj_survey.urls')),
    (r'^databrowse/(.*)', databrowse.site.root),
    (r'^accounts/', include('registration.urls')),
)

Test

Restart apache and now you should be able to enjoy your application served by Apache and mod_wsgi.

References

Note: See TracWiki for help on using the wiki.
Back to Top