Apache2のダイジェスト認証ではまった

AuthType Digest
AuthName realm
AuthDigestFile passwd.digest
require valid-user

と書いたら

Invalid command 'AuthDigestFile', perhaps mis-spelled or defined by a module not included in the server configuration

というエラーが出た。

AuthDigestFile -> AuthUserFile と変更すると、500 Internal Error になって、エラーログには

configuration error:  couldn't check user.  No user file?: /admin/

と出ていた。

  • 原因1: mod_auth_digest.so が読み込まれていなかった
sudo ln -s /etc/apache2/mods-available/auth_digest.load /etc/apache2/mods-enabled/
  • 原因2: パスワードファイルを作成するコマンドが違った
htpasswd -c passwd.digest user

ではなくて、

htdigest -c passwd.digest realm user

htpasswd は BASIC 認証用。

  • 原因3: Apache 2.2じゃなかった

Apache 2.2 から AuthDigestFile -> AuthUserFile と設定名が変わったので、最初そのせいだと思って変更したら、実はモジュールが読み込まれていないことが原因だった。逆に名前を変えると他を正しく設定してもApache 2.2以前では"couldn't check user."エラーが出る。

特に原因1と3が複合して混乱の元になった。

まとめると、

  • 必要なモジュールを読み込む
  • パスワードを作成するのに正しいコマンドを使う
  • パスワードファイルの場所を設定するのは、 Apache 2.2 以前は AuthDigestFileで、Apache 2.2以降は AuthUserFile