| | 1 | = How to use django with mod_wsgi = |
| | 2 | |
| | 3 | 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. |
| | 4 | |
| | 5 | == Installation == |
| | 6 | |
| | 7 | 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 |
| | 8 | On that page you will find a link to precompiled binaries. |
| | 9 | |
| | 10 | Apache and mod_wsgi application |
| | 11 | |
| | 12 | In this section I will take you trough an example, the django application is called dj_survey. |
| | 13 | |
| | 14 | The original urls.py is looking like this: |
| | 15 | {{{ |
| | 16 | from django.conf.urls.defaults import * |
| | 17 | from django.contrib import databrowse |
| | 18 | import registration |
| | 19 | |
| | 20 | |
| | 21 | urlpatterns = 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 | |
| | 35 | 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 "\". |
| | 36 | httpd.conf |
| | 37 | {{{ |
| | 38 | #This should be included somewhere at the top of this file |
| | 39 | LoadModule wsgi_module modules/mod_wsgi.so |
| | 40 | |
| | 41 | #somewhere at the bottom |
| | 42 | Include "<PATH TO YOUR DJANGO PROJECT>/apache/apache_django_wsgi.conf" |
| | 43 | }}} |
| | 44 | |
| | 45 | This suppose that you create a folder named apache in your django project in this folder your should add the following files: |
| | 46 | |
| | 47 | 08/16/2007 04:12 PM 1,082 apache_django_wsgi.conf |
| | 48 | 08/16/2007 04:31 PM 557 dj_survey.wsgi |
| | 49 | 08/16/2007 04:31 PM 4,362 settings_production.py |
| | 50 | 08/16/2007 04:09 PM 712 urls_production.py |
| | 51 | 08/16/2007 04:33 PM 0 __init__.py |
| | 52 | |
| | 53 | Like in the httpd.conf you should pay attention to the separator. You should use "/" and not "\" |
| | 54 | apache_django_wsgi.conf |
| | 55 | {{{ |
| | 56 | Alias /site_media/ "<PATH TO YOUR DJANGO PROJECT>/media/" |
| | 57 | <Directory "<PATH TO YOUR DJANGO PROJECT>/"> |
| | 58 | Order allow,deny |
| | 59 | Options Indexes |
| | 60 | Allow from all |
| | 61 | IndexOptions FancyIndexing |
| | 62 | </Directory> |
| | 63 | |
| | 64 | Alias /yui/ "<PATH TO YOUR YUI>/build/" |
| | 65 | <Directory "<PATH TO YOUR YUI>//build"> |
| | 66 | Order allow,deny |
| | 67 | Options Indexes |
| | 68 | Allow from all |
| | 69 | IndexOptions FancyIndexing |
| | 70 | </Directory> |
| | 71 | |
| | 72 | Alias /media/ "<PATH TO YOUR DJANGO SRC>/trunk/django/contrib/admin/media/" |
| | 73 | <Directory "<PATH TO YOUR DJANGO SRC>/trunk/django/contrib/admin/media"> |
| | 74 | Order allow,deny |
| | 75 | Options Indexes |
| | 76 | Allow from all |
| | 77 | IndexOptions FancyIndexing |
| | 78 | </Directory> |
| | 79 | |
| | 80 | |
| | 81 | WSGIScriptAlias / "<PATH TO YOUR DJANGO PROJECT>/apache/dj_survey.wsgi" |
| | 82 | |
| | 83 | <Directory "<PATH TO YOUR DJANGO PROJECT>/apache"> |
| | 84 | Allow from all |
| | 85 | </Directory> |
| | 86 | }}} |
| | 87 | |
| | 88 | Now here it is the core of the wsgi application. The path there should use "\\"as separator. |
| | 89 | dj_survey.wsgi |
| | 90 | {{{ |
| | 91 | import os, sys |
| | 92 | sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\workspace') |
| | 93 | sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\workspace\\dj_project') |
| | 94 | sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\svn_views\\django_src\\trunk') |
| | 95 | sys.path.append('C:\\yml\\_myScript_\\dj_things\\web_development\\svn_views\\django-registration') |
| | 96 | os.environ['DJANGO_SETTINGS_MODULE'] = 'dj_project.apache.settings_production' |
| | 97 | import django.core.handlers.wsgi |
| | 98 | application = django.core.handlers.wsgi.WSGIHandler() |
| | 99 | }}} |
| | 100 | |
| | 101 | In this file you are defining the settings in our it is called: dj_project.apache.settings_production |
| | 102 | There is nothing special there except that I am pointing to a special ulrs.py |
| | 103 | |
| | 104 | {{{ |
| | 105 | ROOT_URLCONF = 'dj_project.apache.urls_production' |
| | 106 | }}} |
| | 107 | |
| | 108 | urls_production.py |
| | 109 | {{{ |
| | 110 | from django.conf.urls.defaults import * |
| | 111 | from django.contrib import databrowse |
| | 112 | import registration |
| | 113 | |
| | 114 | |
| | 115 | urlpatterns = 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 == |
| | 128 | Retstart 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 | |