Webサイトを特定の人にだけ公開したい場合、ユーザー名とパスワードによる認証システムを構築することができる。今回はデジカメ写真を配布するのに認証システムを使ってみた。デジカメ写真も1枚当たり数百kBにもなるとメール添付というわけにもいかず、さりとてリンクなしでWeb内に置いてもファイル名がわかれば誰にでも見えてしまう。認証システムがあると少しは安心できることになる。
認証システムの構築方法には「.htaccess」を使用する方法とした。apacheのバージョンはapache.1.3.27である。先ずhttpd.confの設定状況を確認する。apacheはほとんどデフォルトで使用しているので、手直しする箇所は1箇所だけである。
# AccessFileName: The name of the file to look for in each
directory
# for access control information.
#
AccessFileName .htaccess
となっていることを確認する。これは「.htaccess」ファイルによる認証システムが有効になっていることを意味している。
# The following lines prevent .htaccess files from being viewed
by
# Web clients. Since .htaccess files often contain authorization
# information, access is disallowed for security reasons. Comment
# these lines out if you want Web visitors to see the contents of
# .htaccess files. If you change the AccessFileName directive
above,
# be sure to make the corresponding changes here.
#
# Also, folks tend to use names such as .htpasswd for password
# files, so this will protect those as well.
#
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>
の設定より、「.htaccess」ファイルへの接続が禁止されている必要がある。以上の設定はデフォルトのままで確認だけでOKのはずである。
次に
# This controls which options the .htaccess files in directories
can
# override. Can also be "All", or any combination of
"Options", "FileInfo",
# "AuthConfig", and "Limit"
#
AllowOverride None
の箇所を探す。例にあるようにここはNoneからAuthConfigに変更する。
AllowOverride AuthConfig
以上でhttpd.confの設定は終了である。設定を有効にするためapacheを再スタートする。
パスワード認証をしたいディレクトリーに「.htaccess」ファイルを設置する必要がある。このファイルはviで作成する。
%vi .htaccess
AuthUserFile /home/hoge/.htpasswd
AuthGroupFile /dev/null
AuthName "Please Enter Your Password"
AuthType Basic
<limit GET>
require valid-user
</limit>
1行目はパスワードファイルの置き場所、3行目は認証時に表示される文字列、6行目は認証可能なユーザー名であり、valid-userとすると.htpasswdに記載されている全てのユーザーとなる。このファイルはapacheユーザーで作成し、chmod
644とする。
ユーザ、パスワードの登録は、Apache付属のhtpasswdコマンドを使う。先ず、htpasswdの場所を探す。
%locate htpasswd
/usr/local/etc/apache/bin/htpasswd
最初はユーザーとパスワードを一緒に作成する。
%/usr/local/etc/apache/bin/htpasswd -c /home/hoge/.htpasswd
hogehoge
とすると/home/hogeに「.htpasswd」が自動作成され、ユーザーhogehogeが登録される。パスワードを要求されるので2回タイプする。
ユーザーを追加する場合は -c
オプションを外す。-c
オプションをつけたままだと以前のユーザー全てがクリアーされてしまう。
%/usr/local/etc/apache/bin/htpasswd /home/hoge/.htpasswd
hogehogehoge
「.htpasswd」ファイルもapacheユーザーで作成しchmod
644としておく。
httpd.confを変更した場合は、apacheを再起動しておかないと有効にならない。「.htpasswd」ファイルの変更ではapacheの再起動は不要である。「.htaccess」を置いたディレクトリーにデジカメ写真を格納しファイル名を指定してアクセスすると無事認証システムが機能した。