modwsgi.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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: https://httpd.apache.org/
  7. .. _mod_wsgi: http://www.modwsgi.org/
  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: https://modwsgi.readthedocs.org/
  16. .. _installation and configuration documentation: https://modwsgi.readthedocs.org/en/develop/installation.html
  17. Basic configuration
  18. ===================
  19. Once you've got mod_wsgi installed and activated, edit your Apache server's
  20. `httpd.conf`_ file (or a `virtual host`_ file) and add the following. If you
  21. are using a version of Apache older than 2.4, replace ``Require all granted``
  22. with ``Allow from all`` and also add the line ``Order deny,allow`` above it.
  23. .. _httpd.conf: https://wiki.apache.org/httpd/DistrosDefaultLayout
  24. .. _virtual host: https://httpd.apache.org/docs/current/en/vhosts/
  25. .. code-block:: apache
  26. WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
  27. WSGIPythonPath /path/to/mysite.com
  28. <Directory /path/to/mysite.com/mysite>
  29. <Files wsgi.py>
  30. Require all granted
  31. </Files>
  32. </Directory>
  33. The first bit in the ``WSGIScriptAlias`` line is the base URL path you want to
  34. serve your application at (``/`` indicates the root url), and the second is the
  35. location of a "WSGI file" -- see below -- on your system, usually inside of
  36. your project package (``mysite`` in this example). This tells Apache to serve
  37. any request below the given URL using the WSGI application defined in that
  38. file.
  39. The ``WSGIPythonPath`` line ensures that your project package is available for
  40. import on the Python path; in other words, that ``import mysite`` works.
  41. The ``<Directory>`` piece just ensures that Apache can access your
  42. :file:`wsgi.py` file.
  43. Next we'll need to ensure this :file:`wsgi.py` with a WSGI application object
  44. exists. As of Django version 1.4, :djadmin:`startproject` will have created one
  45. for you; otherwise, you'll need to create it. See the :doc:`WSGI overview
  46. documentation</howto/deployment/wsgi/index>` for the default contents you
  47. should put in this file, and what else you can add to it.
  48. .. warning::
  49. If multiple Django sites are run in a single mod_wsgi process, all of them
  50. will use the settings of whichever one happens to run first. This can be
  51. solved by changing::
  52. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
  53. in ``wsgi.py``, to::
  54. os.environ["DJANGO_SETTINGS_MODULE"] = "{{ project_name }}.settings"
  55. or by :ref:`using mod_wsgi daemon mode<daemon-mode>` and ensuring that each
  56. site runs in its own daemon process.
  57. .. admonition:: Fixing ``UnicodeEncodeError`` for file uploads
  58. If you get a ``UnicodeEncodeError`` when uploading files with file names
  59. that contain non-ASCII characters, make sure Apache is configured to accept
  60. non-ASCII file names::
  61. export LANG='en_US.UTF-8'
  62. export LC_ALL='en_US.UTF-8'
  63. A common location to put this configuration is ``/etc/apache2/envvars``.
  64. See the :ref:`unicode-files` section of the Unicode reference guide for
  65. details.
  66. Using a ``virtualenv``
  67. ======================
  68. If you install your project's Python dependencies inside a `virtualenv`_,
  69. you'll need to add the path to this virtualenv's ``site-packages`` directory to
  70. your Python path as well. To do this, add an additional path to your
  71. ``WSGIPythonPath`` directive, with multiple paths separated by a colon (``:``)
  72. if using a UNIX-like system, or a semicolon (``;``) if using Windows. If any
  73. part of a directory path contains a space character, the complete argument
  74. string to ``WSGIPythonPath`` must be quoted:
  75. .. code-block:: apache
  76. WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python3.X/site-packages
  77. Make sure you give the correct path to your virtualenv, and replace
  78. ``python3.X`` with the correct Python version (e.g. ``python3.4``).
  79. .. _virtualenv: http://www.virtualenv.org
  80. .. _daemon-mode:
  81. Using ``mod_wsgi`` daemon mode
  82. ==============================
  83. "Daemon mode" is the recommended mode for running mod_wsgi (on non-Windows
  84. platforms). To create the required daemon process group and delegate the
  85. Django instance to run in it, you will need to add appropriate
  86. ``WSGIDaemonProcess`` and ``WSGIProcessGroup`` directives. A further change
  87. required to the above configuration if you use daemon mode is that you can't
  88. use ``WSGIPythonPath``; instead you should use the ``python-path`` option to
  89. ``WSGIDaemonProcess``, for example:
  90. .. code-block:: apache
  91. WSGIDaemonProcess example.com python-path=/path/to/mysite.com:/path/to/venv/lib/python2.7/site-packages
  92. WSGIProcessGroup example.com
  93. If you want to serve your project in a subdirectory
  94. (``https://example.com/mysite`` in this example), you can add ``WSGIScriptAlias``
  95. to the configuration above:
  96. .. code-block:: apache
  97. WSGIScriptAlias /mysite /path/to/mysite.com/mysite/wsgi.py process-group=example.com
  98. See the official mod_wsgi documentation for `details on setting up daemon
  99. mode`_.
  100. .. _details on setting up daemon mode: https://modwsgi.readthedocs.org/en/develop/user-guides/quick-configuration-guide.html#delegation-to-daemon-process
  101. .. _serving-files:
  102. Serving files
  103. =============
  104. Django doesn't serve files itself; it leaves that job to whichever Web
  105. server you choose.
  106. We recommend using a separate Web server -- i.e., one that's not also running
  107. Django -- for serving media. Here are some good choices:
  108. * Nginx_
  109. * A stripped-down version of Apache_
  110. If, however, you have no option but to serve media files on the same Apache
  111. ``VirtualHost`` as Django, you can set up Apache to serve some URLs as
  112. static media, and others using the mod_wsgi interface to Django.
  113. This example sets up Django at the site root, but serves ``robots.txt``,
  114. ``favicon.ico``, and anything in the ``/static/`` and ``/media/`` URL space as
  115. a static file. All other URLs will be served using mod_wsgi:
  116. .. code-block:: apache
  117. Alias /robots.txt /path/to/mysite.com/static/robots.txt
  118. Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
  119. Alias /media/ /path/to/mysite.com/media/
  120. Alias /static/ /path/to/mysite.com/static/
  121. <Directory /path/to/mysite.com/static>
  122. Require all granted
  123. </Directory>
  124. <Directory /path/to/mysite.com/media>
  125. Require all granted
  126. </Directory>
  127. WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
  128. <Directory /path/to/mysite.com/mysite>
  129. <Files wsgi.py>
  130. Require all granted
  131. </Files>
  132. </Directory>
  133. If you are using a version of Apache older than 2.4, replace
  134. ``Require all granted`` with ``Allow from all`` and also add the line
  135. ``Order deny,allow`` above it.
  136. .. _Nginx: http://wiki.nginx.org/Main
  137. .. _Apache: https://httpd.apache.org/
  138. .. More details on configuring a mod_wsgi site to serve static files can be found
  139. .. in the mod_wsgi documentation on `hosting static files`_.
  140. .. _hosting static files: https://modwsgi.readthedocs.org/en/develop/user-guides/configuration-guidelines.html#hosting-of-static-files
  141. .. _serving-the-admin-files:
  142. Serving the admin files
  143. =======================
  144. When :mod:`django.contrib.staticfiles` is in :setting:`INSTALLED_APPS`, the
  145. Django development server automatically serves the static files of the
  146. admin app (and any other installed apps). This is however not the case when you
  147. use any other server arrangement. You're responsible for setting up Apache, or
  148. whichever Web server you're using, to serve the admin files.
  149. The admin files live in (:file:`django/contrib/admin/static/admin`) of the
  150. Django distribution.
  151. We **strongly** recommend using :mod:`django.contrib.staticfiles` to handle the
  152. admin files (along with a Web server as outlined in the previous section; this
  153. means using the :djadmin:`collectstatic` management command to collect the
  154. static files in :setting:`STATIC_ROOT`, and then configuring your Web server to
  155. serve :setting:`STATIC_ROOT` at :setting:`STATIC_URL`), but here are three
  156. other approaches:
  157. 1. Create a symbolic link to the admin static files from within your
  158. document root (this may require ``+FollowSymLinks`` in your Apache
  159. configuration).
  160. 2. Use an ``Alias`` directive, as demonstrated above, to alias the appropriate
  161. URL (probably :setting:`STATIC_URL` + ``admin/``) to the actual location of
  162. the admin files.
  163. 3. Copy the admin static files so that they live within your Apache
  164. document root.
  165. Authenticating against Django's user database from Apache
  166. =========================================================
  167. Django provides a handler to allow Apache to authenticate users directly
  168. against Django's authentication backends. See the :doc:`mod_wsgi authentication
  169. documentation </howto/deployment/wsgi/apache-auth>`.