index.txt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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: https://wsgi.readthedocs.io/en/latest/
  7. Django's :djadmin:`startproject` management command sets up a minimal 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. gunicorn
  14. uwsgi
  15. modwsgi
  16. apache-auth
  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, reads 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 :pep:`WSGI middleware
  53. <3333#middleware-components-that-play-both-sides>` you can wrap the application
  54. object. For instance you could add these lines at the bottom of
  55. :file:`wsgi.py`::
  56. from helloworld.wsgi import HelloWorldApplication
  57. application = HelloWorldApplication(application)
  58. You could also replace the Django WSGI application with a custom WSGI
  59. application that later delegates to the Django WSGI application, if you want
  60. to combine a Django application with a WSGI application of another framework.