如何使用 WSGI 进行部署

    Django 的管理命令 生成了一个简单的默认 WSGI 配置,你可以按照自己项目的需要去调整这个配置,任何兼容 WSGI 的应用服务器都可以直接使用。

    Django 提供了下面这些 WSGI 服务的入门文档:

    The key concept of deploying with WSGI is the application callable whichthe application server uses to communicate with your code. It's commonlyprovided as an object named application in a Python module accessible tothe server.

    The command creates a file<project_name>/wsgi.py that contains such an application callable.

    WSGI servers obtain the path to the callable from theirconfiguration. Django's built-in server, namely the runservercommand, reads it from the setting. By default, it'sset to <project_name>.wsgi.application, which points to the applicationcallable in <project_name>/wsgi.py.

    When the WSGI server loads your application, Django needs to import thesettings module — that's where your entire application is defined.

    Django uses the DJANGO_SETTINGS_MODULE environment variable tolocate the appropriate settings module. It must contain the dotted path to thesettings module. You can use a different value for development and production;it all depends on how you organize your settings.

    If this variable isn't set, the default sets it tomysite.settings, where mysite is the name of your project. That's how discovers the default settings file by default.

    Since environment variables are process-wide, this doesn't work when yourun multiple Django sites in the same process. This happens with mod_wsgi.

    To avoid this problem, use mod_wsgi's daemon mode with each site in itsown daemon process, or override the value from the environment byenforcing os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings" inyour wsgi.py.

    To apply WSGI middleware you can simply wrap the application object. Forinstance you could add these lines at the bottom of wsgi.py:

    You could also replace the Django WSGI application with a custom WSGIapplication that later delegates to the Django WSGI application, if you wantto combine a Django application with a WSGI application of another framework.