---
title: "Django のユーザーデータベースに対する Apache からの認証"
version: 6.1
locale: ja
source: https://docs.djangoproject.com/ja/6.1/howto/deployment/wsgi/apache-auth/
canonical: https://djangodocs.dev/ja/6.1/howto/deployment/wsgi/apache-auth/
---
# Django のユーザーデータベースに対する Apache からの認証

Since keeping multiple authentication databases in sync is a common problem
when dealing with Apache, you can configure Apache to authenticate against
Django's [authentication system](/ja/6.1/topics/auth/) directly. This
requires Apache version \>= 2.2 and `mod_wsgi` \>= 2.0. For example, you could:

- 認証されたユーザにのみ、静的/メディアファイルを Apache から直接提供する。
- 特定のパーミッションを持つ Django ユーザに対して、 [Subversion](https://subversion.apache.org/) リポジトリへのアクセスを認証する。
- [mod\_dav](https://httpd.apache.org/docs/current/mod/mod_dav.html) で作成された WebDAV 共有に特定のユーザが接続できるように許可する。

> **Note**
>
> もし [カスタムユーザーモデル](/ja/6.1/topics/auth/customizing/#auth-custom-user) をインストールしていて、このデフォルトの認証ハンドラを使用したい場合は、 `is_active` 属性をサポートしている必要があります。グループベースの認可を使用したい場合、カスタムユーザは 'group' という名前のリレーションを持ち、'name' フィールドを持つリレーション先のオブジェクトを参照する必要があります。カスタムの mod\_wsgi 認証ハンドラがこれらの要件に適合しない場合は、 独自のカスタムの mod\_wsgi 認証ハンドラを指定することもできます。

## `mod_wsgi` による認証

> **Note**
>
> 以下の設定における `WSGIApplicationGroup %{GLOBAL}` の使用は、 Apache インスタンスが 1 つの Django アプリケーションだけを実行していることを想定しています。複数の Django アプリケーションを実行している場合は、この設定については mod\_wsgi ドキュメントの [Defining Application Groups](https://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html#defining-application-groups) セクションを参照してください。

mod\_wsgiがインストールされ、有効になっていることと、 [Apache と mod\_wsgi](/ja/6.1/howto/deployment/wsgi/modwsgi/) をセットアップするステップに従っていることを確認してください。

次に、Apacheの設定を編集して、認証されたユーザだけが閲覧できるようにしたい場所を追加します:

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

WSGIProcessGroup %{GLOBAL}
WSGIApplicationGroup %{GLOBAL}

<Location "/secret">
    AuthType Basic
    AuthName "Top Secret"
    Require valid-user
    AuthBasicProvider wsgi
    WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
</Location>
```

`WSGIAuthUserScript` ディレクティブは、プロンプトから受け取ったユーザ名とパスワードを渡して、指定した wsgi スクリプトの `check_password` 関数を実行するように mod\_wsgi に指示します。この例では、 `WSGIAuthUserScript` は [django-admin startproject で作成される](/ja/6.1/howto/deployment/wsgi/) アプリケーションを定義する `WSGIScriptAlias` と同じです。

> **Using Apache 2.2+ with authentication**
>
> `mod_auth_basic` と `mod_authz_user` がロードされていることを確認してください。
>
> これらは Apache に静的にコンパイルされているかもしれませんし、LoadModule を使って `httpd.conf` で動的にロードする必要があるかもしれません:
>
> ```apache
> LoadModule auth_basic_module modules/mod_auth_basic.so
> LoadModule authz_user_module modules/mod_authz_user.so
> ```

最後に、WSGIスクリプト `mysite.wsgi` を編集して、 `check_password` 関数をインポートすることで、Apacheの認証とサイトの認証メカニズムを紐づけます:

```
import os

os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"

from django.contrib.auth.handlers.modwsgi import check_password

from django.core.handlers.wsgi import WSGIHandler

application = WSGIHandler()
```

これで、 `/secret/` で始まるリクエストはユーザー認証が必要になりました。

mod\_wsgi の [access control mechanisms documentation](https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.html) に、認証の代替方法についての詳細と情報があります。

### `mod_wsgi` と Django グループを使った認可

mod\_wsgi は、グループのメンバーに特定の場所を制限する機能も提供します。

この場合、Apacheの設定は次のようになります:

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

WSGIProcessGroup %{GLOBAL}
WSGIApplicationGroup %{GLOBAL}

<Location "/secret">
    AuthType Basic
    AuthName "Top Secret"
    AuthBasicProvider wsgi
    WSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py
    WSGIAuthGroupScript /path/to/mysite.com/mysite/wsgi.py
    Require group secret-agents
    Require valid-user
</Location>
```

To support the `WSGIAuthGroupScript` directive, the same WSGI script
`mysite.wsgi` must also import the `groups_for_user` function which
returns a list of groups the given user belongs to.

```python
from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
```

これで、 `/secret/` に対するリクエストは、ユーザーが "secret-agents" グループのメンバーであることも要求するようになりました。
