静的ファイルのデプロイ¶
参考
django.contrib.staticfiles
の使い方の基本に関しては、静的ファイル (画像、JavaScript、CSS など) を管理する を読んでください。
本番環境における静的ファイルの配信¶
The basic outline of putting static files into production consists of two
steps: run the collectstatic
command when static files change, then
arrange for the collected static files directory (STATIC_ROOT
) to be
moved to the static file server and served. Depending on
STATICFILES_STORAGE
, files may need to be moved to a new location
manually or the post_process
method of
the Storage
class might take care of that.
As with all deployment tasks, the devil's in the details. Every production setup will be a bit different, so you'll need to adapt the basic outline to fit your needs. Below are a few common patterns that might help.
サイトと静的ファイルを同じサーバから配信する¶
静的ファイルをすでにサイトを配信しているのと同じサーバから配信したい場合、配信の手順は次のようになります。
- デプロイするサーバにコードを push する。
- サーバ側で、
collectstatic
を実行することで、すべての静的ファイルをSTATIC_ROOT
で設定したディレクトリに集める。 STATIC_ROOT
に置かれたファイルをSTATIC_URL
から配信するように、Web サーバの設定を行う。たとえば、Apache と mod_wsgi を使用している場合、Apache と mod_wsgi を使用したファイルの配信 が参考になると思います。
複数の Web サーバーがある場合は、おそらくこのプロセスを自動化したいと思うでしょう。