Gentoo + Apache2 + mod-auth-mysql (Intro)
たまには、技術ネタでもいれておくことにしましょう。
とある事情で、Gentoo + Apache2 + mod-auth-mysql な環境を作ることになりました。
が、いろいろハマったので、いろいろメモしておきます。
Gentoo + Apache2 + mod-auth-mysql (install)
まずは、インストール。
ここは序の口で、誰でも通れる道ですが、mod-auth-mysql が Mask されているので、
# vi /etc/portage/package.keyword
www-apache/mod-auth-mysql ~x86
こうやってマスクを外して
# emerge www-servers/apache www-apache/mod-auth-mysql
* www-servers/apache
Latest version available: 2.2.16
Latest version installed: 2.2.16
Size of files: 4,725 kB
Homepage: http://httpd.apache.org/
Description: The Apache Web Server.
License: Apache-2.0 Apache-1.1
* www-apache/mod-auth-mysql
Latest version available: 4.3.9
Latest version installed: 4.3.9
Size of files: 67 kB
Homepage: http://packages.debian.org/source/mod-auth-mysql
Description: A module for the Apache 2 web server which enables HTTP authentication against information stored in a MySQL database.
License: Apache-1.1
こう emerge してインストールしました。
# これで、apache 起動すればいいよね、と思っていたのがバカでした。
Gentoo + Apache2 + mod-auth-mysql (module config)
Mask されているだけあってか、そのまま起動しても mod-auth-mysql が読み込まれません。
mod-auth-mysql の設定ファイルを見てみると、
$ cat /etc/apache2/modules.d/12_mod_auth_mysql.conf
<IfDefine AUTH_MYSQL>
LoadModule apache2_mysql_auth_module modules/apache2_mod_auth_mysql.so
(snip...)
</IfDefine>
AUTH_MYSQL がどっかで定義されてないとモジュールが読み込まれなくなってます。
が、こいつ、インストールするだけでは実はどこにも定義されません。
仕方なく、Gentoo の流儀を調べてみると、
# vi /etc/conf.d/apache2
APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D SSL -D SSL_DEFAULT_VHOST -D LANGUAGE -D AUTH_MYSQL"
と、/etc/conf.d/apache2 に “-D AUTH_MYSQL” を入れるのが筋らしいので、こいつに追加して apache 起動。
# /etc/init.d/apache2 start
* apache2 has detected an error in your setup:
apache2: Syntax error on line 155 of /etc/apache2/httpd.conf: Syntax error on line 2 of /etc/apache2/modules.d/12_mod_auth_mysql.conf: Can't locate API module structure `apache2_mysql_auth_module' in file /usr/lib/apache2/modules/apache2_mod_auth_mysql.so: /usr/lib/apache2/modules/apache2_mod_auth_mysql.so: undefined symbol: apache2_mysql_auth_module
…….
もちろん、/usr/lib/apache2/modules/apache2_mod_auth_mysql.so はちゃんとあります。
とすると、何が原因なんだろうなあ、、、と悩んで、ダメモトでモジュールを見てみました。
$ less /usr/lib/apache2/modules/apache2_mode_auth_mysql.so
(snip...)
Symbol table '.dynsym' contains 64 entries:
51: 00006160 56 OBJECT GLOBAL DEFAULT 21 auth_mysql_module
auth_mysql_module….
もしかして、コイツか? ということで、さっきの設定ファイルを書き換えてみました。
# vi /etc/apache2/modules.d/12_mod_auth_mysql.conf
<IfDefine AUTH_MYSQL>
# LoadModule apache2_mysql_auth_module modules/apache2_mod_auth_mysql.so
LoadModule auth_mysql_module modules/apache2_mod_auth_mysql.so
(snip...)
</IfDefine>
そして、起動してみると、
# /etc/init.d/apache2 start
* Starting apache2 ... [ok]
となり起動しました。
よし、後は DB 連携の設定だけだ!
Gentoo + Apache2 + mod-auth-mysql (DB config)
“よし、後は DB 連携の設定だけだ!” なんて言ってごめんなさい。
/etc/apache2/modules.d/12_mod_auth_mysql.conf を参考に
<Directory /foo>
AuthBasicAuthoritative Off
AuthMySQLEnable on
AuthMySQLHost localhost
AuthMySQLUser user
AuthMySQLPassword password
AuthMySQLDB DB
AuthMySQLUserTable Authentication
AuthMySQLNameField Name
AuthMySQLPasswordField Password
AuthMySQLPwEncryption sha1
AuthType Basic
</Directory>
とか書いて restart してみたのですが、
# /etc/init.d/apache2 start
* Stoppping apache2 ... [ok]
* apache2 has detected an error in your setup:
Invalid command 'AuthMySQLEnable', perhaps misspelled or defined by a module not included in the server configuration
とか出ました…
設定例をそのままコピーしてきてるようなものなのに、ダメと言われる…
スペルミスだってしてないし、ダメなはずはない…
ここで悩むこと10分…
で、気分転換もかねて一度ちゃんとドキュメント読んでみたら、解決しました。
$ less /usr/share/doc/mod-auth-mysql-4.3.9/DIRECTIVES.bz2
Auth_MySQL
Enable/disable MySQL authentication
(snip...)
Auth_MySQL_Host
Synonym for Auth_MySQL_DefaultHost, to be used in .htaccess files and directory-specific entries.
(snip...)
……..
……..
……..
設定例とドキュメントで項目名違うじゃなイカ!!!!
ドキュメントにそって設定しなおしたら動きました….
Mask されてるとはいえ、こんなハマり方は初めてでした….
Gentoo + Apache2 + mod-auth-mysql (summary)
- インストール
# vi /etc/portage/package.keyword
www-apache/mod-auth-mysql ~x86
# emerge www-servers/apache www-apache/mod-auth-mysql
- Apache 設定
# vi /etc/conf.d/apache2
APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D SSL -D SSL_DEFAULT_VHOST -D LANGUAGE -D AUTH_MYSQL"
# vi /etc/apache2/modules.d/12_mod_auth_mysql.conf
<IfDefine AUTH_MYSQL>
# LoadModule apache2_mysql_auth_module modules/apache2_mod_auth_mysql.so
LoadModule auth_mysql_module modules/apache2_mod_auth_mysql.so
(snip...)
</IfDefine>
- MySQL DB 連携設定
# vi (your virtual host configuration file)
<Directory /foo>
AuthBasicAuthoritative Off
Auth_MySQL on
Auth_MySQL_Host localhost
Auth_MySQL_User user
Auth_MySQL_Password password
Auth_MySQL_DB Database
Auth_MySQL_Password_Table Table
Auth_MySQL_UserName_Field Name
Auth_MySQL_Password_Field Password
Auth_MySQL_Encryption_Types SHA1Sum
Auth_MySQL_Empty_Passwords Off
AuthGroupFile /dev/null
AuthName "MySQL Auth"
AuthType "Basic"
require valid-user
</Directory>
- Apache 起動
# /etc/init.d/apache2