構成概要
「http://www.example.com/secret/」以下のページは、ユーザ「user1」のみアクセス可能とするよう制限をかける。
前提条件
「http://www.example.com/secret/」が対象となる設定ファイル(384氏的設定の場合、/etc/apache2/sites-available/www.example.com.conf)にて、
「AllowOverride」に「All」または「AuthConfig」が指定されていること。
Digest認証
Digest認証のモジュールを有効にする。
a2enmod auth_digest
「secret」フォルダ直下に.htaccessファイルを作成する。
AuthType Digest AuthName "Secret Page" AuthDigestProvider file AuthUserFile /home/www-data/www.example.com/pass/.htdigest Require user user1
※5行目を「Require valid-user」にすると、.htdigestに記載された全ユーザを許可する。
パスワードファイルを作成する。
htdigest -c /home/www-data/www.example.com/pass/.htdigest 'Secret Page' user1 New password: ←パスワードを入力 Re-type new password: ←パスワードを入力
Apacheを再起動する。
/etc/init.d/apache2 restart
Basic認証
Basic認証のモジュールを有効にする。
a2enmod auth_basic
「secret」フォルダ直下に.htaccessファイルを作成する。
AuthType Basic AuthName "Secret Page" AuthUserFile /home/www-data/www.example.com/pass/.htpasswd Require user user1
※4行目を「Require valid-user」にすると、.htpasswdに記載された全ユーザを許可する。
パスワードファイルを作成する。
htpasswd -c /home/www-data/www.example.com/pass/.htpasswd New password: ←パスワードを入力 Re-type new password: ←パスワードを入力
Apacheを再起動する。
/etc/init.d/apache2 restart