{"title":"WSGI とともにデプロイするには","version":"1.10","locale":"ja","docname":"howto/deployment/wsgi/index","url":"/ja/1.10/howto/deployment/wsgi/","canonical":"https://djangodocs.dev/ja/1.10/howto/deployment/wsgi/","summary":"Django の主要なデプロイプラットフォームは、 Web サーバと Web アプリケーションに関して Python の標準である WSGI です。","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 の主要なデプロイプラットフォームは、 Web サーバと Web アプリケーション に関して Python の標準である <a class=\"reference external\" href=\"http://www.wsgi.org\">WSGI</a> です。</p>\n<p>Django の <a class=\"reference internal\" href=\"/ja/1.10/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a> 管理用コマンドはシンプルなデフォルトの WSGI 設定をセットアップします。必要に応じて、あなたのプロジェクトと WSGI 準拠の Web サーバに合わせて微調整することができます。</p>\n<p>Django には以下の WSGI サーバのために、手引きとなるドキュメントが用意されています:</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/ja/1.10/howto/deployment/wsgi/modwsgi/\">Django を Apache と <code class=\"docutils literal notranslate\">mod_wsgi</code> とともに使うには？</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/ja/1.10/howto/deployment/wsgi/apache-auth/\">Django のユーザーデータベースに対する Apache からの認証</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/ja/1.10/howto/deployment/wsgi/gunicorn/\">How to use Django with Gunicorn</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/ja/1.10/howto/deployment/wsgi/uwsgi/\">How to use Django with uWSGI</a></li>\n</ul>\n</div>\n<section id=\"the-application-object\">\n<h2><code class=\"docutils literal notranslate\">application</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 デプロイでキーとなる概念は、Web サーバがあなたのコードと通信するために使う <code class=\"docutils literal notranslate\">application</code> という呼び出し可能オブジェクトです。これは一般的に、サーバにアクセスできる Python モジュールの中で <code class=\"docutils literal notranslate\">application</code> という名前のオブジェクトとして提供されています。</p>\n<p><a class=\"reference internal\" href=\"/ja/1.10/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a> コマンドは、この <code class=\"docutils literal notranslate\">application</code> 呼び出し可能オブジェクトを含む <code class=\"file docutils literal notranslate\">&lt;project_name&gt;/wsgi.py</code> ファイルを生成します。</p>\n<p>これは、Django のデプロイサーバーによって、また WSGI デプロイプロダクションにおいて、これら両方で使われます。</p>\n<p>WSGI サーバーは <code class=\"docutils literal notranslate\">application</code> 呼び出し可能オブジェクトへのパスを、その設定から取得します。Django のビルトインサーバー (要するに <a class=\"reference internal\" href=\"/ja/1.10/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\">runserver</code></a> コマンド) は、<a class=\"reference internal\" href=\"/ja/1.10/ref/settings/#std-setting-WSGI_APPLICATION\"><code class=\"xref std std-setting docutils literal notranslate\">WSGI_APPLICATION</code></a> の設定からこれを読み出します。デフォルトでは、 <code class=\"file docutils literal notranslate\">&lt;project_name&gt;/wsgi.py</code> 内の <code class=\"docutils literal notranslate\">application</code> 呼び出し可能オブジェクトを指す <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>&lt;project_name&gt;.wsgi.application` がセットされています。</p>\n</section>\n<section id=\"configuring-the-settings-module\">\n<h2>設定モジュールを設定する<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 uses the <span class=\"target\" id=\"index-0\"></span><a class=\"reference internal\" href=\"/ja/1.10/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\">DJANGO_SETTINGS_MODULE</code></a> environment variable to\nlocate the appropriate settings module. It must contain the dotted path to the\nsettings module. You can use a different value for development and production;\nit all depends on how you organize your settings.</p>\n<p>If this variable isn't set, the default <code class=\"file docutils literal notranslate\">wsgi.py</code> sets it to\n<code class=\"docutils literal notranslate\">mysite.settings</code>, where <code class=\"docutils literal notranslate\">mysite</code> is the name of your project. That's how\n<a class=\"reference internal\" href=\"/ja/1.10/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\">runserver</code></a> discovers the default settings file by default.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>Since environment variables are process-wide, this doesn't work when you\nrun multiple Django sites in the same process. This happens with mod_wsgi.</p>\n<p>To avoid this problem, use mod_wsgi's daemon mode with each site in its\nown daemon process, or override the value from the environment by\nenforcing <code class=\"docutils literal notranslate\">os.environ[&quot;DJANGO_SETTINGS_MODULE&quot;] = &quot;mysite.settings&quot;</code> in\nyour <code class=\"file docutils literal notranslate\">wsgi.py</code>.</p>\n</aside>\n</section>\n<section id=\"applying-wsgi-middleware\">\n<h2>Applying WSGI middleware<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>To apply <a class=\"reference external\" href=\"https://www.python.org/dev/peps/pep-3333/#middleware-components-that-play-both-sides\">WSGI middleware</a> you can simply wrap the application object. For\ninstance you could add these lines at the bottom of <code class=\"file docutils literal notranslate\">wsgi.py</code>:</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=\"nn\">helloworld.wsgi</span> <span class=\"kn\">import</span> HelloWorldApplication\napplication <span class=\"o\">=</span> HelloWorldApplication<span class=\"p\">(</span>application<span class=\"p\">)</span>\n</code></pre></div>\n<p>You could also replace the Django WSGI application with a custom WSGI\napplication that later delegates to the Django WSGI application, if you want\nto combine a Django application with a WSGI application of another framework.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>Some third-party WSGI middleware do not call <code class=\"docutils literal notranslate\">close</code> on the response\nobject after handling a request. In those cases the\n<a class=\"reference internal\" href=\"/ja/1.10/ref/signals/#django.core.signals.request_finished\" title=\"django.core.signals.request_finished\"><code class=\"xref py py-data docutils literal notranslate\">request_finished</code></a> signal isn't sent. This can\nresult in idle connections to database and memcache servers.</p>\n</aside>\n</section>","rootId":"how-to-deploy-with-wsgi","toc":[{"title":"application オブジェクト","anchor":"the-application-object","children":[]},{"title":"設定モジュールを設定する","anchor":"configuring-the-settings-module","children":[]},{"title":"Applying WSGI middleware","anchor":"applying-wsgi-middleware","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"「How-to」ガイド","url":"/ja/1.10/howto/"},{"docname":"howto/deployment/index","title":"Django をデプロイする","url":"/ja/1.10/howto/deployment/"}],"prev":{"docname":"howto/deployment/index","title":"Django をデプロイする","url":"/ja/1.10/howto/deployment/"},"next":{"docname":"howto/deployment/wsgi/modwsgi","title":"Django を Apache と mod_wsgi とともに使うには？","url":"/ja/1.10/howto/deployment/wsgi/modwsgi/"},"formats":{"html":"/ja/1.10/howto/deployment/wsgi/","markdown":"/ja/1.10/howto/deployment/wsgi.md","json":"/ja/1.10/howto/deployment/wsgi.json"},"source":"https://github.com/django/django/blob/stable/1.10.x/docs/howto/deployment/wsgi/index.txt","official":"https://docs.djangoproject.com/ja/1.10/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","1.11","1.10","1.9"],"inLocales":["en","fr","ja","id","pt-br","es","el","pl"]}