= 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. [[BR]] 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: {{{ #!python 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.*)$', 'django.views.static.serve', {'document_root': '../../svn_views/yui/build','show_indexes': True}), (r'^site_media/(?P.*)$', '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 "/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 {{{/apache}}}. In this case {{{}}} is equal to {{{c:\\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/ "/media/" /media"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing Alias /yui/ "/build/" /build"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing Alias /media/ "/trunk/django/contrib/admin/media/" /trunk/django/contrib/admin/media"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing WSGIScriptAlias / "/apache/dj_survey.wsgi" /apache"> Allow from all }}} Now here it is the core of the wsgi application. The path there should use "\\"as separator. === dj_survey.wsgi === {{{ #!python 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 {{{\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: {{{ #!python 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. {{{ #!python ROOT_URLCONF = 'dj_project.apache.urls_production' }}} === urls_production.py === {{{ #!python 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 == * http://code.google.com/p/modwsgi/ * http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango