2005-07-20 03:55:18 +08:00
|
|
|
=================================
|
|
|
|
How to use Django with mod_python
|
|
|
|
=================================
|
|
|
|
|
2005-09-15 03:07:29 +08:00
|
|
|
`Apache`_ with `mod_python`_ currently is the preferred setup for using Django
|
|
|
|
on a production server.
|
2005-07-20 03:55:18 +08:00
|
|
|
|
2005-09-15 03:07:29 +08: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.
|
|
|
|
|
|
|
|
.. _Apache: http://httpd.apache.org/
|
|
|
|
.. _mod_python: http://www.modpython.org/
|
|
|
|
.. _mod_perl: http://perl.apache.org/
|
2005-07-20 03:55:18 +08:00
|
|
|
|
2005-07-20 04:20:38 +08:00
|
|
|
Basic configuration
|
|
|
|
===================
|
|
|
|
|
2005-07-20 03:55:18 +08: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
|
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.settings.main
|
|
|
|
PythonDebug On
|
|
|
|
</Location>
|
|
|
|
|
2005-09-30 03:56:17 +08:00
|
|
|
...and replace ``myproject.settings.main`` with the Python path to your
|
|
|
|
settings file.
|
2005-07-20 03:55:18 +08:00
|
|
|
|
2005-07-20 04:05:52 +08: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-20 04:08:17 +08: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-20 03:55:18 +08: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-20 04:05:52 +08: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-20 03:55:18 +08:00
|
|
|
Here's a template for an admin configuration::
|
|
|
|
|
|
|
|
<Location "/admin/">
|
|
|
|
SetHandler python-program
|
|
|
|
PythonHandler django.core.handlers.modpython
|
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.settings.admin
|
|
|
|
PythonDebug On
|
|
|
|
</Location>
|
|
|
|
|
|
|
|
The only thing different here is the ``DJANGO_SETTINGS_MODULE``.
|
2005-07-20 04:05:52 +08:00
|
|
|
|
2005-07-21 23:40:33 +08: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
|
|
|
|
# ...
|
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.settings.main
|
|
|
|
</VirtualHost>
|
|
|
|
|
|
|
|
<VirtualHost *>
|
|
|
|
ServerName admin.example.com
|
|
|
|
# ...
|
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.settings.admin
|
|
|
|
</VirtualHost>
|
|
|
|
|
2005-07-22 03:42:51 +08: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-22 01:37:36 +08:00
|
|
|
|
|
|
|
<VirtualHost *>
|
|
|
|
ServerName www.example.com
|
|
|
|
# ...
|
|
|
|
<Location "/something">
|
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.settings.main
|
|
|
|
PythonInterpreter myproject_main
|
|
|
|
</Location>
|
2005-07-22 03:42:51 +08:00
|
|
|
|
2005-07-22 01:37:36 +08:00
|
|
|
<Location "/admin">
|
|
|
|
SetEnv DJANGO_SETTINGS_MODULE myproject.settings.admin
|
|
|
|
PythonInterpreter myproject_admin
|
|
|
|
</Location>
|
|
|
|
</VirtualHost>
|
|
|
|
|
2005-07-22 03:42:51 +08:00
|
|
|
The values of ``PythonInterpreter`` don't really matter, as long as they're
|
|
|
|
different between the two ``Location`` blocks.
|
2005-07-21 23:40:33 +08:00
|
|
|
|
2005-07-20 04:20:38 +08: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-07-20 04:05:52 +08:00
|
|
|
.. _mod_python documentation: http://modpython.org/live/current/doc-html/directives.html
|
2005-07-21 09:48:59 +08:00
|
|
|
|
|
|
|
Serving media files
|
|
|
|
===================
|
|
|
|
|
|
|
|
Django doesn't serve media files itself. It'd be inefficient to flow media
|
|
|
|
files through a (relatively) complex framework when much, much more well-tuned
|
|
|
|
solutions are better.
|
|
|
|
|
|
|
|
We recommend using a separate Web server for serving media. Here are some good
|
|
|
|
choices:
|
|
|
|
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
Just change ``Location`` to the root URL of your media files.
|
|
|
|
|
|
|
|
.. _lighttpd: http://www.lighttpd.net/
|
|
|
|
.. _TUX: http://en.wikipedia.org/wiki/TUX_web_server
|
|
|
|
.. _Apache: http://httpd.apache.org/
|