Django

Code

root/django/branches/full-history/docs/modpython.txt

Revision 4869, 9.3 kB (checked in by utrebec, 2 years ago)

[full-history]
svnmerge: merging in revision(s) 4816,4818-4823,4825-4835,4837,4844-4850,4856-4868 from "http://code.djangoproject.com/svn/django/trunk"

Line 
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`_.
17
18 .. _Apache: http://httpd.apache.org/
19 .. _mod_python: http://www.modpython.org/
20 .. _mod_perl: http://perl.apache.org/
21 .. _prefork MPM: http://httpd.apache.org/docs/2.2/mod/prefork.html
22 .. _worker MPM: http://httpd.apache.org/docs/2.2/mod/worker.html
23 .. _How to use Django with FastCGI: ../fastcgi/
24
25 Basic configuration
26 ===================
27
28 To configure Django with mod_python, first make sure you have Apache installed,
29 with the mod_python module activated.
30
31 Then edit your ``httpd.conf`` file and add the following::
32
33     <Location "/mysite/">
34         SetHandler python-program
35         PythonHandler django.core.handlers.modpython
36         SetEnv DJANGO_SETTINGS_MODULE mysite.settings
37         PythonDebug On
38     </Location>
39
40 ...and replace ``mysite.settings`` with the Python import path to your Django
41 project's settings file.
42
43 This tells Apache: "Use mod_python for any URL at or under '/mysite/', using the
44 Django mod_python handler." It passes the value of ``DJANGO_SETTINGS_MODULE``
45 so mod_python knows which settings to use.
46
47 Note that we're using the ``<Location>`` directive, not the ``<Directory>``
48 directive. The latter is used for pointing at places on your filesystem,
49 whereas ``<Location>`` points at places in the URL structure of a Web site.
50 ``<Directory>`` would be meaningless here.
51
52 Also, if you've manually altered your ``PYTHONPATH`` to put your Django project
53 on it, you'll need to tell mod_python::
54
55     PythonPath "['/path/to/project'] + sys.path"
56
57 You can also add directives such as ``PythonAutoReload Off`` for performance.
58 See the `mod_python documentation`_ for a full list of options.
59
60 Note that you should set ``PythonDebug Off`` on a production server. If you
61 leave ``PythonDebug On``, your users would see ugly (and revealing) Python
62 tracebacks if something goes wrong within mod_python.
63
64 Restart Apache, and any request to /mysite/ or below will be served by Django.
65 Note that Django's URLconfs won't trim the "/mysite/" -- they get passed the
66 full URL.
67
68 When deploying Django sites on mod_python, you'll need to restart Apache each
69 time you make changes to your Python code.
70
71 Multiple Django installations on the same Apache
72 ================================================
73
74 It's entirely possible to run multiple Django installations on the same Apache
75 instance. Just use ``VirtualHost`` for that, like so::
76
77     NameVirtualHost *
78
79     <VirtualHost *>
80         ServerName www.example.com
81         # ...
82         SetEnv DJANGO_SETTINGS_MODULE mysite.settings
83     </VirtualHost>
84
85     <VirtualHost *>
86         ServerName www2.example.com
87         # ...
88         SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings
89     </VirtualHost>
90
91 If you need to put two Django installations within the same ``VirtualHost``,
92 you'll need to take a special precaution to ensure mod_python's cache doesn't
93 mess things up. Use the ``PythonInterpreter`` directive to give different
94 ``<Location>`` directives separate interpreters::
95
96     <VirtualHost *>
97         ServerName www.example.com
98         # ...
99         <Location "/something">
100             SetEnv DJANGO_SETTINGS_MODULE mysite.settings
101             PythonInterpreter mysite
102         </Location>
103
104         <Location "/otherthing">
105             SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings
106             PythonInterpreter mysite_other
107         </Location>
108     </VirtualHost>
109
110 The values of ``PythonInterpreter`` don't really matter, as long as they're
111 different between the two ``Location`` blocks.
112
113 Running a development server with mod_python
114 ============================================
115
116 If you use mod_python for your development server, you can avoid the hassle of
117 having to restart the server each time you make code changes. Just set
118 ``MaxRequestsPerChild 1`` in your ``httpd.conf`` file to force Apache to reload
119 everything for each request. But don't do that on a production server, or we'll
120 revoke your Django privileges.
121
122 If you're the type of programmer who debugs using scattered ``print``
123 statements, note that ``print`` statements have no effect in mod_python; they
124 don't appear in the Apache log, as one might expect. If you have the need to
125 print debugging information in a mod_python setup, either do this::
126
127     assert False, the_value_i_want_to_see
128
129 Or add the debugging information to the template of your page.
130
131 .. _mod_python documentation: http://modpython.org/live/current/doc-html/directives.html
132
133 Serving media files
134 ===================
135
136 Django doesn't serve media files itself; it leaves that job to whichever Web
137 server you choose.
138
139 We recommend using a separate Web server -- i.e., one that's not also running
140 Django -- for serving media. Here are some good choices:
141
142 * lighttpd_
143 * TUX_
144 * A stripped-down version of Apache_
145
146 If, however, you have no option but to serve media files on the same Apache
147 ``VirtualHost`` as Django, here's how you can turn off mod_python for a
148 particular part of the site::
149
150     <Location "/media/">
151         SetHandler None
152     </Location>
153
154 Just change ``Location`` to the root URL of your media files. You can also use
155 ``<LocationMatch>`` to match a regular expression.
156
157 This example sets up Django at the site root but explicitly disables Django for
158 the ``media`` subdirectory and any URL that ends with ``.jpg``, ``.gif`` or
159 ``.png``::
160
161     <Location "/">
162         SetHandler python-program
163         PythonHandler django.core.handlers.modpython
164         SetEnv DJANGO_SETTINGS_MODULE mysite.settings
165     </Location>
166
167     <Location "media">
168         SetHandler None
169     </Location>
170
171     <LocationMatch "\.(jpg|gif|png)$">
172         SetHandler None
173     </LocationMatch>
174
175
176 .. _lighttpd: http://www.lighttpd.net/
177 .. _TUX: http://en.wikipedia.org/wiki/TUX_web_server
178 .. _Apache: http://httpd.apache.org/
179
180 Serving the admin files
181 =======================
182
183 Note that the Django development server automagically serves admin media files,
184 but this is not the case when you use any other server arrangement. You're
185 responsible for setting up Apache, or whichever media server you're using, to
186 serve the admin files.
187
188 The admin files live in (``django/contrib/admin/media``) of the Django
189 distribution.
190
191 Here are two recommended approaches:
192
193     1. Create a symbolic link to the admin media files from within your
194        document root. This way, all of your Django-related files -- code
195        **and** templates -- stay in one place, and you'll still be able to
196        ``svn update`` your code to get the latest admin templates, if they
197        change.
198     2. Or, copy the admin media files so that they live within your Apache
199        document root.
200
201 Error handling
202 ==============
203
204 When you use Apache/mod_python, errors will be caught by Django -- in other
205 words, they won't propagate to the Apache level and won't appear in the Apache
206 ``error_log``.
207
208 The exception for this is if something is really wonky in your Django setup. In
209 that case, you'll see an "Internal Server Error" page in your browser and the
210 full Python traceback in your Apache ``error_log`` file. The ``error_log``
211 traceback is spread over multiple lines. (Yes, this is ugly and rather hard to
212 read, but it's how mod_python does things.)
213
214 If you get a segmentation fault
215 ===============================
216
217 If Apache causes a segmentation fault, there are two probable causes, neither
218 of which has to do with Django itself.
219
220     1. It may be because your Python code is importing the "pyexpat" module,
221        which may conflict with the version embedded in Apache. For full
222        information, see `Expat Causing Apache Crash`_.
223     2. It may be because you're running mod_python and mod_php in the same
224        Apache instance, with MySQL as your database backend. In some cases,
225        this causes a known mod_python issue due to version conflicts in PHP and
226        the Python MySQL backend. There's full information in the
227        `mod_python FAQ entry`_.
228
229 If you continue to have problems setting up mod_python, a good thing to do is
230 get a barebones mod_python site working, without the Django framework. This is
231 an easy way to isolate mod_python-specific problems. `Getting mod_python Working`_
232 details this procedure.
233
234 The next step should be to edit your test code and add an import of any
235 Django-specific code you're using -- your views, your models, your URLconf,
236 your RSS configuration, etc. Put these imports in your test handler function
237 and access your test URL in a browser. If this causes a crash, you've confirmed
238 it's the importing of Django code that causes the problem. Gradually reduce the
239 set of imports until it stops crashing, so as to find the specific module that
240 causes the problem. Drop down further into modules and look into their imports,
241 as necessary.
242
243 .. _Expat Causing Apache Crash: http://www.dscpl.com.au/articles/modpython-006.html
244 .. _mod_python FAQ entry: http://modpython.org/FAQ/faqw.py?req=show&file=faq02.013.htp
245 .. _Getting mod_python Working: http://www.dscpl.com.au/articles/modpython-001.html
Note: See TracBrowser for help on using the browser.