Webページパスワード制限(htpasswd)

アクセス時にパスワードで制限を掛けます。

1.WEBサーバー設定ファイル編集

[root@miracle ~]# vi /etc/httpd/conf/httpd.conf
# 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
#
    AllowOverride All ← Allにする(.htaccessを許可)

2..htpasswdファイル作成

①.htpasswdファイルを新規作成する場合

[root@miracle ~]# htpasswd -b -c -m /etc/httpd/conf/.htpasswd neko [パスワード]
Adding password for user neko

②既存の.htpasswdファイルへユーザーを追加する場合

[root@miracle ~]# htpasswd -b -m /etc/httpd/conf/.htpasswd neko [パスワード]
Adding password for user neko

③ユーザー登録確認

[root@miracle ~]# cat /etc/httpd/conf/.htpasswd
neko:$apr1$A8qNnqfK$OaJAcZqhQXR8IviSznFuE/

3.Webページパスワード制限確認

①テスト用ディレクトリ、ページ作成

[root@miracle ~]# mkdir /var/www/html/miracle-serv.website/secret ← テスト用ディレクトリ作成

[root@miracle ~]# echo test > /var/www/html/miracle-serv.website/secret/index.html ← テスト用ページ作成

②.htaccessファイル作成

[root@miracle ~]# vi /var/www/html/miracle-serv.website/secret/.htaccess ← テスト用ディレクトリに.htaccess作成
SSLRequireSSL 
AuthUserFile /etc/httpd/conf/.htpasswd 
AuthGroupFile /dev/null 
AuthName "secret page" 
AuthType Basic 
require valid-user

③ユーザー認証確認

https://サーバー名/secret/にアクセスして、表示されるユーザー認証画面に.htpasswdファイルへ登録したユーザー名、パスワードを入力してテストページが表示されればOK!

④Webページパスワード制限確認後始末

[root@miracle ~]# rm -rf /var/www/html/miracle-serv.website/secret/ ← テスト用ディレクトリ削除

[root@miracle ~]# rm -f /etc/httpd/conf/.htpasswd ← テスト用.htpasswd削除

コメント

タイトルとURLをコピーしました