---
title: "Django を Apache と mod_wsgi とともに使うには？"
version: 4.0
locale: ja
source: https://docs.djangoproject.com/ja/4.0/howto/deployment/wsgi/modwsgi/
canonical: https://djangodocs.dev/ja/4.0/howto/deployment/wsgi/modwsgi/
---
# Django を Apache と `mod_wsgi` とともに使うには？

[Apache](https://httpd.apache.org/) と [mod\_wsgi](https://modwsgi.readthedocs.io/en/develop/) と共にDjangoをデプロイすることは、Djangoを製品とする際に試され、テストされた方法です。

mod\_wsgi は、Djangoを含む任意のPythonの W​​SGI\_ アプリケーションをホストできるApacheのモジュールです。 Djangoはmod\_wsgiをサポートしているApacheのすべてのバージョンで動作します。

The [official mod\_wsgi documentation](https://modwsgi.readthedocs.io/) is your source for all the details about
how to use mod\_wsgi. You'll probably want to start with the [installation and
configuration documentation](https://modwsgi.readthedocs.io/en/develop/installation.html).

## 基本設定

Once you've got mod\_wsgi installed and activated, edit your Apache server's
[httpd.conf](https://cwiki.apache.org/confluence/display/httpd/DistrosDefaultLayout) file and add the following.

```apache
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonHome /path/to/venv
WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
```

`WSGIScriptAlias​​` の行の最初の部分は、あなたがアプリケーションを提供したいベースとなるURLパスであり( `/` ルートURLを示している)、二番目の部分はシステムの `WSGI file` の場所 (下記参照) です。大抵はプロジェクトパッケージ (本例では `mysite` ) の内部です。これは、そのファイルで定義された WSGI アプリケーションを使用して、指定された URL 以下のすべての要求にサービスを提供するように Apache に指示します。

If you install your project's Python dependencies inside a [`virtual
environment`](https://docs.python.org/3/library/venv.html#module-venv), add the path using `WSGIPythonHome`. See the [mod\_wsgi
virtual environment guide](https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html) for more details.

`WSGIPythonPath` の行は、プロジェクトのパッケージは Python のパス上でインポートすることが可能であることを保証します。言い換えると、 `import mysite` が動作することを保証します。

The `<Directory>` piece ensures that Apache can access your `wsgi.py`
file.

次に、この `wsgi.py` がWSGIアプリケーションのオブジェクトで存在することを確認する必要があります。 Djangoのバージョン1.4現在、 [`startproject`](/ja/4.0/ref/django-admin/#django-admin-startproject)  を実行した際に作成されます ; それより前のバージョンでは、あなたはそれを作成する必要があります。 [WSGI概要ドキュメント](/ja/4.0/howto/deployment/wsgi/) を参照し、デフォルトのコンテンツや、その他の必要な記載をこのファイルに追加してください。

> **Warning**
>
> 複数の Django のサイトが単一の mod\_wsgi プロセスで実行されている場合は、それらのすべては、最初に実行されたものの設定を使用します。これを解消するには、次の
>
> ```
> os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
> ```
>
> という `wsgi.py` 内のコードを、次のように変更します。
>
> ```
> os.environ["DJANGO_SETTINGS_MODULE"] = "{{ project_name }}.settings"
> ```
>
> もしくは、 [mod\_wsgi のデーモンモード](#daemon-mode) を使用することで、各サイトをそれぞれ独立したデーモンプロセスで実行できるようにします。

> **ファイルアップロード時の UnicodeEncodeError を修正する**
>
> If you get a `UnicodeEncodeError` when uploading or writing files with
> file names or content that contains non-ASCII characters, make sure Apache
> is configured to support UTF-8 encoding:
>
> ```
> export LANG='en_US.UTF-8'
> export LC_ALL='en_US.UTF-8'
> ```
>
> この設定はふつう `/etc/apache2/envvars` で可能です。
>
> Alternatively, if you are [using mod\_wsgi daemon mode](#daemon-mode)
> you can add `lang` and `locale` options to the `WSGIDaemonProcess`
> directive:
>
> ```
> WSGIDaemonProcess example.com lang='en_US.UTF-8' locale='en_US.UTF-8'
> ```
>
> 詳細については、Unicode リファレンスガイドの [Files](/ja/4.0/ref/unicode/#unicode-files) セクションを参照してください。

## `mod_wsgi` をデーモンモードで使用する

`デーモンモード` はmod\_wsgiを (Windows以外のプラットフォーム上で) 実行するための推奨モードです。必要なデーモンプロセスグループを作成し、その中で起動するための Djangoのインスタンスを委任するためには、適切な \`\` WSGIDaemonProcess\`\` と \`\` WSGIProcessGroup\`\` ディレクティブを追加する必要があります。デーモンモードを使用する場合には `WSGIPythonPath` を使用できないため、上記設定にさらなる変更が求められます。代わりに `WSGIDaemonProcess` に `python-path` オプションを使用する必要があります。例えば、

```apache
WSGIDaemonProcess example.com python-home=/path/to/venv python-path=/path/to/mysite.com
WSGIProcessGroup example.com
```

サブディレクトリでプロジェクトを提供したい場合は(本例では `https://example.com/mysite` となります) 、 `WSGIScriptAlias` を設定の上部に追記します。

```apache
WSGIScriptAlias /mysite /path/to/mysite.com/mysite/wsgi.py process-group=example.com
```

詳細については、公式の mod\_wsgi のドキュメント [details on setting up daemon mode](https://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html#delegation-to-daemon-process) を参照してください。

## ファイルを配信する

Django doesn't serve files itself; it leaves that job to whichever web
server you choose.

We recommend using a separate web server -- i.e., one that's not also running
Django -- for serving media. Here are some good choices:

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

しかし、Django と同じ Apache の `VirtualHost` からメディアファイルを配信しなければならない場合には、一部の URL を静的メディアを配信するように設定し、その他の URL を Django への mod\_wsgi のインターフェイスとして設定することができます。

この例では、サイトのルートには Django を設定していますが、 `robots.txt` 、 `favicon.ico` 、 `/static/` 、そして `/media/` の URL 空間は静的ファイルとして配信しています。他のすべての URL は mod\_wsgi を使用して配信されます。

```apache
Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico

Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/

<Directory /path/to/mysite.com/static>
Require all granted
</Directory>

<Directory /path/to/mysite.com/media>
Require all granted
</Directory>

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
```

## admin ファイルを配信する

When [`django.contrib.staticfiles`](/ja/4.0/ref/contrib/staticfiles/#module-django.contrib.staticfiles) is in [`INSTALLED_APPS`](/ja/4.0/ref/settings/#std-setting-INSTALLED_APPS), the
Django development server automatically serves the static files of the
admin app (and any other installed apps). This is however not the case when you
use any other server arrangement. You're responsible for setting up Apache, or
whichever web server you're using, to serve the admin files.

admin ファイルは Django ディストリビューションの (`django/contrib/admin/static/admin`) にあります。

We **strongly** recommend using [`django.contrib.staticfiles`](/ja/4.0/ref/contrib/staticfiles/#module-django.contrib.staticfiles) to handle the
admin files (along with a web server as outlined in the previous section; this
means using the [`collectstatic`](/ja/4.0/ref/contrib/staticfiles/#django-admin-collectstatic) management command to collect the
static files in [`STATIC_ROOT`](/ja/4.0/ref/settings/#std-setting-STATIC_ROOT), and then configuring your web server to
serve [`STATIC_ROOT`](/ja/4.0/ref/settings/#std-setting-STATIC_ROOT) at [`STATIC_URL`](/ja/4.0/ref/settings/#std-setting-STATIC_URL)), but here are three
other approaches:

1. ドキュメントルートに admin の静的ファイルへのシンボリックを作ります。 (Apache の設定に `+FollowSymLinks` が必要になるでしょう)
2. 上で示したように Alias ディレクティブを使って、適切な URL (おそらく [`STATIC_URL`](/ja/4.0/ref/settings/#std-setting-STATIC_URL) \+ `admin/`) から admin ファイルがある実際の場所へのエ イリアスを作ります。
3. admin の静的ファイルをコピーし、 Apache のドキュメントルートに置きます。

## Django のユーザーデータベースに対する Apache からの認証

DjangoはApacheがDjangoの認証バックエンドに対して直接ユーザを認証できるようにハンドラを提供します。 [mod\_wsgi 認証ドキュメント](/ja/4.0/howto/deployment/wsgi/apache-auth/) を参照してください。
