ApacheのBasic認証をDB(MySQL)で実施していみた。
手順をメモメモ。
1.認証に利用するテーブル作成
認証に利用するDB・テーブルを作成します。
例)
1 2 3 4 5 6 7 |
CREATE DATABASE db_auth; USE db_auth; CREATE TABLE users ( user_id varchar(15) NOT NULL, password varchar(15) NOT NULL, PRIMARY KEY (user_id) ); |
作成したテーブルにデータを登録します。
1 |
INSERT INTO users VALUES ('test', 'testpass'); |
2.Apacheの認証設定
以下コマンドで「mod_auth_mysql」をインストールします。
1 |
[root@localhost ~]# yum install mod_auth_mysql |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
epel | 4.3 kB 00:00 epel/primary_db | 5.9 MB 00:36 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package mod_auth_mysql.x86_64 1:3.0.0-11.el6_0.1 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: mod_auth_mysql x86_64 1:3.0.0-11.el6_0.1 base 24 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 24 k Installed size: 49 k Is this ok [y/N]: y Downloading Packages: mod_auth_mysql-3.0.0-11.el6_0.1.x86_64.rpm | 24 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : 1:mod_auth_mysql-3.0.0-11.el6_0.1.x86_64 1/1 Verifying : 1:mod_auth_mysql-3.0.0-11.el6_0.1.x86_64 1/1 Installed: mod_auth_mysql.x86_64 1:3.0.0-11.el6_0.1 Complete! [root@localhost ~]# |
インストールできました。
以下ファイルを編集します。
・auth_mysql.conf
1 2 |
[root@localhost ~]# cd /etc/httpd/conf.d/ [root@localhost conf.d]# vi auth_mysql.conf |
ファイルの末に以下を追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<Directory "/var/www/html"> AuthName "Authenticated zone" AuthType Basic AuthMySQLEnable on AuthMySQLUser root AuthMySQLPassword pass! AuthMySQLDB db_auth AuthMySQLUserTable users AuthMySQLNameField user_id AuthMySQLPasswordField password AuthMySQLPwEncryption none Require valid-user </Directory> |
Apacheを再起動します。
1 2 3 |
[root@localhost conf.d]# service httpd restart httpd を停止中: [ OK ] httpd を起動中: [ OK ] |
3.動作確認
以下URLにアクセスすると、ユーザー認証のダイアログが表示されます。
・http://{ホスト名}/
ログインできれば成功です。
以上です。