| 1 |
================================= |
|---|
| 2 |
How to use Django with mod_python |
|---|
| 3 |
================================= |
|---|
| 4 |
|
|---|
| 5 |
Apache_ with `mod_python`_ currently is the preferred setup for using Django |
|---|
| 6 |
on a production server. |
|---|
| 7 |
|
|---|
| 8 |
mod_python is similar to `mod_perl`_ : It embeds Python within Apache and loads |
|---|
| 9 |
Python code into memory when the server starts. Code stays in memory throughout |
|---|
| 10 |
the life of an Apache process, which leads to significant performance gains over |
|---|
| 11 |
other server arrangements. |
|---|
| 12 |
|
|---|
| 13 |
Django requires Apache 2.x and mod_python 3.x. |
|---|
| 14 |
|
|---|
| 15 |
.. _Apache: http://httpd.apache.org/ |
|---|
| 16 |
.. _mod_python: http://www.modpython.org/ |
|---|
| 17 |
.. _mod_perl: http://perl.apache.org/ |
|---|
| 18 |
|
|---|
| 19 |
Basic configuration |
|---|
| 20 |
=================== |
|---|
| 21 |
|
|---|
| 22 |
To configure Django with mod_python, first make sure you have Apache installed, |
|---|
| 23 |
with the mod_python module activated. |
|---|
| 24 |
|
|---|
| 25 |
Then edit your ``httpd.conf`` file and add the following:: |
|---|
| 26 |
|
|---|
| 27 |
<Location "/mysite/"> |
|---|
| 28 |
SetHandler python-program |
|---|
| 29 |
PythonHandler django.core.handlers.modpython |
|---|
| 30 |
SetEnv DJANGO_SETTINGS_MODULE myproject.settings |
|---|
| 31 |
PythonDebug On |
|---|
| 32 |
</Location> |
|---|
| 33 |
|
|---|
| 34 |
...and replace ``myproject.settings`` with the Python path to your settings file. |
|---|
| 35 |
|
|---|
| 36 |
This tells Apache: "Use mod_python for any URL at or under '/mysite/', using the |
|---|
| 37 |
Django mod_python handler." It passes the value of ``DJANGO_SETTINGS_MODULE`` |
|---|
| 38 |
so mod_python knows which settings to use. |
|---|
| 39 |
|
|---|
| 40 |
Also, if you've manually altered your ``PYTHONPATH`` to put your Django project |
|---|
| 41 |
on it, you'll need to tell mod_python:: |
|---|
| 42 |
|
|---|
| 43 |
PythonPath "['/path/to/project'] + sys.path" |
|---|
| 44 |
|
|---|
| 45 |
You can also add directives such as ``PythonAutoReload Off`` for performance. |
|---|
| 46 |
See the `mod_python documentation`_ for a full list of options. |
|---|
| 47 |
|
|---|
| 48 |
Note that you should set ``PythonDebug Off`` on a production server. If you |
|---|
| 49 |
leave ``PythonDebug On``, your users would see ugly (and revealing) Python |
|---|
| 50 |
tracebacks if something goes wrong within mod_python. |
|---|
| 51 |
|
|---|
| 52 |
Restart Apache, and any request to /mysite/ or below will be served by Django. |
|---|
| 53 |
Note that Django's URLconfs won't trim the "/mysite/" -- they get passed the |
|---|
| 54 |
full URL. |
|---|
| 55 |
|
|---|
| 56 |
When deploying Django sites on mod_python, you'll need to restart Apache each |
|---|
| 57 |
time you make changes to your Python code. |
|---|
| 58 |
|
|---|
| 59 |
Multiple Django installations on the same Apache |
|---|
| 60 |
================================================ |
|---|
| 61 |
|
|---|
| 62 |
It's entirely possible to run multiple Django installations on the same Apache |
|---|
| 63 |
instance. Just use ``VirtualHost`` for that, like so:: |
|---|
| 64 |
|
|---|
| 65 |
NameVirtualHost * |
|---|
| 66 |
|
|---|
| 67 |
<VirtualHost *> |
|---|
| 68 |
ServerName www.example.com |
|---|
| 69 |
# ... |
|---|
| 70 |
SetEnv DJANGO_SETTINGS_MODULE myproject.settings |
|---|
| 71 |
</VirtualHost> |
|---|
| 72 |
|
|---|
| 73 |
<VirtualHost *> |
|---|
| 74 |
ServerName www2.example.com |
|---|
| 75 |
# ... |
|---|
| 76 |
SetEnv DJANGO_SETTINGS_MODULE myproject.other_settings |
|---|
| 77 |
</VirtualHost> |
|---|
| 78 |
|
|---|
| 79 |
If you need to put two Django installations within the same ``VirtualHost``, |
|---|
| 80 |
you'll need to take a special precaution to ensure mod_python's cache doesn't |
|---|
| 81 |
mess things up. Use the ``PythonInterpreter`` directive to give different |
|---|
| 82 |
``<Location>`` directives separate interpreters:: |
|---|
| 83 |
|
|---|
| 84 |
<VirtualHost *> |
|---|
| 85 |
ServerName www.example.com |
|---|
| 86 |
# ... |
|---|
| 87 |
<Location "/something"> |
|---|
| 88 |
SetEnv DJANGO_SETTINGS_MODULE myproject.settings |
|---|
| 89 |
PythonInterpreter myproject |
|---|
| 90 |
</Location> |
|---|
| 91 |
|
|---|
| 92 |
<Location "/otherthing"> |
|---|
| 93 |
SetEnv DJANGO_SETTINGS_MODULE myproject.other_settings |
|---|
| 94 |
PythonInterpreter myproject_other |
|---|
| 95 |
</Location> |
|---|
| 96 |
</VirtualHost> |
|---|
| 97 |
|
|---|
| 98 |
The values of ``PythonInterpreter`` don't really matter, as long as they're |
|---|
| 99 |
different between the two ``Location`` blocks. |
|---|
| 100 |
|
|---|
| 101 |
Running a development server with mod_python |
|---|
| 102 |
============================================ |
|---|
| 103 |
|
|---|
| 104 |
If you use mod_python for your development server, you can avoid the hassle of |
|---|
| 105 |
having to restart the server each time you make code changes. Just set |
|---|
| 106 |
``MaxRequestsPerChild 1`` in your ``httpd.conf`` file to force Apache to reload |
|---|
| 107 |
everything for each request. But don't do that on a production server, or we'll |
|---|
| 108 |
revoke your Django privileges. |
|---|
| 109 |
|
|---|
| 110 |
.. _mod_python documentation: http://modpython.org/live/current/doc-html/directives.html |
|---|
| 111 |
|
|---|
| 112 |
Serving media files |
|---|
| 113 |
=================== |
|---|
| 114 |
|
|---|
| 115 |
Django doesn't serve media files itself; it leaves that job to whichever Web |
|---|
| 116 |
server you choose. |
|---|
| 117 |
|
|---|
| 118 |
We recommend using a separate Web server -- i.e., one that's not also running |
|---|
| 119 |
Django -- for serving media. Here are some good choices: |
|---|
| 120 |
|
|---|
| 121 |
* lighttpd_ |
|---|
| 122 |
* TUX_ |
|---|
| 123 |
* A stripped-down version of Apache_ |
|---|
| 124 |
|
|---|
| 125 |
If, however, you have no option but to serve media files on the same Apache |
|---|
| 126 |
``VirtualHost`` as Django, here's how you can turn off mod_python for a |
|---|
| 127 |
particular part of the site:: |
|---|
| 128 |
|
|---|
| 129 |
<Location "/media/"> |
|---|
| 130 |
SetHandler None |
|---|
| 131 |
</Location> |
|---|
| 132 |
|
|---|
| 133 |
Just change ``Location`` to the root URL of your media files. You can also use |
|---|
| 134 |
``<LocationMatch>`` to match a regular expression. |
|---|
| 135 |
|
|---|
| 136 |
This example sets up Django at the site root but explicitly disables Django for |
|---|
| 137 |
the ``media`` subdirectory and any URL that ends with ``.jpg``, ``.gif`` or |
|---|
| 138 |
``.png``:: |
|---|
| 139 |
|
|---|
| 140 |
<Location "/"> |
|---|
| 141 |
SetHandler python-program |
|---|
| 142 |
PythonHandler django.core.handlers.modpython |
|---|
| 143 |
SetEnv DJANGO_SETTINGS_MODULE myproject.settings |
|---|
| 144 |
</Location> |
|---|
| 145 |
|
|---|
| 146 |
<Location "media"> |
|---|
| 147 |
SetHandler None |
|---|
| 148 |
</Location> |
|---|
| 149 |
|
|---|
| 150 |
<LocationMatch "\.(jpg|gif|png)$"> |
|---|
| 151 |
SetHandler None |
|---|
| 152 |
</LocationMatch> |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
.. _lighttpd: http://www.lighttpd.net/ |
|---|
| 156 |
.. _TUX: http://en.wikipedia.org/wiki/TUX_web_server |
|---|
| 157 |
.. _Apache: http://httpd.apache.org/ |
|---|
| 158 |
|
|---|
| 159 |
Serving the admin files |
|---|
| 160 |
======================= |
|---|
| 161 |
|
|---|
| 162 |
Note that the Django development server automagically serves admin media files, |
|---|
| 163 |
but this is not the case when you use any other server arrangement. You're |
|---|
| 164 |
responsible for setting up Apache, or whichever media server you're using, to |
|---|
| 165 |
serve the admin files. |
|---|
| 166 |
|
|---|
| 167 |
The admin files live in (``django/contrib/admin/media``) of the Django |
|---|
| 168 |
distribution. |
|---|
| 169 |
|
|---|
| 170 |
Here are two recommended approaches: |
|---|
| 171 |
|
|---|
| 172 |
1. Create a symbolic link to the admin media files from within your |
|---|
| 173 |
document root. This way, all of your Django-related files -- code |
|---|
| 174 |
**and** templates -- stay in one place, and you'll still be able to |
|---|
| 175 |
``svn update`` your code to get the latest admin templates, if they |
|---|
| 176 |
change. |
|---|
| 177 |
2. Or, copy the admin media files so that they live within your document |
|---|
| 178 |
root. |
|---|