2008-08-24 06:25:40 +08:00
|
|
|
============================================
|
|
|
|
How to use Django with Apache and mod_python
|
|
|
|
============================================
|
|
|
|
|
2010-08-28 10:40:57 +08:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Support for mod_python will be deprecated in a future release of Django. If
|
|
|
|
you are configuring a new deployment, you are strongly encouraged to
|
|
|
|
consider using :doc:`mod_wsgi </howto/deployment/modwsgi>` or any of the
|
|
|
|
other :doc:`supported backends </howto/deployment/index>`.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. highlight:: apache
|
2005-07-20 03:55:18 +08:00
|
|
|
|
2009-07-03 15:16:25 +08:00
|
|
|
The `mod_python`_ module for Apache_ can be used to deploy Django to a
|
|
|
|
production server, although it has been mostly superseded by the simpler
|
2010-08-20 03:27:44 +08:00
|
|
|
:doc:`mod_wsgi deployment option </howto/deployment/modwsgi>`.
|
2005-07-20 03:55:18 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
mod_python is similar to (and inspired by) `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-09-15 03:07:29 +08:00
|
|
|
|
2006-06-26 20:43:21 +08:00
|
|
|
Django requires Apache 2.x and mod_python 3.x, and you should use Apache's
|
|
|
|
`prefork MPM`_, as opposed to the `worker MPM`_.
|
2005-11-07 22:39:13 +08:00
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
* Apache is a big, complex animal, and this document only scratches the
|
|
|
|
surface of what Apache can do. If you need more advanced information about
|
|
|
|
Apache, there's no better source than `Apache's own official
|
|
|
|
documentation`_
|
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
* You may also be interested in :doc:`How to use Django with FastCGI, SCGI,
|
|
|
|
or AJP </howto/deployment/fastcgi>`.
|
2006-04-22 00:59:14 +08:00
|
|
|
|
2005-09-15 03:07:29 +08:00
|
|
|
.. _Apache: http://httpd.apache.org/
|
|
|
|
.. _mod_python: http://www.modpython.org/
|
|
|
|
.. _mod_perl: http://perl.apache.org/
|
2006-04-22 00:59:14 +08:00
|
|
|
.. _prefork MPM: http://httpd.apache.org/docs/2.2/mod/prefork.html
|
|
|
|
.. _worker MPM: http://httpd.apache.org/docs/2.2/mod/worker.html
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
.. _apache's own official documentation: http://httpd.apache.org/docs/
|
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
|
2006-05-02 09:31:56 +08:00
|
|
|
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
|
Fixed #10389, #10501, #10502, #10540, #10562, #10563, #10564, #10565, #10568, #10569, #10614, #10617, #10619 -- Fixed several typos as well as a couple minor issues in the docs, patches from timo, nih, bthomas, rduffield, UloPe, and sebleier@gmail.com.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10242 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-31 15:01:01 +08:00
|
|
|
PythonOption django.root /mysite
|
2005-07-20 03:55:18 +08:00
|
|
|
PythonDebug On
|
|
|
|
</Location>
|
|
|
|
|
2007-03-30 14:16:41 +08:00
|
|
|
...and replace ``mysite.settings`` with the Python import path to your Django
|
|
|
|
project's 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
|
2008-08-24 06:25:40 +08:00
|
|
|
Django mod_python handler." It passes the value of :ref:`DJANGO_SETTINGS_MODULE
|
|
|
|
<django-settings-module>` so mod_python knows which settings to use.
|
2005-07-20 04:05:52 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionadded:: 1.0
|
|
|
|
The ``PythonOption django.root ...`` is new in this version.
|
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
Because mod_python does not know we are serving this site from underneath the
|
|
|
|
``/mysite/`` prefix, this value needs to be passed through to the mod_python
|
|
|
|
handler in Django, via the ``PythonOption django.root ...`` line. The value set
|
|
|
|
on that line (the last item) should match the string given in the ``<Location
|
|
|
|
...>`` directive. The effect of this is that Django will automatically strip the
|
|
|
|
``/mysite`` string from the front of any URLs before matching them against your
|
|
|
|
URLconf patterns. If you later move your site to live under ``/mysite2``, you
|
|
|
|
will not have to change anything except the ``django.root`` option in the config
|
|
|
|
file.
|
2008-07-21 15:57:10 +08:00
|
|
|
|
|
|
|
When using ``django.root`` you should make sure that what's left, after the
|
2009-04-01 00:07:07 +08:00
|
|
|
prefix has been removed, begins with a slash. Your URLconf patterns that are
|
2008-07-21 15:57:10 +08:00
|
|
|
expecting an initial slash will then work correctly. In the above example,
|
|
|
|
since we want to send things like ``/mysite/admin/`` to ``/admin/``, we need
|
|
|
|
to remove the string ``/mysite`` from the beginning, so that is the
|
|
|
|
``django.root`` value. It would be an error to use ``/mysite/`` (with a
|
|
|
|
trailing slash) in this case.
|
|
|
|
|
2006-10-27 10:03:15 +08:00
|
|
|
Note that we're using the ``<Location>`` directive, not the ``<Directory>``
|
|
|
|
directive. The latter is used for pointing at places on your filesystem,
|
|
|
|
whereas ``<Location>`` points at places in the URL structure of a Web site.
|
|
|
|
``<Directory>`` would be meaningless here.
|
|
|
|
|
2007-07-12 21:32:00 +08:00
|
|
|
Also, if your Django project is not on the default ``PYTHONPATH`` for your
|
|
|
|
computer, you'll have to tell mod_python where your project can be found:
|
2005-07-20 04:05:52 +08:00
|
|
|
|
2007-06-08 02:03:21 +08:00
|
|
|
.. parsed-literal::
|
|
|
|
|
|
|
|
<Location "/mysite/">
|
|
|
|
SetHandler python-program
|
|
|
|
PythonHandler django.core.handlers.modpython
|
|
|
|
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
|
Fixed #10389, #10501, #10502, #10540, #10562, #10563, #10564, #10565, #10568, #10569, #10614, #10617, #10619 -- Fixed several typos as well as a couple minor issues in the docs, patches from timo, nih, bthomas, rduffield, UloPe, and sebleier@gmail.com.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10242 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-31 15:01:01 +08:00
|
|
|
PythonOption django.root /mysite
|
2007-06-08 02:03:21 +08:00
|
|
|
PythonDebug On
|
|
|
|
**PythonPath "['/path/to/project'] + sys.path"**
|
|
|
|
</Location>
|
2005-07-20 04:05:52 +08:00
|
|
|
|
2007-08-15 20:25:21 +08:00
|
|
|
The value you use for ``PythonPath`` should include the parent directories of
|
|
|
|
all the modules you are going to import in your application. It should also
|
2008-08-24 06:25:40 +08:00
|
|
|
include the parent directory of the :ref:`DJANGO_SETTINGS_MODULE
|
|
|
|
<django-settings-module>` location. This is exactly the same situation as
|
|
|
|
setting the Python path for interactive usage. Whenever you try to import
|
|
|
|
something, Python will run through all the directories in ``sys.path`` in turn,
|
|
|
|
from first to last, and try to import from each directory until one succeeds.
|
2007-08-15 20:25:21 +08:00
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
Make sure that your Python source files' permissions are set such that the
|
|
|
|
Apache user (usually named ``apache`` or ``httpd`` on most systems) will have
|
|
|
|
read access to the files.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
An example might make this clearer. Suppose you have some applications under
|
|
|
|
``/usr/local/django-apps/`` (for example, ``/usr/local/django-apps/weblog/`` and
|
|
|
|
so forth), your settings file is at ``/var/www/mysite/settings.py`` and you have
|
|
|
|
specified :ref:`DJANGO_SETTINGS_MODULE <django-settings-module>` as in the above
|
|
|
|
example. In this case, you would need to write your ``PythonPath`` directive
|
|
|
|
as::
|
2007-08-15 20:25:21 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
PythonPath "['/usr/local/django-apps/', '/var/www'] + sys.path"
|
2007-08-15 20:25:21 +08:00
|
|
|
|
2007-08-15 20:29:15 +08:00
|
|
|
With this path, ``import weblog`` and ``import mysite.settings`` will both
|
2007-08-15 20:25:21 +08:00
|
|
|
work. If you had ``import blogroll`` in your code somewhere and ``blogroll``
|
|
|
|
lived under the ``weblog/`` directory, you would *also* need to add
|
2007-09-14 16:49:02 +08:00
|
|
|
``/usr/local/django-apps/weblog/`` to your ``PythonPath``. Remember: the
|
2007-08-15 20:25:21 +08:00
|
|
|
**parent directories** of anything you import directly must be on the Python
|
|
|
|
path.
|
|
|
|
|
2007-10-20 11:21:51 +08:00
|
|
|
.. note::
|
2007-03-31 17:40:27 +08:00
|
|
|
|
2007-11-30 13:20:27 +08:00
|
|
|
If you're using Windows, we still recommended that you use forward
|
|
|
|
slashes in the pathnames, even though Windows normally uses the backslash
|
|
|
|
character as its native separator. Apache knows how to convert from the
|
|
|
|
forward slash format to the native format, so this approach is portable and
|
|
|
|
easier to read. (It avoids tricky problems with having to double-escape
|
|
|
|
backslashes.)
|
2007-03-31 17:40:27 +08:00
|
|
|
|
2007-10-20 11:21:51 +08:00
|
|
|
This is valid even on a Windows system::
|
2007-03-31 17:40:27 +08:00
|
|
|
|
2007-10-20 11:21:51 +08:00
|
|
|
PythonPath "['c:/path/to/project'] + sys.path"
|
2007-03-31 17:40:27 +08:00
|
|
|
|
2005-07-20 04:05:52 +08:00
|
|
|
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.
|
|
|
|
|
2008-08-24 06:25:40 +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 03:55:18 +08:00
|
|
|
|
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.
|
|
|
|
|
2010-10-09 13:08:42 +08:00
|
|
|
.. _mod_python documentation: http://modpython.org/live/current/doc-html/directives.html
|
|
|
|
|
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
|
|
|
|
# ...
|
2006-05-02 09:31:56 +08:00
|
|
|
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
|
2005-07-21 23:40:33 +08:00
|
|
|
</VirtualHost>
|
|
|
|
|
|
|
|
<VirtualHost *>
|
2005-10-19 09:09:05 +08:00
|
|
|
ServerName www2.example.com
|
2005-07-21 23:40:33 +08:00
|
|
|
# ...
|
2006-05-02 09:31:56 +08:00
|
|
|
SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings
|
2005-07-21 23:40:33 +08:00
|
|
|
</VirtualHost>
|
|
|
|
|
2008-10-24 17:26:09 +08:00
|
|
|
If you need to put two Django installations within the same ``VirtualHost``
|
|
|
|
(or in different ``VirtualHost`` blocks that share the same server name),
|
2005-07-22 03:42:51 +08:00
|
|
|
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">
|
2006-05-02 09:31:56 +08:00
|
|
|
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
|
|
|
|
PythonInterpreter mysite
|
2005-07-22 01:37:36 +08:00
|
|
|
</Location>
|
2005-07-22 03:42:51 +08:00
|
|
|
|
2005-10-19 09:09:05 +08:00
|
|
|
<Location "/otherthing">
|
2006-05-02 09:31:56 +08:00
|
|
|
SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings
|
2007-10-31 06:50:42 +08:00
|
|
|
PythonInterpreter othersite
|
2005-07-22 01:37:36 +08:00
|
|
|
</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-11-27 21:53:35 +08:00
|
|
|
If you're the type of programmer who debugs using scattered ``print``
|
2010-10-09 13:08:42 +08:00
|
|
|
statements, note that output to ``stdout`` will not appear in the Apache
|
|
|
|
log and can even `cause response errors`_.
|
2005-11-27 21:53:35 +08:00
|
|
|
|
2010-10-09 13:08:42 +08:00
|
|
|
.. _cause response errors: http://blog.dscpl.com.au/2009/04/wsgi-and-printing-to-standard-output.html
|
2005-11-27 21:53:35 +08:00
|
|
|
|
2010-10-09 13:08:42 +08:00
|
|
|
If you have the need to print debugging information in a mod_python setup, you
|
|
|
|
have a few options. You can print to ``stderr`` explicitly, like so::
|
|
|
|
|
|
|
|
print >> sys.stderr, 'debug text'
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
|
|
|
(note that ``stderr`` is buffered, so calling ``flush`` is necessary if you wish
|
|
|
|
debugging information to be displayed promptly.)
|
|
|
|
|
|
|
|
A more compact approach is to use an assertion::
|
2005-11-27 21:53:35 +08:00
|
|
|
|
2010-10-09 13:08:42 +08:00
|
|
|
assert False, 'debug text'
|
|
|
|
|
|
|
|
Another alternative is to add debugging information to the template of your page.
|
2005-07-21 09:48:59 +08:00
|
|
|
|
|
|
|
Serving media files
|
|
|
|
===================
|
|
|
|
|
2005-11-07 22:47:29 +08:00
|
|
|
Django doesn't serve media files itself; it leaves that job to whichever Web
|
|
|
|
server you choose.
|
2005-07-21 09:48:59 +08:00
|
|
|
|
2005-11-07 22:47:29 +08: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 09:48:59 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
* lighttpd_
|
2009-04-01 07:34:03 +08:00
|
|
|
* Nginx_
|
2008-08-24 06:25:40 +08:00
|
|
|
* TUX_
|
|
|
|
* A stripped-down version of Apache_
|
2009-04-01 07:34:03 +08:00
|
|
|
* Cherokee_
|
2005-07-21 09:48:59 +08:00
|
|
|
|
|
|
|
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::
|
|
|
|
|
2007-04-09 09:06:09 +08:00
|
|
|
<Location "/media">
|
2005-07-21 09:48:59 +08:00
|
|
|
SetHandler None
|
|
|
|
</Location>
|
|
|
|
|
2005-11-07 22:39:13 +08: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
|
2006-05-02 09:31:56 +08:00
|
|
|
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
|
2005-11-07 22:39:13 +08:00
|
|
|
</Location>
|
|
|
|
|
2007-04-09 09:06:09 +08:00
|
|
|
<Location "/media">
|
2005-11-07 22:39:13 +08:00
|
|
|
SetHandler None
|
|
|
|
</Location>
|
|
|
|
|
|
|
|
<LocationMatch "\.(jpg|gif|png)$">
|
|
|
|
SetHandler None
|
2005-11-14 00:14:14 +08:00
|
|
|
</LocationMatch>
|
2005-07-21 09:48:59 +08:00
|
|
|
|
2005-10-12 11:52:05 +08:00
|
|
|
|
2005-07-21 09:48:59 +08:00
|
|
|
.. _lighttpd: http://www.lighttpd.net/
|
2009-08-14 00:53:19 +08:00
|
|
|
.. _Nginx: http://wiki.nginx.org/Main
|
2005-07-21 09:48:59 +08:00
|
|
|
.. _TUX: http://en.wikipedia.org/wiki/TUX_web_server
|
|
|
|
.. _Apache: http://httpd.apache.org/
|
2009-04-01 07:34:03 +08:00
|
|
|
.. _Cherokee: http://www.cherokee-project.com/
|
2005-11-07 22:47:29 +08: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.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
The admin files live in (:file:`django/contrib/admin/media`) of the Django
|
2005-11-07 22:47:29 +08:00
|
|
|
distribution.
|
|
|
|
|
|
|
|
Here are two recommended approaches:
|
|
|
|
|
|
|
|
1. Create a symbolic link to the admin media files from within your
|
2008-08-24 06:25:40 +08:00
|
|
|
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.
|
Fixed #10389, #10501, #10502, #10540, #10562, #10563, #10564, #10565, #10568, #10569, #10614, #10617, #10619 -- Fixed several typos as well as a couple minor issues in the docs, patches from timo, nih, bthomas, rduffield, UloPe, and sebleier@gmail.com.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10242 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-31 15:01:01 +08:00
|
|
|
|
2006-05-09 04:39:52 +08:00
|
|
|
2. Or, copy the admin media files so that they live within your Apache
|
|
|
|
document root.
|
2005-11-26 11:17:52 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Using "eggs" with mod_python
|
|
|
|
============================
|
2007-05-27 20:45:14 +08:00
|
|
|
|
|
|
|
If you installed Django from a Python egg_ or are using eggs in your Django
|
|
|
|
project, some extra configuration is required. Create an extra file in your
|
2008-08-24 06:25:40 +08:00
|
|
|
project (or somewhere else) that contains something like the following:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2007-05-27 20:45:14 +08:00
|
|
|
|
|
|
|
import os
|
|
|
|
os.environ['PYTHON_EGG_CACHE'] = '/some/directory'
|
|
|
|
|
2010-10-09 16:12:50 +08:00
|
|
|
Here, ``/some/directory`` is a directory that the Apache Web server process can
|
2007-05-27 20:45:14 +08:00
|
|
|
write to. It will be used as the location for any unpacking of code the eggs
|
|
|
|
need to do.
|
|
|
|
|
|
|
|
Then you have to tell mod_python to import this file before doing anything
|
|
|
|
else. This is done using the PythonImport_ directive to mod_python. You need
|
|
|
|
to ensure that you have specified the ``PythonInterpreter`` directive to
|
|
|
|
mod_python as described above__ (you need to do this even if you aren't
|
|
|
|
serving multiple installations in this case). Then add the ``PythonImport``
|
2007-09-03 01:09:27 +08:00
|
|
|
line in the main server configuration (i.e., outside the ``Location`` or
|
|
|
|
``VirtualHost`` sections). For example::
|
2007-05-27 20:45:14 +08:00
|
|
|
|
|
|
|
PythonInterpreter my_django
|
|
|
|
PythonImport /path/to/my/project/file.py my_django
|
|
|
|
|
|
|
|
Note that you can use an absolute path here (or a normal dotted import path),
|
|
|
|
as described in the `mod_python manual`_. We use an absolute path in the
|
|
|
|
above example because if any Python path modifications are required to access
|
|
|
|
your project, they will not have been done at the time the ``PythonImport``
|
|
|
|
line is processed.
|
|
|
|
|
|
|
|
.. _Egg: http://peak.telecommunity.com/DevCenter/PythonEggs
|
|
|
|
.. _PythonImport: http://www.modpython.org/live/current/doc-html/dir-other-pimp.html
|
|
|
|
.. _mod_python manual: PythonImport_
|
|
|
|
__ `Multiple Django installations on the same Apache`_
|
|
|
|
|
2005-11-26 11:17:52 +08:00
|
|
|
Error handling
|
|
|
|
==============
|
|
|
|
|
|
|
|
When you use Apache/mod_python, errors will be caught by Django -- in other
|
2006-04-12 21:35:31 +08:00
|
|
|
words, they won't propagate to the Apache level and won't appear in the Apache
|
2005-11-26 11:17:52 +08:00
|
|
|
``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.)
|
2006-02-16 11:55:48 +08:00
|
|
|
|
|
|
|
If you get a segmentation fault
|
|
|
|
===============================
|
|
|
|
|
2006-02-17 00:00:32 +08:00
|
|
|
If Apache causes a segmentation fault, there are two probable causes, neither
|
|
|
|
of which has to do with Django itself.
|
|
|
|
|
|
|
|
1. It may be because your Python code is importing the "pyexpat" module,
|
|
|
|
which may conflict with the version embedded in Apache. For full
|
|
|
|
information, see `Expat Causing Apache Crash`_.
|
Fixed #10389, #10501, #10502, #10540, #10562, #10563, #10564, #10565, #10568, #10569, #10614, #10617, #10619 -- Fixed several typos as well as a couple minor issues in the docs, patches from timo, nih, bthomas, rduffield, UloPe, and sebleier@gmail.com.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10242 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-31 15:01:01 +08:00
|
|
|
|
2006-02-17 00:00:32 +08:00
|
|
|
2. It may be because you're running mod_python and mod_php in the same
|
|
|
|
Apache instance, with MySQL as your database backend. In some cases,
|
|
|
|
this causes a known mod_python issue due to version conflicts in PHP and
|
|
|
|
the Python MySQL backend. There's full information in the
|
|
|
|
`mod_python FAQ entry`_.
|
|
|
|
|
|
|
|
If you continue to have problems setting up mod_python, a good thing to do is
|
|
|
|
get a barebones mod_python site working, without the Django framework. This is
|
|
|
|
an easy way to isolate mod_python-specific problems. `Getting mod_python Working`_
|
|
|
|
details this procedure.
|
|
|
|
|
|
|
|
The next step should be to edit your test code and add an import of any
|
2006-02-17 00:01:59 +08:00
|
|
|
Django-specific code you're using -- your views, your models, your URLconf,
|
|
|
|
your RSS configuration, etc. Put these imports in your test handler function
|
|
|
|
and access your test URL in a browser. If this causes a crash, you've confirmed
|
|
|
|
it's the importing of Django code that causes the problem. Gradually reduce the
|
|
|
|
set of imports until it stops crashing, so as to find the specific module that
|
|
|
|
causes the problem. Drop down further into modules and look into their imports,
|
|
|
|
as necessary.
|
2006-02-17 00:00:32 +08:00
|
|
|
|
2009-12-26 14:37:26 +08:00
|
|
|
.. _Expat Causing Apache Crash: http://www.dscpl.com.au/wiki/ModPython/Articles/ExpatCausingApacheCrash
|
2006-02-16 11:55:48 +08:00
|
|
|
.. _mod_python FAQ entry: http://modpython.org/FAQ/faqw.py?req=show&file=faq02.013.htp
|
2009-12-26 14:37:26 +08:00
|
|
|
.. _Getting mod_python Working: http://www.dscpl.com.au/wiki/ModPython/Articles/GettingModPythonWorking
|
2009-07-03 15:16:25 +08:00
|
|
|
|
|
|
|
If you get a UnicodeEncodeError
|
|
|
|
===============================
|
|
|
|
|
|
|
|
If you're taking advantage of the internationalization features of Django (see
|
2010-08-20 03:27:44 +08:00
|
|
|
:doc:`/topics/i18n/index`) and you intend to allow users to upload files, you must
|
2009-07-03 15:16:25 +08:00
|
|
|
ensure that the environment used to start Apache is configured to accept
|
|
|
|
non-ASCII file names. If your environment is not correctly configured, you
|
|
|
|
will trigger ``UnicodeEncodeError`` exceptions when calling functions like
|
|
|
|
``os.path()`` on filenames that contain non-ASCII characters.
|
|
|
|
|
|
|
|
To avoid these problems, the environment used to start Apache should contain
|
|
|
|
settings analogous to the following::
|
|
|
|
|
|
|
|
export LANG='en_US.UTF-8'
|
|
|
|
export LC_ALL='en_US.UTF-8'
|
|
|
|
|
|
|
|
Consult the documentation for your operating system for the appropriate syntax
|
|
|
|
and location to put these configuration items; ``/etc/apache2/envvars`` is a
|
|
|
|
common location on Unix platforms. Once you have added these statements
|
|
|
|
to your environment, restart Apache.
|