modwsgi.txt 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ==========================================
  2. How to use Django with Apache and mod_wsgi
  3. ==========================================
  4. Deploying Django with Apache_ and `mod_wsgi`_ is a tried and tested way to get
  5. Django into production.
  6. .. _Apache: http://httpd.apache.org/
  7. .. _mod_wsgi: http://code.google.com/p/modwsgi/
  8. mod_wsgi is an Apache module which can host any Python WSGI_ application,
  9. including Django. Django will work with any version of Apache which supports
  10. mod_wsgi.
  11. .. _WSGI: http://www.wsgi.org
  12. The `official mod_wsgi documentation`_ is fantastic; it's your source for all
  13. the details about how to use mod_wsgi. You'll probably want to start with the
  14. `installation and configuration documentation`_.
  15. .. _official mod_wsgi documentation: http://www.modwsgi.org/
  16. .. _installation and configuration documentation: http://www.modwsgi.org/wiki/InstallationInstructions
  17. Basic configuration
  18. ===================
  19. Once you've got mod_wsgi installed and activated, edit your Apache server's
  20. ``httpd.conf`` file and add
  21. .. code-block:: apache
  22. WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
  23. WSGIPythonPath /path/to/mysite.com
  24. <Directory /path/to/mysite.com/mysite>
  25. <Files wsgi.py>
  26. Order deny,allow
  27. Allow from all
  28. </Files>
  29. </Directory>
  30. The first bit in the ``WSGIScriptAlias`` line is the base URL path you want to
  31. serve your application at (``/`` indicates the root url), and the second is the
  32. location of a "WSGI file" -- see below -- on your system, usually inside of
  33. your project package (``mysite`` in this example). This tells Apache to serve
  34. any request below the given URL using the WSGI application defined in that
  35. file.
  36. The ``WSGIPythonPath`` line ensures that your project package is available for
  37. import on the Python path; in other words, that ``import mysite`` works.
  38. The ``<Directory>`` piece just ensures that Apache can access your
  39. :file:`wsgi.py` file.
  40. Next we'll need to ensure this :file:`wsgi.py` with a WSGI application object
  41. exists. As of Django version 1.4, :djadmin:`startproject` will have created one
  42. for you; otherwise, you'll need to create it. See the :doc:`WSGI overview
  43. documentation</howto/deployment/wsgi/index>` for the default contents you
  44. should put in this file, and what else you can add to it.
  45. .. warning::
  46. If multiple Django sites are run in a single mod_wsgi process, all of them
  47. will use the settings of whichever one happens to run first. This can be
  48. solved with a minor edit to ``wsgi.py`` (see comment in the file for
  49. details), or by :ref:`using mod_wsgi daemon mode<daemon-mode>` and ensuring
  50. that each site runs in its own daemon process.
  51. Using a virtualenv
  52. ==================
  53. If you install your project's Python dependencies inside a `virtualenv`_,
  54. you'll need to add the path to this virtualenv's ``site-packages`` directory to
  55. your Python path as well. To do this, add an additional path to your
  56. `WSGIPythonPath` directive with multiple paths separated by a colon::
  57. WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python2.X/site-packages
  58. Make sure you give the correct path to your virtualenv, and replace
  59. ``python2.X`` with the correct Python version (e.g. ``python2.7``).
  60. .. _virtualenv: http://www.virtualenv.org
  61. .. _daemon-mode:
  62. Using mod_wsgi daemon mode
  63. ==========================
  64. "Daemon mode" is the recommended mode for running mod_wsgi (on non-Windows
  65. platforms). See the `official mod_wsgi documentation`_ for details on setting
  66. up daemon mode. The only change required to the above configuration if you use
  67. daemon mode is that you can't use ``WSGIPythonPath``; instead you should use
  68. the ``python-path`` option to ``WSGIDaemonProcess``, for example::
  69. WSGIDaemonProcess example.com python-path=/path/to/mysite.com:/path/to/venv/lib/python2.7/site-packages
  70. .. _serving-files:
  71. Serving files
  72. =============
  73. Django doesn't serve files itself; it leaves that job to whichever Web
  74. server you choose.
  75. We recommend using a separate Web server -- i.e., one that's not also running
  76. Django -- for serving media. Here are some good choices:
  77. * lighttpd_
  78. * Nginx_
  79. * TUX_
  80. * A stripped-down version of Apache_
  81. * Cherokee_
  82. If, however, you have no option but to serve media files on the same Apache
  83. ``VirtualHost`` as Django, you can set up Apache to serve some URLs as
  84. static media, and others using the mod_wsgi interface to Django.
  85. This example sets up Django at the site root, but explicitly serves
  86. ``robots.txt``, ``favicon.ico``, any CSS file, and anything in the
  87. ``/static/`` and ``/media/`` URL space as a static file. All other URLs
  88. will be served using mod_wsgi::
  89. Alias /robots.txt /path/to/mysite.com/static/robots.txt
  90. Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
  91. AliasMatch ^/([^/]*\.css) /path/to/mysite.com/static/styles/$1
  92. Alias /media/ /path/to/mysite.com/media/
  93. Alias /static/ /path/to/mysite.com/static/
  94. <Directory /path/to/mysite.com/static>
  95. Order deny,allow
  96. Allow from all
  97. </Directory>
  98. <Directory /path/to/mysite.com/media>
  99. Order deny,allow
  100. Allow from all
  101. </Directory>
  102. WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
  103. <Directory /path/to/mysite.com/mysite>
  104. <Files wsgi.py>
  105. Order allow,deny
  106. Allow from all
  107. </Files>
  108. </Directory>
  109. .. _lighttpd: http://www.lighttpd.net/
  110. .. _Nginx: http://wiki.nginx.org/Main
  111. .. _TUX: http://en.wikipedia.org/wiki/TUX_web_server
  112. .. _Apache: http://httpd.apache.org/
  113. .. _Cherokee: http://www.cherokee-project.com/
  114. .. More details on configuring a mod_wsgi site to serve static files can be found
  115. .. in the mod_wsgi documentation on `hosting static files`_.
  116. .. _hosting static files: http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#Hosting_Of_Static_Files
  117. .. _serving-the-admin-files:
  118. Serving the admin files
  119. =======================
  120. Note that the Django development server automatically serves the static files
  121. of the admin app (and any other installed apps), but this is not the case when
  122. you use any other server arrangement. You're responsible for setting up Apache,
  123. or whichever media server you're using, to serve the admin files.
  124. The admin files live in (:file:`django/contrib/admin/static/admin`) of the
  125. Django distribution.
  126. We **strongly** recommend using :mod:`django.contrib.staticfiles` to handle the
  127. admin files (along with a Web server as outlined in the previous section; this
  128. means using the :djadmin:`collectstatic` management command to collect the
  129. static files in :setting:`STATIC_ROOT`, and then configuring your Web server to
  130. serve :setting:`STATIC_ROOT` at :setting:`STATIC_URL`), but here are three
  131. other approaches:
  132. 1. Create a symbolic link to the admin static files from within your
  133. document root (this may require ``+FollowSymLinks`` in your Apache
  134. configuration).
  135. 2. Use an ``Alias`` directive, as demonstrated above, to alias the appropriate
  136. URL (probably :setting:`STATIC_URL` + `admin/`) to the actual location of
  137. the admin files.
  138. 3. Copy the admin static files so that they live within your Apache
  139. document root.
  140. Authenticating against Django's user database from Apache
  141. =========================================================
  142. Django provides a handler to allow Apache to authenticate users directly
  143. against Django's authentication backends. See the :doc:`mod_wsgi authentication
  144. documentation </howto/deployment/wsgi/apache-auth>`.
  145. If you get a UnicodeEncodeError
  146. ===============================
  147. If you're taking advantage of the internationalization features of Django (see
  148. :doc:`/topics/i18n/index`) and you intend to allow users to upload files, you must
  149. ensure that the environment used to start Apache is configured to accept
  150. non-ASCII file names. If your environment is not correctly configured, you
  151. will trigger ``UnicodeEncodeError`` exceptions when calling functions like
  152. ``os.path()`` on filenames that contain non-ASCII characters.
  153. To avoid these problems, the environment used to start Apache should contain
  154. settings analogous to the following::
  155. export LANG='en_US.UTF-8'
  156. export LC_ALL='en_US.UTF-8'
  157. Consult the documentation for your operating system for the appropriate syntax
  158. and location to put these configuration items; ``/etc/apache2/envvars`` is a
  159. common location on Unix platforms. Once you have added these statements
  160. to your environment, restart Apache.