index.txt 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. =======================
  2. How to deploy with WSGI
  3. =======================
  4. Django's primary deployment platform is WSGI_, the Python standard for web
  5. servers and applications.
  6. .. _WSGI: http://www.wsgi.org
  7. Django's :djadmin:`startproject` management command sets up a simple default
  8. WSGI configuration for you, which you can tweak as needed for your project,
  9. and direct any WSGI-compliant application server to use.
  10. Django includes getting-started documentation for the following WSGI servers:
  11. .. toctree::
  12. :maxdepth: 1
  13. modwsgi
  14. apache-auth
  15. gunicorn
  16. uwsgi
  17. The ``application`` object
  18. --------------------------
  19. The key concept of deploying with WSGI is the ``application`` callable which
  20. the application server uses to communicate with your code. It's commonly
  21. provided as an object named ``application`` in a Python module accessible to
  22. the server.
  23. The :djadmin:`startproject` command creates a file
  24. :file:`<project_name>/wsgi.py` that contains such an ``application`` callable.
  25. It's used both by Django's development server and in production WSGI
  26. deployments.
  27. WSGI servers obtain the path to the ``application`` callable from their
  28. configuration. Django's built-in server, namely the :djadmin:`runserver`
  29. command, read it from the :setting:`WSGI_APPLICATION` setting. By default, it's
  30. set to ``<project_name>.wsgi.application``, which points to the ``application``
  31. callable in :file:`<project_name>/wsgi.py`.
  32. Configuring the settings module
  33. -------------------------------
  34. When the WSGI server loads your application, Django needs to import the
  35. settings module — that's where your entire application is defined.
  36. Django uses the :envvar:`DJANGO_SETTINGS_MODULE` environment variable to
  37. locate the appropriate settings module. It must contain the dotted path to the
  38. settings module. You can use a different value for development and production;
  39. it all depends on how you organize your settings.
  40. If this variable isn't set, the default :file:`wsgi.py` sets it to
  41. ``mysite.settings``, where ``mysite`` is the name of your project. That's how
  42. :djadmin:`runserver` discovers the default settings file by default.
  43. .. note::
  44. Since environment variables are process-wide, this doesn't work when you
  45. run multiple Django sites in the same process. This happens with mod_wsgi.
  46. To avoid this problem, use mod_wsgi's daemon mode with each site in its
  47. own daemon process, or override the value from the environment by
  48. enforcing ``os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"`` in
  49. your :file:`wsgi.py`.
  50. Applying WSGI middleware
  51. ------------------------
  52. To apply `WSGI middleware`_ you can simply wrap the application object. For
  53. instance you could add these lines at the bottom of :file:`wsgi.py`::
  54. from helloworld.wsgi import HelloWorldApplication
  55. application = HelloWorldApplication(application)
  56. You could also replace the Django WSGI application with a custom WSGI
  57. application that later delegates to the Django WSGI application, if you want
  58. to combine a Django application with a WSGI application of another framework.
  59. .. _`WSGI middleware`: https://www.python.org/dev/peps/pep-3333/#middleware-components-that-play-both-sides
  60. .. note::
  61. Some third-party WSGI middleware do not call ``close`` on the response
  62. object after handling a request — most notably Sentry's error reporting
  63. middleware up to version 2.0.7. In those cases the
  64. :data:`~django.core.signals.request_finished` signal isn't sent. This can
  65. result in idle connections to database and memcache servers.