{"title":"静的ファイルをデプロイする","version":"5.2","locale":"ja","docname":"howto/static-files/deployment","url":"/ja/5.2/howto/static-files/deployment/","canonical":"https://djangodocs.dev/ja/5.2/howto/static-files/deployment/","summary":"参考 django.contrib.staticfiles の使い方の基本に関しては、 静的ファイル (画像、JavaScript、CSS など) を管理する を読んでください。 本番環境における静的ファイルの配信 Link to this heading #…","html":"<h1>静的ファイルをデプロイする<a class=\"heading-anchor\" href=\"#how-to-deploy-static-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">参考</p>\n<p><a class=\"reference internal\" href=\"/ja/5.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\"><span class=\"pre\">django.contrib.staticfiles</span></code></a> の使い方の基本に関しては、<a class=\"reference internal\" href=\"/ja/5.2/howto/static-files/\"><span class=\"doc\">静的ファイル (画像、JavaScript、CSS など) を管理する</span></a> を読んでください。</p>\n</aside>\n<section id=\"serving-static-files-in-production\">\n<span id=\"staticfiles-production\"></span><h2>本番環境における静的ファイルの配信<a class=\"heading-anchor\" href=\"#serving-static-files-in-production\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>本番環境で静的ファイルを配置する基本的な手順は、次の2ステップからなります。まず、静的ファイルが変更されたときに <a class=\"reference internal\" href=\"/ja/5.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> コマンドを実行します。その後、収集された静的ファイルのディレクトリ (<a class=\"reference internal\" href=\"/ja/5.2/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a>) を静的ファイルサーバーに移動し、配信できるようにします。<code class=\"docutils literal notranslate\"><span class=\"pre\">staticfiles</span></code> の <a class=\"reference internal\" href=\"/ja/5.2/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STORAGES</span></code></a> エイリアスに応じて、ファイルを手動で新しい場所に移動しなければならないこともありますが、<code class=\"docutils literal notranslate\"><span class=\"pre\">Storage</span></code> クラスの <a class=\"reference internal\" href=\"/ja/5.2/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.StaticFilesStorage.post_process\" title=\"django.contrib.staticfiles.storage.StaticFilesStorage.post_process\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">post_process</span></code></a> メソッドで処理できることもあります。</p>\n<p>すべてのデプロイメントタスクと同様に、悪魔は細部に宿ります。プロダクションのセットアップはそれぞれ少しずつ異なるので、基本的なアウトラインをニーズに合わせて変更する必要があります。以下に、役立つかもしれないいくつかの一般的なパターンを示します。</p>\n<section id=\"serving-the-site-and-your-static-files-from-the-same-server\">\n<h3>サイトと静的ファイルを同じサーバから配信する<a class=\"heading-anchor\" href=\"#serving-the-site-and-your-static-files-from-the-same-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>静的ファイルをすでにサイトを配信しているのと同じサーバから配信したい場合、配信の手順は次のようになります。</p>\n<ul class=\"simple\">\n<li><p>デプロイするサーバにコードを push する。</p></li>\n<li><p>サーバ側で、 <a class=\"reference internal\" href=\"/ja/5.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> を実行することで、すべての静的ファイルを  <a class=\"reference internal\" href=\"/ja/5.2/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> で設定したディレクトリに集める。</p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/5.2/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> に置かれたファイルを <a class=\"reference internal\" href=\"/ja/5.2/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a> から配信するように、Web サーバの設定を行う。たとえば、Apache と mod_wsgi を使用している場合、<a class=\"reference internal\" href=\"/ja/5.2/howto/deployment/wsgi/modwsgi/#serving-files\"><span class=\"std std-ref\">Apache と mod_wsgi を使用したファイルの配信</span></a> が参考になると思います。</p></li>\n</ul>\n<p>複数の Web サーバーがある場合は、おそらくこのプロセスを自動化したいと思うでしょう。</p>\n</section>\n<section id=\"serving-static-files-from-a-dedicated-server\">\n<h3>専用のサーバから静的ファイルを配信する<a class=\"heading-anchor\" href=\"#serving-static-files-from-a-dedicated-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>ほとんどの大規模な Django サイトでは、静的ファイルの配信に別の Web サーバ（つまり Django を動かしていないサーバ）を使っています。このサーバは、多くの場合、別のタイプの Web サーバを実行しています。高速で、機能が限定的なものです。よくある選択肢は下記です:</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>これらのサーバの設定方法は、このドキュメントの範囲外です。それぞれのサーバのドキュメントを参考に設定してください。</p>\n<p>静的ファイルサーバでは Django が実行されていないので、次のようにデプロイの戦略を変更する必要があります。</p>\n<ul class=\"simple\">\n<li><p>静的ファイルが変更されたら、ローカル側で <a class=\"reference internal\" href=\"/ja/5.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> を実行する。</p></li>\n<li><p>ローカルの <a class=\"reference internal\" href=\"/ja/5.2/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> を静的ファイルサーバのファイル配信ディレクトリに push します。これには通常、<a class=\"reference external\" href=\"https://rsync.samba.org/\">rsync</a> を使います。変更された静的ファイルだけを転送できるからです。</p></li>\n</ul>\n</section>\n<section id=\"serving-static-files-from-a-cloud-service-or-cdn\">\n<span id=\"staticfiles-from-cdn\"></span><h3>クラウドサービスや CDN から静的ファイルを配信する<a class=\"heading-anchor\" href=\"#serving-static-files-from-a-cloud-service-or-cdn\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>もう1つの一般的な方法は、AmazonのS3やCDN（コンテンツ・デリバリー・ネットワーク）のようなクラウドストレージから静的ファイルを配信することです。これにより、静的ファイルを配信する際の問題を無視することができ、多くの場合、Webページの読み込みが速くなります（特にCDNを使用する場合）。</p>\n<p>これらのサービスを使う場合でも、基本的なワークフローは上で説明した通りです。ただし、<code class=\"docutils literal notranslate\"><span class=\"pre\">rsync</span></code> を使って静的ファイルをサーバに転送する代わりに、ストレージプロバイダや CDN に転送する必要があります。</p>\n<p>そのための方法はいろいろありますが、プロバイダが API を提供している場合、<a class=\"reference internal\" href=\"/ja/5.2/howto/custom-file-storage/\"><span class=\"doc\">カスタムのファイルストレージバックエンド</span></a> を使って Django プロジェクトと CDN を統合できます。独自に作成したカスタムストレージバックエンドを使用している場合や、サードパーティのカスタムストレージバックエンドを使用している場合は、<a class=\"reference internal\" href=\"/ja/5.2/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STORAGES</span></code></a> に <code class=\"docutils literal notranslate\"><span class=\"pre\">staticfiles</span></code> を設定することで <a class=\"reference internal\" href=\"/ja/5.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> にそれを使うよう指示できます。</p>\n<p>たとえば、S3 storage backend を <code class=\"docutils literal notranslate\"><span class=\"pre\">myproject.storage.S3Storage</span></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=\"n\">STORAGES</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"s2\">&quot;staticfiles&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span><span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;myproject.storage.S3Storage&quot;</span><span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>これが完了したら、 <a class=\"reference internal\" href=\"/ja/5.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> を実行するだけで、静的ファイルがストレージパッケージを通して S3 にプッシュされます。後で別のストレージプロバイダに切り替える必要がある場合は、 <a class=\"reference internal\" href=\"/ja/5.2/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STORAGES</span></code></a> 設定の <code class=\"docutils literal notranslate\"><span class=\"pre\">staticfiles</span></code> を変更するだけです。</p>\n<p>これらのバックエンドの書き方の詳細については <a class=\"reference internal\" href=\"/ja/5.2/howto/custom-file-storage/\"><span class=\"doc\">カスタムのストレージクラスを書く</span></a> を参照してください。多くの一般的なファイルストレージ API 用のストレージバックエンドを提供するサードパーティアプリがあります。良い出発点は <a class=\"reference external\" href=\"https://djangopackages.org/grids/g/storage-backends/\">djangopackages.org の overview</a> です。</p>\n</section>\n</section>\n<section id=\"learn-more\">\n<h2>さらに学ぶ<a class=\"heading-anchor\" href=\"#learn-more\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>すべての設定、コマンド、テンプレートタグなどの詳細と、<a class=\"reference internal\" href=\"/ja/5.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\"><span class=\"pre\">django.contrib.staticfiles</span></code></a> に含まれているその他の機能については、<a class=\"reference internal\" href=\"/ja/5.2/ref/contrib/staticfiles/\"><span class=\"doc\">staticfiles リファレンス</span></a> を読んでください。</p>\n</section>","rootId":"how-to-deploy-static-files","toc":[{"title":"本番環境における静的ファイルの配信","anchor":"serving-static-files-in-production","children":[{"title":"サイトと静的ファイルを同じサーバから配信する","anchor":"serving-the-site-and-your-static-files-from-the-same-server","children":[]},{"title":"専用のサーバから静的ファイルを配信する","anchor":"serving-static-files-from-a-dedicated-server","children":[]},{"title":"クラウドサービスや CDN から静的ファイルを配信する","anchor":"serving-static-files-from-a-cloud-service-or-cdn","children":[]}]},{"title":"さらに学ぶ","anchor":"learn-more","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"How-to ガイド","url":"/ja/5.2/howto/"}],"prev":{"docname":"howto/deployment/checklist","title":"デプロイチェックリスト","url":"/ja/5.2/howto/deployment/checklist/"},"next":{"docname":"howto/auth-remote-user","title":"REMOTE_USER で認証する","url":"/ja/5.2/howto/auth-remote-user/"},"formats":{"html":"/ja/5.2/howto/static-files/deployment/","markdown":"/ja/5.2/howto/static-files/deployment.md","json":"/ja/5.2/howto/static-files/deployment.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/howto/static-files/deployment.txt","official":"https://docs.djangoproject.com/ja/5.2/howto/static-files/deployment/","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","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}