Apache フォルダアクセス制限

構成概要

「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認証のモジュールを有効にする。
[shell]
a2enmod auth_digest
[/shell]

「secret」フォルダ直下に.htaccessファイルを作成する。
[code title=”/home/www-data/www.example.com/html/secret/.htaccess” highlight=”5″]
AuthType Digest
AuthName "Secret Page"
AuthDigestProvider file
AuthUserFile /home/www-data/www.example.com/pass/.htdigest
Require user user1
[/code]
※5行目を「Require valid-user」にすると、.htdigestに記載された全ユーザを許可する。

パスワードファイルを作成する。
[shell]
htdigest -c /home/www-data/www.example.com/pass/.htdigest ‘Secret Page’ user1
New password: ←パスワードを入力
Re-type new password: ←パスワードを入力
[/shell]

Apacheを再起動する。
[shell]
/etc/init.d/apache2 restart
[/shell]

Basic認証

Basic認証のモジュールを有効にする。
[shell]
a2enmod auth_basic
[/shell]

「secret」フォルダ直下に.htaccessファイルを作成する。
[code title=”/home/www-data/www.example.com/html/secret/.htaccess” highlight=”4″]
AuthType Basic
AuthName "Secret Page"
AuthUserFile /home/www-data/www.example.com/pass/.htpasswd
Require user user1
[/code]
※4行目を「Require valid-user」にすると、.htpasswdに記載された全ユーザを許可する。

パスワードファイルを作成する。
[shell]
htpasswd -c /home/www-data/www.example.com/pass/.htpasswd
New password: ←パスワードを入力
Re-type new password: ←パスワードを入力
[/shell]

Apacheを再起動する。
[shell]
/etc/init.d/apache2 restart
[/shell]

  • このエントリーをはてなブックマークに追加

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です