Webサーバ Apache アクセス制限(パスワード)
ホームページアクセス時にパスワードによりアクセス制限を行う
やりたいこと
ホームページアクセス時に、ログイン名とパスワードを表示、入力が正しい場合のみページの表示を行う。
条件
使用OS | CentOS5.5 64bit |
アプリ | Apache+PHP+MySQL (CentOS付属) |
文字コード | utf-8 |
設定
(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を有効にする
#<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 123 7月 25 02:08 .htaccess
- アクセス権限付与
$ chmod 604 public_html/.htaccess
- 権限確認
$ ls -al public_html/ -rw----r-- 1 hogehoge hogehoge 123 7月 25 02:08 .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