Version 2 (modified by yml, 17 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 surported 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 binaries.

Apache and mod_wsgi application

In this section I will take you trough an example, the django application is called dj_survey.

The original urls.py is looking like this:

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 de 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 create 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>\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>/">
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
sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\workspace')
sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\workspace\\dj_project')
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()

In this file you are defining the settings in our it is called: dj_project.apache.settings_production There is nothing special there except that I am pointing to a special ulrs.py

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

Retstart apache and now you should be able enjoy your application serve by apache.

References

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