{"title":"如何使用 WSGI 进行部署","version":"4.2","locale":"zh-hans","docname":"howto/deployment/wsgi/index","url":"/zh-hans/4.2/howto/deployment/wsgi/","canonical":"https://djangodocs.dev/zh-hans/4.2/howto/deployment/wsgi/","summary":"Django 的主要部署平台是 WSGI ，它是 Web 服务器和 Web 应用的 Python 标准。 Django 的管理命令 startproject 生成了一个最小化的默认 WSGI 配置，你可以按照自己项目的需要去调整这个配置，任何兼容 WSGI 的应用程序服务器都可以直接使用。 Django 提供了下面这些…","html":"<h1>如何使用 WSGI 进行部署<a class=\"heading-anchor\" href=\"#how-to-deploy-with-wsgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django 的主要部署平台是 <a class=\"reference external\" href=\"https://wsgi.readthedocs.io/en/latest/\">WSGI</a>，它是 Web 服务器和 Web 应用的 Python 标准。</p>\n<p>Django 的管理命令 <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startproject</span></code></a> 生成了一个最小化的默认 WSGI 配置，你可以按照自己项目的需要去调整这个配置，任何兼容 WSGI 的应用程序服务器都可以直接使用。</p>\n<p>Django 提供了下面这些 WSGI 服务的入门文档：</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/4.2/howto/deployment/wsgi/gunicorn/\">如何使用 Gunicorn 托管 Django</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/4.2/howto/deployment/wsgi/uwsgi/\">如何用 uWSGI 托管 Django</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/4.2/howto/deployment/wsgi/modwsgi/\">如何使用 Apache 和 <code class=\"docutils literal notranslate\"><span class=\"pre\">mod_wsgi</span></code> 托管 Django</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/4.2/howto/deployment/wsgi/apache-auth/\">如何从 Apache 对 Django 的用户数据库进行认证</a></li>\n</ul>\n</div>\n<section id=\"the-application-object\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">application</span></code> 对象<a class=\"heading-anchor\" href=\"#the-application-object\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>用 WSGI 部署的关键是 <code class=\"docutils literal notranslate\"><span class=\"pre\">application</span></code> callable，应用服务器用它与你的代码交互。 <code class=\"docutils literal notranslate\"><span class=\"pre\">application</span></code> callable 一般以一个位于 Python 模块中，名为 <code class=\"docutils literal notranslate\"><span class=\"pre\">application</span></code> 的对象的形式提供，且对服务器可见。</p>\n<p><a class=\"reference internal\" href=\"/zh-hans/4.2/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startproject</span></code></a> 命令创建了文件 <code class=\"file docutils literal notranslate\"><span class=\"pre\">&lt;project_name&gt;/wsgi.py</span></code>，其中包含了 <code class=\"docutils literal notranslate\"><span class=\"pre\">application</span></code> callable。</p>\n<p>Django 开发服务器和生产环境的 WSGI 部署都用到了它。</p>\n<p>WSGI 服务器从其配置中获取 <code class=\"docutils literal notranslate\"><span class=\"pre\">application</span></code> callable 的路径。Django 的默认服务器（ <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a> 命令），从配置项 <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/settings/#std-setting-WSGI_APPLICATION\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">WSGI_APPLICATION</span></code></a> 中获取。默认值是 <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;project_name&gt;.wsgi.application</span></code>，指向 <code class=\"file docutils literal notranslate\"><span class=\"pre\">&lt;project_name&gt;/wsgi.py</span></code> 中的 <code class=\"docutils literal notranslate\"><span class=\"pre\">application</span></code> callable。</p>\n</section>\n<section id=\"configuring-the-settings-module\">\n<h2>配置 settings 模块<a class=\"heading-anchor\" href=\"#configuring-the-settings-module\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>当 WSGI 服务器加载应用时，Django 需要导入配置模块——完整定义应用的地方。</p>\n<p>Django 利用 <span class=\"target\" id=\"index-3\"></span><a class=\"reference internal\" href=\"/zh-hans/4.2/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\"><span class=\"pre\">DJANGO_SETTINGS_MODULE</span></code></a> 环境变量来定位合适的配置模块。它必须包含到配置模块的点式路径。开发环境和生产环境可以配置不同值；这都取决于你是如何组织配置的。</p>\n<p>若未设置该变量， <code class=\"file docutils literal notranslate\"><span class=\"pre\">wsgi.py</span></code> 默认将其设置为 <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite.settings</span></code>， <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite</span></code> 即工程名字。这就是 <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a> 默认的发现默认配置行为。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>由于环境变量是进程级的，所以如果在同一进程运行多个 Django 站点将出错。这在使用 mod_wsgi 时会出现。</p>\n<p>要避免此问题，为每个站点在后台进程使用 mod_wsgi 的后台模式，或者在 <code class=\"file docutils literal notranslate\"><span class=\"pre\">wsgi.py</span></code> 中通过 <code class=\"docutils literal notranslate\"><span class=\"pre\">os.environ[&quot;DJANGO_SETTINGS_MODULE&quot;]</span> <span class=\"pre\">=</span> <span class=\"pre\">&quot;mysite.settings&quot;</span></code> 重写来自环境变量的值。</p>\n</aside>\n</section>\n<section id=\"applying-wsgi-middleware\">\n<h2>应用 WSGI 中间件<a class=\"heading-anchor\" href=\"#applying-wsgi-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>要应用 WSGI 中间件 &lt;3333#middleware-components-that-play-both-sides&gt; ` 中间件组件，你只需简单包裹应用程序对象。举个例子，你可以在文件 :file:`wsgi.py 末尾添加以下代码:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">helloworld.wsgi</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HelloWorldApplication</span>\n\n<span class=\"n\">application</span> <span class=\"o\">=</span> <span class=\"n\">HelloWorldApplication</span><span class=\"p\">(</span><span class=\"n\">application</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>如果你想将 Django 应用于一个 WSGI 应用或其它框架联合起来，可以用自定义 WSGI 应用替换 Django 的 WSGI 应用，前者会在稍晚时候将任务委托给 WSGI 应用。</p>\n</section>","rootId":"how-to-deploy-with-wsgi","toc":[{"title":"application 对象","anchor":"the-application-object","children":[]},{"title":"配置 settings 模块","anchor":"configuring-the-settings-module","children":[]},{"title":"应用 WSGI 中间件","anchor":"applying-wsgi-middleware","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"操作指南","url":"/zh-hans/4.2/howto/"},{"docname":"howto/deployment/index","title":"如何部署 Django","url":"/zh-hans/4.2/howto/deployment/"}],"prev":{"docname":"howto/deployment/index","title":"如何部署 Django","url":"/zh-hans/4.2/howto/deployment/"},"next":{"docname":"howto/deployment/wsgi/gunicorn","title":"如何使用 Gunicorn 托管 Django","url":"/zh-hans/4.2/howto/deployment/wsgi/gunicorn/"},"formats":{"html":"/zh-hans/4.2/howto/deployment/wsgi/","markdown":"/zh-hans/4.2/howto/deployment/wsgi.md","json":"/zh-hans/4.2/howto/deployment/wsgi.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/howto/deployment/wsgi/index.txt","official":"https://docs.djangoproject.com/zh-hans/4.2/howto/deployment/wsgi/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}