index.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 servers, namely the :djadmin:`runserver` and
  29. :djadmin:`runfcgi` commands, read it from the :setting:`WSGI_APPLICATION`
  30. setting. By default, it's set to ``<project_name>.wsgi.application``, which
  31. points to the ``application`` 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 environnemnt 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. istance 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`: http://www.python.org/dev/peps/pep-3333/#middleware-components-that-play-both-sides
  60. Upgrading from Django < 1.4
  61. ---------------------------
  62. If you're upgrading from Django 1.3.x or earlier, you don't have a
  63. :file:`wsgi.py` file in your project.
  64. You can simply add one to your project's top-level Python package (probably
  65. next to :file:`settings.py` and :file:`urls.py`) with the contents below::
  66. import os
  67. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
  68. from django.core.wsgi import get_wsgi_application
  69. application = get_wsgi_application()
  70. The ``os.environ.setdefault`` line just sets the default settings module to
  71. use, if you haven't explicitly set the :envvar:`DJANGO_SETTINGS_MODULE`
  72. environment variable. You'll need to edit this line to replace ``mysite`` with
  73. the name of your project package, so the path to your settings module is
  74. correct.
  75. Also add ``WSGI_APPLICATION = "mysite.wsgi.application"`` in your settings, so
  76. that :djadmin:`runserver` finds your ``application`` callable. Don't forget to
  77. replace ``mysite`` with the name of your project in this line.