{"title":"Django を Apache と mod_wsgi とともに使うには？","version":"4.2","locale":"ja","docname":"howto/deployment/wsgi/modwsgi","url":"/ja/4.2/howto/deployment/wsgi/modwsgi/","canonical":"https://djangodocs.dev/ja/4.2/howto/deployment/wsgi/modwsgi/","summary":"Apache と mod_wsgi と共にDjangoをデプロイすることは、Djangoを製品とする際に試され、テストされた方法です。","html":"<h1>Django を Apache と <code class=\"docutils literal notranslate\">mod_wsgi</code> とともに使うには？<a class=\"heading-anchor\" href=\"#how-to-use-django-with-apache-and-mod-wsgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p><a class=\"reference external\" href=\"https://httpd.apache.org/\">Apache</a> と <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/\">mod_wsgi</a> と共にDjangoをデプロイすることは、Djangoを製品とする際に試され、テストされた方法です。</p>\n<p>mod_wsgi は、Djangoを含む任意のPythonの W​​SGI_ アプリケーションをホストできるApacheのモジュールです。 Djangoはmod_wsgiをサポートしているApacheのすべてのバージョンで動作します。</p>\n<p>The <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/\">official mod_wsgi documentation</a> is your source for all the details about\nhow to use mod_wsgi. You'll probably want to start with the <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/installation.html\">installation and\nconfiguration documentation</a>.</p>\n<section id=\"basic-configuration\">\n<h2>基本設定<a class=\"heading-anchor\" href=\"#basic-configuration\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Once you've got mod_wsgi installed and activated, edit your Apache server's\n<a class=\"reference external\" href=\"https://cwiki.apache.org/confluence/display/httpd/DistrosDefaultLayout\">httpd.conf</a> file and add the following.</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"nb\">WSGIScriptAlias</span> / <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n<span class=\"nb\">WSGIPythonHome</span> <span class=\"sx\">/path/to/venv</span>\n<span class=\"nb\">WSGIPythonPath</span> <span class=\"sx\">/path/to/mysite.com</span>\n\n<span class=\"nt\">&lt;Directory</span> <span class=\"s\">/path/to/mysite.com/mysite</span><span class=\"nt\">&gt;</span>\n<span class=\"nt\">&lt;Files</span> <span class=\"s\">wsgi.py</span><span class=\"nt\">&gt;</span>\n<span class=\"nb\">Require</span> <span class=\"k\">all</span> granted\n<span class=\"nt\">&lt;/Files&gt;</span>\n<span class=\"nt\">&lt;/Directory&gt;</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">WSGIScriptAlias​​</code> の行の最初の部分は、あなたがアプリケーションを提供したいベースとなるURLパスであり( <code class=\"docutils literal notranslate\">/</code> ルートURLを示している)、二番目の部分はシステムの <code class=\"docutils literal notranslate\">WSGI file</code> の場所 (下記参照) です。大抵はプロジェクトパッケージ (本例では <code class=\"docutils literal notranslate\">mysite</code> ) の内部です。これは、そのファイルで定義された WSGI アプリケーションを使用して、指定された URL 以下のすべての要求にサービスを提供するように Apache に指示します。</p>\n<p>If you install your project's Python dependencies inside a <a class=\"reference external\" href=\"https://docs.python.org/3/library/venv.html#module-venv\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">virtual\nenvironment</code></a>, add the path using <code class=\"docutils literal notranslate\">WSGIPythonHome</code>. See the <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html\">mod_wsgi\nvirtual environment guide</a> for more details.</p>\n<p><code class=\"docutils literal notranslate\">WSGIPythonPath</code> の行は、プロジェクトのパッケージは Python のパス上でインポートすることが可能であることを保証します。言い換えると、 <code class=\"docutils literal notranslate\">import mysite</code> が動作することを保証します。</p>\n<p>The <code class=\"docutils literal notranslate\">&lt;Directory&gt;</code> piece ensures that Apache can access your <code class=\"file docutils literal notranslate\">wsgi.py</code>\nfile.</p>\n<p>次に、この <code class=\"file docutils literal notranslate\">wsgi.py</code> がWSGIアプリケーションのオブジェクトで存在することを確認する必要があります。 Djangoのバージョン1.4現在、 <a class=\"reference internal\" href=\"/ja/4.2/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a>  を実行した際に作成されます ; それより前のバージョンでは、あなたはそれを作成する必要があります。 <a class=\"reference internal\" href=\"/ja/4.2/howto/deployment/wsgi/\"><span class=\"doc\">WSGI概要ドキュメント</span></a> を参照し、デフォルトのコンテンツや、その他の必要な記載をこのファイルに追加してください。</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>複数の Django のサイトが単一の mod_wsgi プロセスで実行されている場合は、それらのすべては、最初に実行されたものの設定を使用します。これを解消するには、次の</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>os<span class=\"o\">.</span>environ<span class=\"o\">.</span>setdefault<span class=\"p\">(</span><span class=\"s2\">&quot;DJANGO_SETTINGS_MODULE&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;{{ project_name }}.settings&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>という <code class=\"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>os<span class=\"o\">.</span>environ<span class=\"p\">[</span><span class=\"s2\">&quot;DJANGO_SETTINGS_MODULE&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;{{ project_name }}.settings&quot;</span>\n</code></pre></div>\n<p>もしくは、 <a class=\"reference internal\" href=\"#daemon-mode\"><span class=\"std std-ref\">mod_wsgi のデーモンモード</span></a> を使用することで、各サイトをそれぞれ独立したデーモンプロセスで実行できるようにします。</p>\n</aside>\n<aside class=\"admonition-fixing-unicodeencodeerror-for-file-uploads admonition\" role=\"note\">\n<p class=\"admonition-title\">ファイルアップロード時の <code class=\"docutils literal notranslate\">UnicodeEncodeError</code> を修正する</p>\n<p>If you get a <code class=\"docutils literal notranslate\">UnicodeEncodeError</code> when uploading or writing files with\nfile names or content that contains non-ASCII characters, make sure Apache\nis configured to support UTF-8 encoding:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</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=\"Shell code\"><code><span class=\"nb\">export</span> <span class=\"nv\">LANG</span><span class=\"o\">=</span><span class=\"s1\">&#39;en_US.UTF-8&#39;</span>\n<span class=\"nb\">export</span> <span class=\"nv\">LC_ALL</span><span class=\"o\">=</span><span class=\"s1\">&#39;en_US.UTF-8&#39;</span>\n</code></pre></div>\n<p>この設定はふつう <code class=\"docutils literal notranslate\">/etc/apache2/envvars</code> で可能です。</p>\n<p>Alternatively, if you are <a class=\"reference internal\" href=\"#daemon-mode\"><span class=\"std std-ref\">using mod_wsgi daemon mode</span></a>\nyou can add <code class=\"docutils literal notranslate\">lang</code> and <code class=\"docutils literal notranslate\">locale</code> options to the <code class=\"docutils literal notranslate\">WSGIDaemonProcess</code>\ndirective:</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>WSGIDaemonProcess example.com lang=&#39;en_US.UTF-8&#39; locale=&#39;en_US.UTF-8&#39;\n</code></pre></div>\n<p>詳細については、Unicode リファレンスガイドの <a class=\"reference internal\" href=\"/ja/4.2/ref/unicode/#unicode-files\"><span class=\"std std-ref\">Files</span></a> セクションを参照してください。</p>\n</aside>\n</section>\n<section id=\"using-mod-wsgi-daemon-mode\">\n<span id=\"daemon-mode\"></span><h2><code class=\"docutils literal notranslate\">mod_wsgi</code> をデーモンモードで使用する<a class=\"heading-anchor\" href=\"#using-mod-wsgi-daemon-mode\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><code class=\"docutils literal notranslate\">デーモンモード</code> はmod_wsgiを (Windows以外のプラットフォーム上で) 実行するための推奨モードです。必要なデーモンプロセスグループを作成し、その中で起動するための Djangoのインスタンスを委任するためには、適切な `` WSGIDaemonProcess`` と `` WSGIProcessGroup`` ディレクティブを追加する必要があります。デーモンモードを使用する場合には <code class=\"docutils literal notranslate\">WSGIPythonPath</code> を使用できないため、上記設定にさらなる変更が求められます。代わりに <code class=\"docutils literal notranslate\">WSGIDaemonProcess</code> に <code class=\"docutils literal notranslate\">python-path</code> オプションを使用する必要があります。例えば、</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"nb\">WSGIDaemonProcess</span> example.com python-home=/path/to/venv python-path=/path/to/mysite.com\n<span class=\"nb\">WSGIProcessGroup</span> example.com\n</code></pre></div>\n<p>サブディレクトリでプロジェクトを提供したい場合は(本例では <code class=\"docutils literal notranslate\">https://example.com/mysite</code> となります) 、 <code class=\"docutils literal notranslate\">WSGIScriptAlias</code> を設定の上部に追記します。</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"nb\">WSGIScriptAlias</span> <span class=\"sx\">/mysite</span> <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span> process-group=example.com\n</code></pre></div>\n<p>詳細については、公式の mod_wsgi のドキュメント <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html#delegation-to-daemon-process\">details on setting up daemon mode</a> を参照してください。</p>\n</section>\n<section id=\"serving-files\">\n<span id=\"id1\"></span><h2>ファイルを配信する<a class=\"heading-anchor\" href=\"#serving-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django 自体はファイルを配信せず、Web サーバにそのジョブを委ねます。</p>\n<p>We recommend using a separate web server -- i.e., one that's not also running\nDjango -- for serving media. Here are some good choices:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference external\" href=\"https://nginx.org/en/\">Nginx</a></p></li>\n<li><p><a class=\"reference external\" href=\"https://httpd.apache.org/\">Apache</a> の機能縮小版</p></li>\n</ul>\n<p>しかし、Django と同じ Apache の <code class=\"docutils literal notranslate\">VirtualHost</code> からメディアファイルを配信しなければならない場合には、一部の URL を静的メディアを配信するように設定し、その他の URL を Django への mod_wsgi のインターフェイスとして設定することができます。</p>\n<p>この例では、サイトのルートには Django を設定していますが、 <code class=\"docutils literal notranslate\">robots.txt</code> 、 <code class=\"docutils literal notranslate\">favicon.ico</code> 、 <code class=\"docutils literal notranslate\">/static/</code> 、そして <code class=\"docutils literal notranslate\">/media/</code> の URL 空間は静的ファイルとして配信しています。他のすべての URL は mod_wsgi を使用して配信されます。</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"nb\">Alias</span> <span class=\"sx\">/robots.txt</span> <span class=\"sx\">/path/to/mysite.com/static/robots.txt</span>\n<span class=\"nb\">Alias</span> <span class=\"sx\">/favicon.ico</span> <span class=\"sx\">/path/to/mysite.com/static/favicon.ico</span>\n\n<span class=\"nb\">Alias</span> <span class=\"sx\">/media/</span> <span class=\"sx\">/path/to/mysite.com/media/</span>\n<span class=\"nb\">Alias</span> <span class=\"sx\">/static/</span> <span class=\"sx\">/path/to/mysite.com/static/</span>\n\n<span class=\"nt\">&lt;Directory</span> <span class=\"s\">/path/to/mysite.com/static</span><span class=\"nt\">&gt;</span>\n<span class=\"nb\">Require</span> <span class=\"k\">all</span> granted\n<span class=\"nt\">&lt;/Directory&gt;</span>\n\n<span class=\"nt\">&lt;Directory</span> <span class=\"s\">/path/to/mysite.com/media</span><span class=\"nt\">&gt;</span>\n<span class=\"nb\">Require</span> <span class=\"k\">all</span> granted\n<span class=\"nt\">&lt;/Directory&gt;</span>\n\n<span class=\"nb\">WSGIScriptAlias</span> / <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n\n<span class=\"nt\">&lt;Directory</span> <span class=\"s\">/path/to/mysite.com/mysite</span><span class=\"nt\">&gt;</span>\n<span class=\"nt\">&lt;Files</span> <span class=\"s\">wsgi.py</span><span class=\"nt\">&gt;</span>\n<span class=\"nb\">Require</span> <span class=\"k\">all</span> granted\n<span class=\"nt\">&lt;/Files&gt;</span>\n<span class=\"nt\">&lt;/Directory&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"serving-the-admin-files\">\n<span id=\"id3\"></span><h2>admin ファイルを配信する<a class=\"heading-anchor\" href=\"#serving-the-admin-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When <a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.staticfiles</code></a> is in <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a>, the\nDjango development server automatically serves the static files of the\nadmin app (and any other installed apps). This is however not the case when you\nuse any other server arrangement. You're responsible for setting up Apache, or\nwhichever web server you're using, to serve the admin files.</p>\n<p>The admin files live in (<a class=\"extlink-source reference external\" href=\"https://github.com/django/django/blob/stable/4.2.x/django/contrib/admin/static/admin\">django/contrib/admin/static/admin</a>) of the\nDjango distribution.</p>\n<p>We <strong>strongly</strong> recommend using <a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.staticfiles</code></a> to handle the\nadmin files (along with a web server as outlined in the previous section; this\nmeans using the <a class=\"reference internal\" href=\"/ja/4.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\">collectstatic</code></a> management command to collect the\nstatic files in <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_ROOT</code></a>, and then configuring your web server to\nserve <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_ROOT</code></a> at <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_URL</code></a>), but here are three\nother approaches:</p>\n<ol class=\"arabic simple\">\n<li><p>ドキュメントルートに admin の静的ファイルへのシンボリックを作ります。 (Apache の設定に <code class=\"docutils literal notranslate\">+FollowSymLinks</code> が必要になるでしょう)</p></li>\n<li><p>上で示したように Alias ディレクティブを使って、適切な URL (おそらく <a class=\"reference internal\" href=\"/ja/4.2/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_URL</code></a> + <code class=\"docutils literal notranslate\">admin/</code>) から admin ファイルがある実際の場所へのエ イリアスを作ります。</p></li>\n<li><p>admin の静的ファイルをコピーし、 Apache のドキュメントルートに置きます。</p></li>\n</ol>\n</section>\n<section id=\"authenticating-against-django-s-user-database-from-apache\">\n<h2>Django のユーザーデータベースに対する Apache からの認証<a class=\"heading-anchor\" href=\"#authenticating-against-django-s-user-database-from-apache\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>DjangoはApacheがDjangoの認証バックエンドに対して直接ユーザを認証できるようにハンドラを提供します。 <a class=\"reference internal\" href=\"/ja/4.2/howto/deployment/wsgi/apache-auth/\"><span class=\"doc\">mod_wsgi 認証ドキュメント</span></a> を参照してください。</p>\n</section>","rootId":"how-to-use-django-with-apache-and-mod-wsgi","toc":[{"title":"基本設定","anchor":"basic-configuration","children":[]},{"title":"mod_wsgi をデーモンモードで使用する","anchor":"using-mod-wsgi-daemon-mode","children":[]},{"title":"ファイルを配信する","anchor":"serving-files","children":[]},{"title":"admin ファイルを配信する","anchor":"serving-the-admin-files","children":[]},{"title":"Django のユーザーデータベースに対する Apache からの認証","anchor":"authenticating-against-django-s-user-database-from-apache","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"「How-to」ガイド","url":"/ja/4.2/howto/"},{"docname":"howto/deployment/index","title":"Djangoをデプロイするには","url":"/ja/4.2/howto/deployment/"},{"docname":"howto/deployment/wsgi/index","title":"WSGI とともにデプロイするには","url":"/ja/4.2/howto/deployment/wsgi/"}],"prev":{"docname":"howto/deployment/wsgi/uwsgi","title":"Django を uWSGI とともに使うには？","url":"/ja/4.2/howto/deployment/wsgi/uwsgi/"},"next":{"docname":"howto/deployment/wsgi/apache-auth","title":"Django のユーザーデータベースに対する Apache からの認証","url":"/ja/4.2/howto/deployment/wsgi/apache-auth/"},"formats":{"html":"/ja/4.2/howto/deployment/wsgi/modwsgi/","markdown":"/ja/4.2/howto/deployment/wsgi/modwsgi.md","json":"/ja/4.2/howto/deployment/wsgi/modwsgi.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/howto/deployment/wsgi/modwsgi.txt","official":"https://docs.djangoproject.com/ja/4.2/howto/deployment/wsgi/modwsgi/","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","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}