Webサーバ Apache パスワード規制
ホームページアクセス時にパスワードによりアクセス規制を行う
目的
ホームページアクセス時に、ログイン名とパスワードを表示、入力が正しい場合のみページの表示を行う。
構築条件
構築機器
- 仮想サーバ SUN VirtualBox 4.0 で構築
作成環境
OS | CentOS 5.6 64bit版 |
サーバ作成条件
サーバIP | 192.168.0.10/24 |
ホスト名 | hogehogeServer |
user | hogehoge |
userpasswd | hogehogepass |
mysqldb名 | mysql |
mysqluser名 | mysqluser |
mysqluserpasswd | mysqlpass |
作業エリア | /home/hogehoge |
手順記入ルール
- コマンド行頭記号で 「$」はユーザ権限 「#」はスーパーユーザー権限 を表示
- コマンドが長くなる場合は、行末に「\」を記述し複数行に記述。
前準備
- CentOSインストール 実施
- yumリポジトリ設定 実施
- LAMPサーバ構築 実施
- Apache/homeエリア解放 実施
設定
(1)Apache Configファイル設定
【telnet】
- rootでログイン
$ su パスワード:
- httpd.conf設定
# vi /etc/httpd/conf/httpd.conf
<Directory "/var/www/html"> : # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit #
- .htaccessを有効にする
AllowOverride None ↓ AllowOverride All : </Directory>
- コメントを外し.htaccessを有効にする ★homeエリア開放時
#<Directory /home/*/public_html> # AllowOverride FileInfo AuthConfig Limit # Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec # <Limit GET POST OPTIONS> # Order allow,deny # Allow from all # </Limit> # <LimitExcept GET POST OPTIONS> # Order deny,allow # Deny from all # </LimitExcept> #</Directory>
↓(コメントを外す)<Directory /home/*/public_html> AllowOverride FileInfo AuthConfig Limit Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec <Limit GET POST OPTIONS> Order allow,deny Allow from all </Limit> <LimitExcept GET POST OPTIONS> Order deny,allow Deny from all </LimitExcept> </Directory>
- 設定ファイル再ロードにより有効化
# /etc/rc.d/init.d/httpd reload
httpd を再読み込み中: [ OK ] #
(2)ログインユーザID登録
- 「hogehoge」IDを登録(初回)
# htpasswd -b -c /etc/httpd/conf/.htpasswd hogehoge (パスワード)
Adding password for user hogehoge
- .htpasswdが出来、hogehogeが登録されていることを確認
# cat /etc/httpd/conf/.htpasswd
hogehoge:2wNVqh.HJqyiA
- 「testusr」IDを登録(追加)
# htpasswd -b /etc/httpd/conf/.htpasswd testusr (パスワード)
Adding password for user testusr
- testusrが追加されていることを確認
# cat /etc/httpd/conf/.htpasswd
hogehoge:2wNVqh.HJqyiA testusr:AOQiT7rF/lYzk
- 終了
# exit exit $
(3)ホームページパスワード制限設定
http://192.168.0.10/~hogehoge/ をパスワード制限にする
- .htaccess 作成
$ vi public_html/.htaccess
AuthUserFile /etc/httpd/conf/.htpasswd AuthGroupFile /dev/null AuthName "hogehoge Page" AuthType Basic require valid-user
(意味)
AuthUserFile /etc/httpd/conf/.htpasswd ← ログインIDファイル指定(フルパス)
AuthGroupFile /dev/null ←グループ接続拒否
AuthName "hogehoge Page" ← ログインダイアログ表示
AuthType Basic ← Basic認証指定
require valid-user ← 登録全IDでのログイン許可
- ファイルが出来ているか確認
$ ls -al public_html/
-rw-rw-r-- 1 hogehoge hogehoge 122 4月 13 21:42 .htaccess
- アクセス権限付与
$ chmod 604 public_html/.htaccess
- 権限確認
$ ls -al public_html/
-rw----r-- 1 hogehoge hogehoge 122 4月 13 21:42 .htaccess
(4) 動作試験
他のPCから以下URLアクセス
・ http://192.168.0.10/~hogehoge/ → Password要求
[fugafuga]でログイン ・・・・・・ × ログイン不可
[hogehoge][パスワード誤]・・ × ログイン不可
[hogehoge][パスワード正]・・ ○ ログインOK
★上記のとおりアクセスできればOK
遭遇したトラブル
http://(サーバアドレス)/~hogehoge/ アクセス時、パスワード制限が掛からない
問題:
http://(サーバアドレス)/~hogehoge/ アクセス時、パスワード制限が掛からない
原因:
<Directory /home/*/public_html> : </Directory>
のコメントはずし忘れ
謝辞
テキスト作成に当たり、以下サイトを参考にさせて頂きました。ありがとうございます。
http://centossrv.com/apache-htpasswd.shtml
http://httpd.apache.org/docs/2.2/ja/mod/core.html#allowoverride
http://www.shtml.jp/htaccess/basic.html