2005-07-19 19:55:18 +00:00
|
|
|
=================================
|
|
|
|
How to use Django with mod_python
|
|
|
|
=================================
|
|
|
|
|
2005-11-07 14:47:29 +00:00
|
|
|
Apache_ with `mod_python`_ currently is the preferred setup for using Django
|
2005-09-14 19:07:29 +00:00
|
|
|
on a production server.
|
2005-07-19 19:55:18 +00:00
|
|
|
|
2005-09-14 19:07:29 +00:00
|
|
|
mod_python is similar to `mod_perl`_ : It embeds Python within Apache and loads
|
|
|
|
Python code into memory when the server starts. Code stays in memory throughout
|
|
|
|
the life of an Apache process, which leads to significant performance gains over
|
|
|
|
other server arrangements.
|
|
|
|
|
2005-11-07 14:39:13 +00:00
|
|
|
Django requires Apache 2.x and mod_python 3.x.
|
|
|
|
|
2005-09-14 19:07:29 +00:00
|
|
|
.. _Apache: http://httpd.apache.org/
|
|
|
|
.. _mod_python: http://www.modpython.org/
|
|
|
|
.. _mod_perl: http://perl.apache.org/
|
2005-07-19 19:55:18 +00:00
|
|
|
|
2005-07-19 20:20:38 +00:00
|
|
|
Basic configuration
|
|
|
|
===================
|
|
|
|
|
2005-07-19 19:55:18 +00:00
|
|
|
To configure Django with mod_python, first make sure you have Apache installed,
|
|
|
|
with the mod_python module activated.
|
|
|
|
|
|
|
|
Then edit your ``httpd.conf`` file and add the following::
|
|
|
|
|
|
|
|
<Location "/mysite/">
|
|
|
|
SetHandler python-program
|
|
|
|
PythonHandler django.core.handlers.modpython
|
2005-10-19 01:09:05 +00:00
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
|
2005-07-19 19:55:18 +00:00
|
|
|
PythonDebug On
|
|
|
|
</Location>
|
|
|
|
|
2005-10-19 01:09:05 +00:00
|
|
|
...and replace ``myproject.settings`` with the Python path to your settings file.
|
2005-07-19 19:55:18 +00:00
|
|
|
|
2005-07-19 20:05:52 +00:00
|
|
|
This tells Apache: "Use mod_python for any URL at or under '/mysite/', using the
|
|
|
|
Django mod_python handler." It passes the value of ``DJANGO_SETTINGS_MODULE``
|
|
|
|
so mod_python knows which settings to use.
|
|
|
|
|
|
|
|
Also, if you've manually altered your ``PYTHONPATH`` to put your Django project
|
|
|
|
on it, you'll need to tell mod_python::
|
|
|
|
|
|
|
|
PythonPath "['/path/to/project'] + sys.path"
|
|
|
|
|
|
|
|
You can also add directives such as ``PythonAutoReload Off`` for performance.
|
|
|
|
See the `mod_python documentation`_ for a full list of options.
|
|
|
|
|
2005-07-19 20:08:17 +00:00
|
|
|
Note that you should set ``PythonDebug Off`` on a production server. If you
|
|
|
|
leave ``PythonDebug On``, your users would see ugly (and revealing) Python
|
|
|
|
tracebacks if something goes wrong within mod_python.
|
|
|
|
|
2005-07-19 19:55:18 +00:00
|
|
|
Restart Apache, and any request to /mysite/ or below will be served by Django.
|
|
|
|
Note that Django's URLconfs won't trim the "/mysite/" -- they get passed the
|
|
|
|
full URL.
|
|
|
|
|
2005-07-19 20:05:52 +00:00
|
|
|
When deploying Django sites on mod_python, you'll need to restart Apache each
|
|
|
|
time you make changes to your Python code.
|
|
|
|
|
2005-07-21 15:40:33 +00:00
|
|
|
Multiple Django installations on the same Apache
|
|
|
|
================================================
|
|
|
|
|
|
|
|
It's entirely possible to run multiple Django installations on the same Apache
|
|
|
|
instance. Just use ``VirtualHost`` for that, like so::
|
|
|
|
|
|
|
|
NameVirtualHost *
|
|
|
|
|
|
|
|
<VirtualHost *>
|
|
|
|
ServerName www.example.com
|
|
|
|
# ...
|
2005-10-19 01:09:05 +00:00
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
|
2005-07-21 15:40:33 +00:00
|
|
|
</VirtualHost>
|
|
|
|
|
|
|
|
<VirtualHost *>
|
2005-10-19 01:09:05 +00:00
|
|
|
ServerName www2.example.com
|
2005-07-21 15:40:33 +00:00
|
|
|
# ...
|
2005-10-19 01:09:05 +00:00
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.other_settings
|
2005-07-21 15:40:33 +00:00
|
|
|
</VirtualHost>
|
|
|
|
|
2005-07-21 19:42:51 +00:00
|
|
|
If you need to put two Django installations within the same ``VirtualHost``,
|
|
|
|
you'll need to take a special precaution to ensure mod_python's cache doesn't
|
|
|
|
mess things up. Use the ``PythonInterpreter`` directive to give different
|
|
|
|
``<Location>`` directives separate interpreters::
|
2005-07-21 17:37:36 +00:00
|
|
|
|
|
|
|
<VirtualHost *>
|
|
|
|
ServerName www.example.com
|
|
|
|
# ...
|
|
|
|
<Location "/something">
|
2005-10-19 01:09:05 +00:00
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
|
|
|
|
PythonInterpreter myproject
|
2005-07-21 17:37:36 +00:00
|
|
|
</Location>
|
2005-07-21 19:42:51 +00:00
|
|
|
|
2005-10-19 01:09:05 +00:00
|
|
|
<Location "/otherthing">
|
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.other_settings
|
|
|
|
PythonInterpreter myproject_other
|
2005-07-21 17:37:36 +00:00
|
|
|
</Location>
|
|
|
|
</VirtualHost>
|
|
|
|
|
2005-07-21 19:42:51 +00:00
|
|
|
The values of ``PythonInterpreter`` don't really matter, as long as they're
|
|
|
|
different between the two ``Location`` blocks.
|
2005-07-21 15:40:33 +00:00
|
|
|
|
2005-07-19 20:20:38 +00:00
|
|
|
Running a development server with mod_python
|
|
|
|
============================================
|
|
|
|
|
|
|
|
If you use mod_python for your development server, you can avoid the hassle of
|
|
|
|
having to restart the server each time you make code changes. Just set
|
|
|
|
``MaxRequestsPerChild 1`` in your ``httpd.conf`` file to force Apache to reload
|
|
|
|
everything for each request. But don't do that on a production server, or we'll
|
|
|
|
revoke your Django privileges.
|
|
|
|
|
2005-11-27 13:53:35 +00:00
|
|
|
If you're the type of programmer who debugs using scattered ``print``
|
|
|
|
statements, note that ``print`` statements have no effect in mod_python; they
|
|
|
|
don't appear in the Apache log, as one might expect. If you have the need to
|
|
|
|
print debugging information in a mod_python setup, either do this::
|
|
|
|
|
|
|
|
assert False, the_value_i_want_to_see
|
|
|
|
|
|
|
|
Or add the debugging information to the template of your page.
|
|
|
|
|
2005-07-19 20:05:52 +00:00
|
|
|
.. _mod_python documentation: http://modpython.org/live/current/doc-html/directives.html
|
2005-07-21 01:48:59 +00:00
|
|
|
|
|
|
|
Serving media files
|
|
|
|
===================
|
|
|
|
|
2005-11-07 14:47:29 +00:00
|
|
|
Django doesn't serve media files itself; it leaves that job to whichever Web
|
|
|
|
server you choose.
|
2005-07-21 01:48:59 +00:00
|
|
|
|
2005-11-07 14:47:29 +00:00
|
|
|
We recommend using a separate Web server -- i.e., one that's not also running
|
|
|
|
Django -- for serving media. Here are some good choices:
|
2005-07-21 01:48:59 +00:00
|
|
|
|
|
|
|
* lighttpd_
|
|
|
|
* TUX_
|
|
|
|
* A stripped-down version of Apache_
|
|
|
|
|
|
|
|
If, however, you have no option but to serve media files on the same Apache
|
|
|
|
``VirtualHost`` as Django, here's how you can turn off mod_python for a
|
|
|
|
particular part of the site::
|
|
|
|
|
|
|
|
<Location "/media/">
|
|
|
|
SetHandler None
|
|
|
|
</Location>
|
|
|
|
|
2005-11-07 14:39:13 +00:00
|
|
|
Just change ``Location`` to the root URL of your media files. You can also use
|
|
|
|
``<LocationMatch>`` to match a regular expression.
|
|
|
|
|
|
|
|
This example sets up Django at the site root but explicitly disables Django for
|
|
|
|
the ``media`` subdirectory and any URL that ends with ``.jpg``, ``.gif`` or
|
|
|
|
``.png``::
|
|
|
|
|
|
|
|
<Location "/">
|
|
|
|
SetHandler python-program
|
|
|
|
PythonHandler django.core.handlers.modpython
|
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
|
|
|
|
</Location>
|
|
|
|
|
|
|
|
<Location "media">
|
|
|
|
SetHandler None
|
|
|
|
</Location>
|
|
|
|
|
|
|
|
<LocationMatch "\.(jpg|gif|png)$">
|
|
|
|
SetHandler None
|
2005-11-13 16:14:14 +00:00
|
|
|
</LocationMatch>
|
2005-07-21 01:48:59 +00:00
|
|
|
|
2005-10-12 03:52:05 +00:00
|
|
|
|
2005-07-21 01:48:59 +00:00
|
|
|
.. _lighttpd: http://www.lighttpd.net/
|
|
|
|
.. _TUX: http://en.wikipedia.org/wiki/TUX_web_server
|
|
|
|
.. _Apache: http://httpd.apache.org/
|
2005-11-07 14:47:29 +00:00
|
|
|
|
|
|
|
Serving the admin files
|
|
|
|
=======================
|
|
|
|
|
|
|
|
Note that the Django development server automagically serves admin media files,
|
|
|
|
but this is not the case when you use any other server arrangement. You're
|
|
|
|
responsible for setting up Apache, or whichever media server you're using, to
|
|
|
|
serve the admin files.
|
|
|
|
|
|
|
|
The admin files live in (``django/contrib/admin/media``) of the Django
|
|
|
|
distribution.
|
|
|
|
|
|
|
|
Here are two recommended approaches:
|
|
|
|
|
|
|
|
1. Create a symbolic link to the admin media files from within your
|
|
|
|
document root. This way, all of your Django-related files -- code
|
|
|
|
**and** templates -- stay in one place, and you'll still be able to
|
|
|
|
``svn update`` your code to get the latest admin templates, if they
|
|
|
|
change.
|
|
|
|
2. Or, copy the admin media files so that they live within your document
|
|
|
|
root.
|
2005-11-26 03:17:52 +00:00
|
|
|
|
|
|
|
Error handling
|
|
|
|
==============
|
|
|
|
|
|
|
|
When you use Apache/mod_python, errors will be caught by Django -- in other
|
|
|
|
words, they won't propogate to the Apache level and won't appear in the Apache
|
|
|
|
``error_log``.
|
|
|
|
|
|
|
|
The exception for this is if something is really wonky in your Django setup. In
|
|
|
|
that case, you'll see an "Internal Server Error" page in your browser and the
|
|
|
|
full Python traceback in your Apache ``error_log`` file. The ``error_log``
|
|
|
|
traceback is spread over multiple lines. (Yes, this is ugly and rather hard to
|
|
|
|
read, but it's how mod_python does things.)
|