---
title: "静的ファイルをデプロイする"
version: 4.2
locale: ja
source: https://docs.djangoproject.com/ja/4.2/howto/static-files/deployment/
canonical: https://djangodocs.dev/ja/4.2/howto/static-files/deployment/
---
# 静的ファイルをデプロイする

> **See also**
>
> [`django.contrib.staticfiles`](/ja/4.2/ref/contrib/staticfiles/#module-django.contrib.staticfiles) の使い方の基本に関しては、[静的ファイル (画像、JavaScript、CSS など) を管理する](/ja/4.2/howto/static-files/) を読んでください。

## 本番環境における静的ファイルの配信

The basic outline of putting static files into production consists of two
steps: run the [`collectstatic`](/ja/4.2/ref/contrib/staticfiles/#django-admin-collectstatic) command when static files change, then
arrange for the collected static files directory ([`STATIC_ROOT`](/ja/4.2/ref/settings/#std-setting-STATIC_ROOT)) to be
moved to the static file server and served. Depending the `staticfiles`
[`STORAGES`](/ja/4.2/ref/settings/#std-setting-STORAGES) alias, files may need to be moved to a new location
manually or the [`post_process`](/ja/4.2/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.StaticFilesStorage.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`](/ja/4.2/ref/contrib/staticfiles/#django-admin-collectstatic) を実行することで、すべての静的ファイルを  [`STATIC_ROOT`](/ja/4.2/ref/settings/#std-setting-STATIC_ROOT) で設定したディレクトリに集める。
- [`STATIC_ROOT`](/ja/4.2/ref/settings/#std-setting-STATIC_ROOT) に置かれたファイルを [`STATIC_URL`](/ja/4.2/ref/settings/#std-setting-STATIC_URL) から配信するように、Web サーバの設定を行う。たとえば、Apache と mod\_wsgi を使用している場合、[Apache と mod\_wsgi を使用したファイルの配信](/ja/4.2/howto/deployment/wsgi/modwsgi/#serving-files) が参考になると思います。

複数の Web サーバーがある場合は、おそらくこのプロセスを自動化したいと思うでしょう。

### 専用のサーバから静的ファイルを配信する

Most larger Django sites use a separate web server -- i.e., one that's not also
running Django -- for serving static files. This server often runs a different
type of web server -- faster but less full-featured. Some common choices are:

- [Nginx](https://nginx.org/en/)
- [Apache](https://httpd.apache.org/) の機能縮小版

これらのサーバの設定方法は、このドキュメントの範囲外です。それぞれのサーバのドキュメントを参考に設定してください。

静的ファイルサーバでは Django が実行されていないので、次のようにデプロイの戦略を変更する必要があります。

- 静的ファイルが変更されたら、ローカル側で [`collectstatic`](/ja/4.2/ref/contrib/staticfiles/#django-admin-collectstatic) を実行する。
- ローカル側の [`STATIC_ROOT`](/ja/4.2/ref/settings/#std-setting-STATIC_ROOT) を静的ファイルサーバのファイル配信ディレクトリにアップロードします。これには、[rsync](https://rsync.samba.org/) を使用するのが一般的です。rsync を利用すれば、変更された静的ファイルの情報だけを転送することができます。

### クラウドサービスや CDN から静的ファイルを配信する

Another common tactic is to serve static files from a cloud storage provider
like Amazon's S3 and/or a CDN (content delivery network). This lets you
ignore the problems of serving static files and can often make for
faster-loading web pages (especially when using a CDN).

これらのサービスを使う場合でも、基本的なワークフローは上で説明した通りです。ただし、`rsync` を使って静的ファイルをサーバに転送する代わりに、ストレージプロバイダや CDN に転送する必要があります。

There's any number of ways you might do this, but if the provider has an API,
you can use a [custom file storage backend](/ja/4.2/howto/custom-file-storage/)
to integrate the CDN with your Django project. If you've written or are using a
3rd party custom storage backend, you can tell [`collectstatic`](/ja/4.2/ref/contrib/staticfiles/#django-admin-collectstatic) to use
it by setting `staticfiles` in [`STORAGES`](/ja/4.2/ref/settings/#std-setting-STORAGES).

たとえば、S3 storage backend を `myproject.storage.S3Storage` としてすでに書いていれば、次のように書くだけでこのストレージを利用できます。

```
STORAGES = {
    # ...
    "staticfiles": {"BACKEND": "myproject.storage.S3Storage"}
}
```

Once that's done, all you have to do is run [`collectstatic`](/ja/4.2/ref/contrib/staticfiles/#django-admin-collectstatic) and your
static files would be pushed through your storage package up to S3. If you
later needed to switch to a different storage provider, you may only have to
change `staticfiles` in the [`STORAGES`](/ja/4.2/ref/settings/#std-setting-STORAGES) setting.

For details on how you'd write one of these backends, see
[カスタムのストレージクラスを書く](/ja/4.2/howto/custom-file-storage/). There are 3rd party apps available that
provide storage backends for many common file storage APIs. A good starting
point is the [overview at djangopackages.org](https://djangopackages.org/grids/g/storage-backends/).

> **Changed in Django 4.2**
>
> :setting:STORAGES\`設定が追加されました。

## さらに学ぶ

すべての設定、コマンド、テンプレートタグなどの詳細と、[`django.contrib.staticfiles`](/ja/4.2/ref/contrib/staticfiles/#module-django.contrib.staticfiles) に含まれているその他の機能については、[staticfiles リファレンス](/ja/4.2/ref/contrib/staticfiles/) を読んでください。
