概要:Webページへのアクセスにhtpasswdによりユーザー名とパスワードによるアクセス制限をかける。
ここでは、https://hostname.com/protect/以下のWebページへユーザー名(centosとする)とパスワード(passwdとする)によるアクセス制限をかけるようにする。
また、SSL経由でしかアクセスできないようにする。
前提:
1. Webサーバー設定
# vi /etc/httpd/conf/httpd.conf ← Webサーバー設定ファイル編集
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs-2.0/mod/core.html#options
# for more information.
#
Options Includes ExecCGI FollowSymLinks
#
# 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を許可)
# /etc/rc.d/init.d/httpd reload ← Webサーバー設定反映
httpd を再読み込み中: [ OK ]
2. .htpasswdファイル作成
- ) .htpasswdファイルを新規作成する場合
# htpasswd -b -c /etc/httpd/conf/.htpasswd centos centospassos
← .htpasswdを作成してユーザーcentosを登録する
Adding password for user centos
- ) 既存の.htpasswdファイルへユーザーを追加する場合
# htpasswd -b /etc/httpd/conf/.htpasswd centos passwd
← 既存の.htpasswdへユーザーcentosを登録する
Adding password for user centos
- ) ユーザー登録確認
# cat /etc/httpd/conf/.htpasswd ← ユーザー登録確認
centos:vYwnFfo59lI/c
3. Webページパスワード制限確認
- ) テスト用ディレクトリ、ページ作成
# mkdir /var/www/html/protect ← テスト用ディレクトリ作成
# echo test > /var/www/html/protect/index.html ← テスト用ページ作成
- ) .htaccessファイル作成
.htpasswdに登録してある全てのユーザー名で認証できるようにする場合
# vi /var/www/html/protect/.htaccess ← テスト用ディレクトリに.htaccess作成
SSLRequireSSL
AuthUserFile /etc/httpd/conf/.htpasswd
AuthGroupFile /dev/null
AuthName "protected page"
AuthType Basic
require valid-user
- ) .htaccessファイル作成
.htpasswdに登録してある特定のユーザー名(ここでは、認証を許可するユーザー名をcentosとする)でのみ認証できるようにする場合
# vi /var/www/html/protect/.htaccess ← テスト用ディレクトリに.htaccess作成
SSLRequireSSL
AuthUserFile /etc/httpd/conf/.htpasswd
AuthGroupFile /dev/null
AuthName "protected page"
AuthType Basic
require user centos ← 認証を許可するユーザー名を指定
- ) ユーザー認証確認
・https://サーバー名/protect/にアクセスしてユーザー認証画面が表示され、ユーザー名、パスワードに.htpasswdファイルへ登録したユーザー名、パスワードを入力してテストページが表示されることを確認。
https://サーバー名/protect/
・非SSL(http://サーバー名/protect/)でアクセスした場合、ユーザー認証画面が表示されずにエラーとなることを確認。
・htpasswd内の特定ユーザー名のみ認証を許可している場合、特定ユーザー以外ではエラーとなることを確認。
- ) Webページパスワード制限確認後始末
# rm -rf /var/www/html/protect/ ← テスト用ディレクトリ削除
# rm -f /etc/httpd/conf/.htpasswd ← テスト用.htpasswd削除