2
0

modwsgi.txt 8.5 KB

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