BASIC認証によるアクセス制限
BASIC認証(.htpasswdによるユーザー名、パスワードでの認証)によるアクセス制限を設定します。
特殊ファイル".htaccess"の有効化
まず、ディレクトリごとにApacheの設定を変更できる.htaccessを使用できるように設定します。
[root@centos ~] vi /etc/httpd/conf/httpd.conf ※apacheの設定ファイルをテキストエディタで開く# # 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.2/mod/core.html#options # for more information. # Options ExecCGI FollowSymLinks Includes # # 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 None ↓ AllowOverride ALL ※ディレクトリごとにApacheの設定を変更できる「.htaccess」を使用できるようにする。 # # Controls who can get stuff from this server. # Order allow,deny Allow from all [root@centos ~] /etc/rc.d/init.d/httpd reload ※設定を反映
.htpasswdファイルの作成
以下のコマンドで認証アカウントの情報を記録する".htpasswd"ファイルを新規作成します。
これは"/etc/httpd/conf/.htpasswd"というパスで.htpasswdファイルを新規作成し、ユーザ名を"user1"、パスワードを"password4user1"とする場合です。
[root@centos ~] htpasswd -b -c /etc/httpd/conf/.htpasswd user1 password4user1
また、既存の.htpasswdファイルへユーザーを追加する場合は下記のコマンドを実行します。
上記のコマンドだとファイルが再生成されてしまい、既存の設定もクリアされてしまうので注意が必要です。
[root@centos ~] htpasswd -b /etc/httpd/conf/.htpasswd user1 password4user1
既に設定したアカウントを削除する場合は下記のコマンドを実行します。
[root@centos ~] htpasswd -D /etc/httpd/conf/.htpasswd user1
.htaccessを編集
設定したアカウントでBASIC認証を行えるように.htaccessに以下のように追記します。
※ここでは"/var/www/html"ディレクトリ直下に.htaccessを配置することを想定して設定しています。
[root@centos ~] vi /var/www/html/.htaccess
# # # # # #
# Basic認証
AuthType Basic
AuthName "ここに認証ダイアログに表示するメッセージを記載"
AuthUserFile /etc/httpd/conf/.htpasswd
Require valid-user
最終更新 2009-03-26

この記事へのコメント
まだコメントは投稿されていません。