Changes between Initial Version and Version 1 of django_apache_and_mod_wsgi


Ignore:
Timestamp:
Aug 18, 2007, 4:38:26 PM (17 years ago)
Author:
yml
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • django_apache_and_mod_wsgi

    v1 v1  
     1= How to use django with mod_wsgi =
     2
     3This 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.
     4
     5== Installation ==
     6
     7mod_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
     8On that page you will find a link to precompiled binaries.
     9
     10Apache and mod_wsgi  application
     11
     12In this section I will take you trough an example, the django application is called dj_survey.
     13
     14The original urls.py is looking like this:
     15{{{
     16from django.conf.urls.defaults import *
     17from django.contrib import databrowse
     18import registration
     19
     20
     21urlpatterns = patterns('',
     22    # Example:
     23    # (r'^dj_project/', include('dj_project.foo.urls')),
     24
     25    # Uncomment this for admin:
     26    (r'^admin/', include('django.contrib.admin.urls')),
     27    (r'^dj_survey/', include('dj_project.dj_survey.urls')),
     28    (r'^databrowse/(.*)', databrowse.site.root),
     29    (r'^accounts/', include('registration.urls')),
     30    (r'^yui/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '../../svn_views/yui/build','show_indexes': True}),
     31    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': './media','show_indexes': True}),
     32)
     33}}}
     34
     35In 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 "\".
     36httpd.conf
     37{{{
     38#This should be included somewhere at the top of this file
     39LoadModule wsgi_module modules/mod_wsgi.so
     40
     41#somewhere at the bottom
     42Include "<PATH TO YOUR DJANGO PROJECT>/apache/apache_django_wsgi.conf"
     43}}}
     44
     45This suppose that you create a folder named apache in your django project in this folder your should add the following files:
     46
     4708/16/2007  04:12 PM             1,082 apache_django_wsgi.conf
     4808/16/2007  04:31 PM               557 dj_survey.wsgi
     4908/16/2007  04:31 PM             4,362 settings_production.py
     5008/16/2007  04:09 PM               712 urls_production.py
     5108/16/2007  04:33 PM                 0 __init__.py
     52
     53Like in the httpd.conf you should pay attention to the separator. You should use "/" and not "\"
     54apache_django_wsgi.conf
     55{{{
     56Alias /site_media/ "<PATH TO YOUR DJANGO PROJECT>/media/"
     57<Directory "<PATH TO YOUR DJANGO PROJECT>/">
     58Order allow,deny
     59Options Indexes
     60Allow from all
     61IndexOptions FancyIndexing
     62</Directory>
     63
     64Alias /yui/ "<PATH TO YOUR YUI>/build/"
     65<Directory "<PATH TO YOUR YUI>//build">
     66Order allow,deny
     67Options Indexes
     68Allow from all
     69IndexOptions FancyIndexing
     70</Directory>
     71
     72Alias /media/ "<PATH TO YOUR DJANGO SRC>/trunk/django/contrib/admin/media/"
     73<Directory "<PATH TO YOUR DJANGO SRC>/trunk/django/contrib/admin/media">
     74Order allow,deny
     75Options Indexes
     76Allow from all
     77IndexOptions FancyIndexing
     78</Directory>
     79
     80
     81WSGIScriptAlias / "<PATH TO YOUR DJANGO PROJECT>/apache/dj_survey.wsgi"
     82
     83<Directory "<PATH TO YOUR DJANGO PROJECT>/apache">
     84Allow from all
     85</Directory>
     86}}}
     87
     88Now here it is the core of the wsgi application. The path there should use "\\"as separator.
     89dj_survey.wsgi
     90{{{
     91import os, sys
     92sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\workspace')
     93sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\workspace\\dj_project')
     94sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\svn_views\\django_src\\trunk')
     95sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\svn_views\\django-registration')
     96os.environ['DJANGO_SETTINGS_MODULE'] = 'dj_project.apache.settings_production'
     97import django.core.handlers.wsgi
     98application = django.core.handlers.wsgi.WSGIHandler()
     99}}}
     100
     101In this file you are defining the settings in our it is called: dj_project.apache.settings_production
     102There is nothing special there except that  I am pointing to a special ulrs.py
     103
     104{{{
     105ROOT_URLCONF = 'dj_project.apache.urls_production'
     106}}}
     107
     108urls_production.py
     109{{{
     110from django.conf.urls.defaults import *
     111from django.contrib import databrowse
     112import registration
     113
     114
     115urlpatterns = patterns('',
     116    # Example:
     117    # (r'^dj_project/', include('dj_project.foo.urls')),
     118
     119    # Uncomment this for admin:
     120    (r'^admin/', include('django.contrib.admin.urls')),
     121    (r'^dj_survey/', include('dj_project.dj_survey.urls')),
     122    (r'^databrowse/(.*)', databrowse.site.root),
     123    (r'^accounts/', include('registration.urls')),
     124)
     125}}}
     126
     127== Test ==
     128Retstart apache and now you should be able enjoy your application serve by apache.
     129
     130== References ==
     131
     132 * http://code.google.com/p/modwsgi/
     133 * http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
     134
     135
Back to Top