Apache2(Win32) - パスワード制限(Basic認証)
Win32版Apache2 でのBasic認証の設定方法について解説します。
Apacheのインストールは
こちら。
コンフィグレーション
C:\usr\local\apache\conf\httpd.conf
デフォルトの設定では、httpd.conf で宣言しているディレクトリは全て「AllowOverride None」になっているので、
パスワード制限を掛けたい該当ディレクトリを「AllowOverride AuthConfig」もしくは「AllowOverride All」へ
httpd.conf を修正します。
--- httpd.conf.orig 2006-12-30 22:04:32.000000000 +0900
+++ httpd.conf 2007-01-07 19:25:40.348661000 +0900
@@ -162,7 +162,7 @@
#
<Directory />
Options FollowSymLinks
- AllowOverride None
+ AllowOverride All
Order deny,allow
Deny from all
Satisfy all
@@ -198,7 +198,7 @@
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
- AllowOverride None
+ AllowOverride All
#
# Controls who can get stuff from this server.
Apache サービスの再起動
Windows「スタート」->「ファイル名を指定して実行」から
「%SystemRoot%\system32\services.msc /s」を実行し、のApacheサービスにて右クリックから
再起動を選択・実行します。
.htaccess の作成
下記の4行を記述します。
パスワードファイルのパスを記述する際に、階層をバックスペース「\」ではなく、
UNIXと同じくスラッシュ「/」で記述します。
AuthType Basic
AuthUserFile "C:/usr/local/apache/htdocs/secret/.htpasswd"
AuthName "Password required."
require valid-user
.htpasswd の作成
ここでは、ユーザー名「myname」、パスワード「mypassword」としています。
C:\>"c:\usr\local\apache\bin\htpasswd" -c "c:\usr\local\apache\htdocs\secret\.htpasswd" myname mypassword
動作確認
ブラウザで該当サーバへアクセスし、認証のポップアップウィンドウが表示されれば設定完了です。