| 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, and you should use Apache's |
|---|
| 14 |
`prefork MPM`_, as opposed to the `worker MPM`_. |
|---|
| 15 |
|
|---|
| 16 |
You may also be interested in `How to use Django with FastCGI, SCGI or AJP`_ |
|---|
| 17 |
(which also covers SCGI and AJP). |
|---|
| 18 |
|
|---|
| 19 |
.. _Apache: http://httpd.apache.org/ |
|---|
| 20 |
.. _mod_python: http://www.modpython.org/ |
|---|
| 21 |
.. _mod_perl: http://perl.apache.org/ |
|---|
| 22 |
.. _prefork MPM: http://httpd.apache.org/docs/2.2/mod/prefork.html |
|---|
| 23 |
.. _worker MPM: http://httpd.apache.org/docs/2.2/mod/worker.html |
|---|
| 24 |
.. _How to use Django with FastCGI, SCGI or AJP: ../fastcgi/ |
|---|
| 25 |
|
|---|
| 26 |
Basic configuration |
|---|
| 27 |
=================== |
|---|
| 28 |
|
|---|
| 29 |
To configure Django with mod_python, first make sure you have Apache installed, |
|---|
| 30 |
with the mod_python module activated. |
|---|
| 31 |
|
|---|
| 32 |
Then edit your ``httpd.conf`` file and add the following:: |
|---|
| 33 |
|
|---|
| 34 |
<Location "/mysite/"> |
|---|
| 35 |
SetHandler python-program |
|---|
| 36 |
PythonHandler django.core.handlers.modpython |
|---|
| 37 |
SetEnv DJANGO_SETTINGS_MODULE mysite.settings |
|---|
| 38 |
PythonDebug On |
|---|
| 39 |
</Location> |
|---|
| 40 |
|
|---|
| 41 |
...and replace ``mysite.settings`` with the Python import path to your Django |
|---|
| 42 |
project's settings file. |
|---|
| 43 |
|
|---|
| 44 |
This tells Apache: "Use mod_python for any URL at or under '/mysite/', using the |
|---|
| 45 |
Django mod_python handler." It passes the value of ``DJANGO_SETTINGS_MODULE`` |
|---|
| 46 |
so mod_python knows which settings to use. |
|---|
| 47 |
|
|---|
| 48 |
Note that we're using the ``<Location>`` directive, not the ``<Directory>`` |
|---|
| 49 |
directive. The latter is used for pointing at places on your filesystem, |
|---|
| 50 |
whereas ``<Location>`` points at places in the URL structure of a Web site. |
|---|
| 51 |
``<Directory>`` would be meaningless here. |
|---|
| 52 |
|
|---|
| 53 |
Also, if your Django project is not on the default ``PYTHONPATH`` for your |
|---|
| 54 |
computer, you'll have to tell mod_python where your project can be found: |
|---|
| 55 |
|
|---|
| 56 |
.. parsed-literal:: |
|---|
| 57 |
|
|---|
| 58 |
<Location "/mysite/"> |
|---|
| 59 |
SetHandler python-program |
|---|
| 60 |
PythonHandler django.core.handlers.modpython |
|---|
| 61 |
SetEnv DJANGO_SETTINGS_MODULE mysite.settings |
|---|
| 62 |
PythonDebug On |
|---|
| 63 |
**PythonPath "['/path/to/project'] + sys.path"** |
|---|
| 64 |
</Location> |
|---|
| 65 |
|
|---|
| 66 |
The value you use for ``PythonPath`` should include the parent directories of |
|---|
| 67 |
all the modules you are going to import in your application. It should also |
|---|
| 68 |
include the parent directory of the ``DJANGO_SETTINGS_MODULE`` location. This |
|---|
| 69 |
is exactly the same situation as setting the Python path for interactive |
|---|
| 70 |
usage. Whenever you try to import something, Python will run through all the |
|---|
| 71 |
directories in ``sys.path`` in turn, from first to last, and try to import |
|---|
| 72 |
from each directory until one succeeds. |
|---|
| 73 |
|
|---|
| 74 |
An example might make this clearer. Suppose |
|---|
| 75 |
you have some applications under ``/usr/local/django-apps/`` (for example, |
|---|
| 76 |
``/usr/local/django-apps/weblog/`` and so forth), your settings file is at |
|---|
| 77 |
``/var/www/mysite/settings.py`` and you have specified |
|---|
| 78 |
``DJANGO_SETTINGS_MODULE`` as in the above example. In this case, you would |
|---|
| 79 |
need to write your ``PythonPath`` directive as:: |
|---|
| 80 |
|
|---|
| 81 |
PythonPath "['/usr/local/django-apps/', '/var/www'] + sys.path" |
|---|
| 82 |
|
|---|
| 83 |
With this path, ``import weblog`` and ``import mysite.settings`` will both |
|---|
| 84 |
work. If you had ``import blogroll`` in your code somewhere and ``blogroll`` |
|---|
| 85 |
lived under the ``weblog/`` directory, you would *also* need to add |
|---|
| 86 |
``/usr/local/django-apps/weblog/`` to your ``PythonPath``. Remember: the |
|---|
| 87 |
**parent directories** of anything you import directly must be on the Python |
|---|
| 88 |
path. |
|---|
| 89 |
|
|---|
| 90 |
.. note:: |
|---|
| 91 |
|
|---|
| 92 |
If you're using Windows, we still recommended that you use forward |
|---|
| 93 |
slashes in the pathnames, even though Windows normally uses the backslash |
|---|
| 94 |
character as its native separator. Apache knows how to convert from the |
|---|
| 95 |
forward slash format to the native format, so this approach is portable and |
|---|
| 96 |
easier to read. (It avoids tricky problems with having to double-escape |
|---|
| 97 |
backslashes.) |
|---|
| 98 |
|
|---|
| 99 |
This is valid even on a Windows system:: |
|---|
| 100 |
|
|---|
| 101 |
PythonPath "['c:/path/to/project'] + sys.path" |
|---|
| 102 |
|
|---|
| 103 |
You can also add directives such as ``PythonAutoReload Off`` for performance. |
|---|
| 104 |
See the `mod_python documentation`_ for a full list of options. |
|---|
| 105 |
|
|---|
| 106 |
Note that you should set ``PythonDebug Off`` on a production server. If you |
|---|
| 107 |
leave ``PythonDebug On``, your users would see ugly (and revealing) Python |
|---|
| 108 |
tracebacks if something goes wrong within mod_python. |
|---|
| 109 |
|
|---|
| 110 |
Restart Apache, and any request to /mysite/ or below will be served by Django. |
|---|
| 111 |
Note that Django's URLconfs won't trim the "/mysite/" -- they get passed the |
|---|
| 112 |
full URL. |
|---|
| 113 |
|
|---|
| 114 |
When deploying Django sites on mod_python, you'll need to restart Apache each |
|---|
| 115 |
time you make changes to your Python code. |
|---|
| 116 |
|
|---|
| 117 |
Multiple Django installations on the same Apache |
|---|
| 118 |
================================================ |
|---|
| 119 |
|
|---|
| 120 |
It's entirely possible to run multiple Django installations on the same Apache |
|---|
| 121 |
instance. Just use ``VirtualHost`` for that, like so:: |
|---|
| 122 |
|
|---|
| 123 |
NameVirtualHost * |
|---|
| 124 |
|
|---|
| 125 |
<VirtualHost *> |
|---|
| 126 |
ServerName www.example.com |
|---|
| 127 |
# ... |
|---|
| 128 |
SetEnv DJANGO_SETTINGS_MODULE mysite.settings |
|---|
| 129 |
</VirtualHost> |
|---|
| 130 |
|
|---|
| 131 |
<VirtualHost *> |
|---|
| 132 |
ServerName www2.example.com |
|---|
| 133 |
# ... |
|---|
| 134 |
SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings |
|---|
| 135 |
</VirtualHost> |
|---|
| 136 |
|
|---|
| 137 |
If you need to put two Django installations within the same ``VirtualHost``, |
|---|
| 138 |
you'll need to take a special precaution to ensure mod_python's cache doesn't |
|---|
| 139 |
mess things up. Use the ``PythonInterpreter`` directive to give different |
|---|
| 140 |
``<Location>`` directives separate interpreters:: |
|---|
| 141 |
|
|---|
| 142 |
<VirtualHost *> |
|---|
| 143 |
ServerName www.example.com |
|---|
| 144 |
# ... |
|---|
| 145 |
<Location "/something"> |
|---|
| 146 |
SetEnv DJANGO_SETTINGS_MODULE mysite.settings |
|---|
| 147 |
PythonInterpreter mysite |
|---|
| 148 |
</Location> |
|---|
| 149 |
|
|---|
| 150 |
<Location "/otherthing"> |
|---|
| 151 |
SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings |
|---|
| 152 |
PythonInterpreter othersite |
|---|
| 153 |
</Location> |
|---|
| 154 |
</VirtualHost> |
|---|
| 155 |
|
|---|
| 156 |
The values of ``PythonInterpreter`` don't really matter, as long as they're |
|---|
| 157 |
different between the two ``Location`` blocks. |
|---|
| 158 |
|
|---|
| 159 |
Running a development server with mod_python |
|---|
| 160 |
============================================ |
|---|
| 161 |
|
|---|
| 162 |
If you use mod_python for your development server, you can avoid the hassle of |
|---|
| 163 |
having to restart the server each time you make code changes. Just set |
|---|
| 164 |
``MaxRequestsPerChild 1`` in your ``httpd.conf`` file to force Apache to reload |
|---|
| 165 |
everything for each request. But don't do that on a production server, or we'll |
|---|
| 166 |
revoke your Django privileges. |
|---|
| 167 |
|
|---|
| 168 |
If you're the type of programmer who debugs using scattered ``print`` |
|---|
| 169 |
statements, note that ``print`` statements have no effect in mod_python; they |
|---|
| 170 |
don't appear in the Apache log, as one might expect. If you have the need to |
|---|
| 171 |
print debugging information in a mod_python setup, either do this:: |
|---|
| 172 |
|
|---|
| 173 |
assert False, the_value_i_want_to_see |
|---|
| 174 |
|
|---|
| 175 |
Or add the debugging information to the template of your page. |
|---|
| 176 |
|
|---|
| 177 |
.. _mod_python documentation: http://modpython.org/live/current/doc-html/directives.html |
|---|
| 178 |
|
|---|
| 179 |
Serving media files |
|---|
| 180 |
=================== |
|---|
| 181 |
|
|---|
| 182 |
Django doesn't serve media files itself; it leaves that job to whichever Web |
|---|
| 183 |
server you choose. |
|---|
| 184 |
|
|---|
| 185 |
We recommend using a separate Web server -- i.e., one that's not also running |
|---|
| 186 |
Django -- for serving media. Here are some good choices: |
|---|
| 187 |
|
|---|
| 188 |
* lighttpd_ |
|---|
| 189 |
* TUX_ |
|---|
| 190 |
* A stripped-down version of Apache_ |
|---|
| 191 |
|
|---|
| 192 |
If, however, you have no option but to serve media files on the same Apache |
|---|
| 193 |
``VirtualHost`` as Django, here's how you can turn off mod_python for a |
|---|
| 194 |
particular part of the site:: |
|---|
| 195 |
|
|---|
| 196 |
<Location "/media"> |
|---|
| 197 |
SetHandler None |
|---|
| 198 |
</Location> |
|---|
| 199 |
|
|---|
| 200 |
Just change ``Location`` to the root URL of your media files. You can also use |
|---|
| 201 |
``<LocationMatch>`` to match a regular expression. |
|---|
| 202 |
|
|---|
| 203 |
This example sets up Django at the site root but explicitly disables Django for |
|---|
| 204 |
the ``media`` subdirectory and any URL that ends with ``.jpg``, ``.gif`` or |
|---|
| 205 |
``.png``:: |
|---|
| 206 |
|
|---|
| 207 |
<Location "/"> |
|---|
| 208 |
SetHandler python-program |
|---|
| 209 |
PythonHandler django.core.handlers.modpython |
|---|
| 210 |
SetEnv DJANGO_SETTINGS_MODULE mysite.settings |
|---|
| 211 |
</Location> |
|---|
| 212 |
|
|---|
| 213 |
<Location "/media"> |
|---|
| 214 |
SetHandler None |
|---|
| 215 |
</Location> |
|---|
| 216 |
|
|---|
| 217 |
<LocationMatch "\.(jpg|gif|png)$"> |
|---|
| 218 |
SetHandler None |
|---|
| 219 |
</LocationMatch> |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
.. _lighttpd: http://www.lighttpd.net/ |
|---|
| 223 |
.. _TUX: http://en.wikipedia.org/wiki/TUX_web_server |
|---|
| 224 |
.. _Apache: http://httpd.apache.org/ |
|---|
| 225 |
|
|---|
| 226 |
Serving the admin files |
|---|
| 227 |
======================= |
|---|
| 228 |
|
|---|
| 229 |
Note that the Django development server automagically serves admin media files, |
|---|
| 230 |
but this is not the case when you use any other server arrangement. You're |
|---|
| 231 |
responsible for setting up Apache, or whichever media server you're using, to |
|---|
| 232 |
serve the admin files. |
|---|
| 233 |
|
|---|
| 234 |
The admin files live in (``django/contrib/admin/media``) of the Django |
|---|
| 235 |
distribution. |
|---|
| 236 |
|
|---|
| 237 |
Here are two recommended approaches: |
|---|
| 238 |
|
|---|
| 239 |
1. Create a symbolic link to the admin media files from within your |
|---|
| 240 |
document root. This way, all of your Django-related files -- code |
|---|
| 241 |
**and** templates -- stay in one place, and you'll still be able to |
|---|
| 242 |
``svn update`` your code to get the latest admin templates, if they |
|---|
| 243 |
change. |
|---|
| 244 |
2. Or, copy the admin media files so that they live within your Apache |
|---|
| 245 |
document root. |
|---|
| 246 |
|
|---|
| 247 |
Using eggs with mod_python |
|---|
| 248 |
========================== |
|---|
| 249 |
|
|---|
| 250 |
If you installed Django from a Python egg_ or are using eggs in your Django |
|---|
| 251 |
project, some extra configuration is required. Create an extra file in your |
|---|
| 252 |
project (or somewhere else) that contains something like the following:: |
|---|
| 253 |
|
|---|
| 254 |
import os |
|---|
| 255 |
os.environ['PYTHON_EGG_CACHE'] = '/some/directory' |
|---|
| 256 |
|
|---|
| 257 |
Here, ``/some/directory`` is a directory that the Apache webserver process can |
|---|
| 258 |
write to. It will be used as the location for any unpacking of code the eggs |
|---|
| 259 |
need to do. |
|---|
| 260 |
|
|---|
| 261 |
Then you have to tell mod_python to import this file before doing anything |
|---|
| 262 |
else. This is done using the PythonImport_ directive to mod_python. You need |
|---|
| 263 |
to ensure that you have specified the ``PythonInterpreter`` directive to |
|---|
| 264 |
mod_python as described above__ (you need to do this even if you aren't |
|---|
| 265 |
serving multiple installations in this case). Then add the ``PythonImport`` |
|---|
| 266 |
line in the main server configuration (i.e., outside the ``Location`` or |
|---|
| 267 |
``VirtualHost`` sections). For example:: |
|---|
| 268 |
|
|---|
| 269 |
PythonInterpreter my_django |
|---|
| 270 |
PythonImport /path/to/my/project/file.py my_django |
|---|
| 271 |
|
|---|
| 272 |
Note that you can use an absolute path here (or a normal dotted import path), |
|---|
| 273 |
as described in the `mod_python manual`_. We use an absolute path in the |
|---|
| 274 |
above example because if any Python path modifications are required to access |
|---|
| 275 |
your project, they will not have been done at the time the ``PythonImport`` |
|---|
| 276 |
line is processed. |
|---|
| 277 |
|
|---|
| 278 |
.. _Egg: http://peak.telecommunity.com/DevCenter/PythonEggs |
|---|
| 279 |
.. _PythonImport: http://www.modpython.org/live/current/doc-html/dir-other-pimp.html |
|---|
| 280 |
.. _mod_python manual: PythonImport_ |
|---|
| 281 |
__ `Multiple Django installations on the same Apache`_ |
|---|
| 282 |
|
|---|
| 283 |
Error handling |
|---|
| 284 |
============== |
|---|
| 285 |
|
|---|
| 286 |
When you use Apache/mod_python, errors will be caught by Django -- in other |
|---|
| 287 |
words, they won't propagate to the Apache level and won't appear in the Apache |
|---|
| 288 |
``error_log``. |
|---|
| 289 |
|
|---|
| 290 |
The exception for this is if something is really wonky in your Django setup. In |
|---|
| 291 |
that case, you'll see an "Internal Server Error" page in your browser and the |
|---|
| 292 |
full Python traceback in your Apache ``error_log`` file. The ``error_log`` |
|---|
| 293 |
traceback is spread over multiple lines. (Yes, this is ugly and rather hard to |
|---|
| 294 |
read, but it's how mod_python does things.) |
|---|
| 295 |
|
|---|
| 296 |
If you get a segmentation fault |
|---|
| 297 |
=============================== |
|---|
| 298 |
|
|---|
| 299 |
If Apache causes a segmentation fault, there are two probable causes, neither |
|---|
| 300 |
of which has to do with Django itself. |
|---|
| 301 |
|
|---|
| 302 |
1. It may be because your Python code is importing the "pyexpat" module, |
|---|
| 303 |
which may conflict with the version embedded in Apache. For full |
|---|
| 304 |
information, see `Expat Causing Apache Crash`_. |
|---|
| 305 |
2. It may be because you're running mod_python and mod_php in the same |
|---|
| 306 |
Apache instance, with MySQL as your database backend. In some cases, |
|---|
| 307 |
this causes a known mod_python issue due to version conflicts in PHP and |
|---|
| 308 |
the Python MySQL backend. There's full information in the |
|---|
| 309 |
`mod_python FAQ entry`_. |
|---|
| 310 |
|
|---|
| 311 |
If you continue to have problems setting up mod_python, a good thing to do is |
|---|
| 312 |
get a barebones mod_python site working, without the Django framework. This is |
|---|
| 313 |
an easy way to isolate mod_python-specific problems. `Getting mod_python Working`_ |
|---|
| 314 |
details this procedure. |
|---|
| 315 |
|
|---|
| 316 |
The next step should be to edit your test code and add an import of any |
|---|
| 317 |
Django-specific code you're using -- your views, your models, your URLconf, |
|---|
| 318 |
your RSS configuration, etc. Put these imports in your test handler function |
|---|
| 319 |
and access your test URL in a browser. If this causes a crash, you've confirmed |
|---|
| 320 |
it's the importing of Django code that causes the problem. Gradually reduce the |
|---|
| 321 |
set of imports until it stops crashing, so as to find the specific module that |
|---|
| 322 |
causes the problem. Drop down further into modules and look into their imports, |
|---|
| 323 |
as necessary. |
|---|
| 324 |
|
|---|
| 325 |
.. _Expat Causing Apache Crash: http://www.dscpl.com.au/articles/modpython-006.html |
|---|
| 326 |
.. _mod_python FAQ entry: http://modpython.org/FAQ/faqw.py?req=show&file=faq02.013.htp |
|---|
| 327 |
.. _Getting mod_python Working: http://www.dscpl.com.au/articles/modpython-001.html |
|---|