2006年03月31日 - apr

mod_dbd

apache 2.2の新機能にSQL Database Supportつーのがある。RDBMSとの接続をhttpdがプールしておき、モジュールやCGIプログラムは接続をもらって利用することができるようになる。はず。 ということなので、とりあえずそのへんで遊んでみることに。
  • OS: FreeBSD 7.0-CURRENT #2: Thu Mar 23 12:41:17 JST 2006
  • RDBMS: PostgreSQL 8.1.3 (/home/pgsql81にインストール)
  • httpd, apr, apr-utilはHEADなもの
HEADなhttpdのビルドをした時にPostgreSQLとのリンクなんかもできてるのかなと思ったらそうでもないようだ。PostgreSQLは諸事情で8.0.3のクライアントをportでインストールしてあり、/usr/local/lib/libpsq.soとかあるので勝手にそれを使ってくれても良さそうなのだが使ってくれてないみたい。mod_dbdはapr-util/dbd/ 以下にあるものを利用するので、そのあたりからやり直し。apr-utilのconfigureのオプションを見てみると
$ configure --help `configure' configures this package to adapt to many kinds of systems. Usage: ./configure [OPTION]... [VAR=VALUE]... ・・・・・・・・ ・・・・・・・・ --with-pgsql=DIR specify PostgreSQL location --with-mysql=DIR **** SEE INSTALL.MySQL **** --with-sqlite3=DIR --with-sqlite2=DIR --with-oracle=DIR specify ORACLE_HOME location
とある。ここで使いたいRDBMSを指定するのね。
$ ./configure --prefix=/home/apache-trunk --with-apr=/home/apache-trunk --with-pgsql=/home/pgsql81 \ --target=x86_64--freebsd7.0 ・・・・・・・・ configure: checking for pgsql in /home/pgsql81 checking libpq-fe.h usability... yes checking libpq-fe.h presence... yes checking for libpq-fe.h... yes checking for PQsendQueryPrepared in -lpq... yes setting APRUTIL_LDFLAGS to "-L/home/pgsql81/lib" setting APRUTIL_INCLUDES to "-I/home/pgsql81/include" setting APRUTIL_EXPORT_LIBS to "-lpq" setting APRUTIL_LIBS to "-lpq" ・・・・・・・・ $ make && sudo make install
あとはhttpdをフツーにconfigureすれば
$ ./configure --prefix=/home/apache-trunk --with-apr=/home/apache-trunk \ --with-apr-util=/home/apache-trunk --with-mpm=worker --enable-so --with-port=80 \ --enable-mods-shared="all ssl rewrite info usertrack unique-id" --build=amd64--freebsd7.0 ・・・・・・・・ checking for APR-util... yes adding "-L/home/pgsql81/lib" to LDFLAGS ・・・・・・・・ $
と、PostgreSQLのライブラリがあるディレクトリがLDFLAGSに追加される。makeしてmake installしておわり。 たしかにlibpqがリンクされている:
$ ldd /home/apache-trunk/bin/httpd /home/apache-trunk/bin/httpd: libm.so.4 => /lib/libm.so.4 (0x80068f000) libaprutil-1.so.3 => /home/apache-trunk/lib/libaprutil-1.so.3 (0x8007ab000) libpq.so.4 => /home/pgsql81/lib/libpq.so.4 (0x8008c6000) libexpat.so.6 => /usr/local/lib/libexpat.so.6 (0x8009e2000) libapr-1.so.3 => /home/apache-trunk/lib/libapr-1.so.3 (0x800b04000) libcrypt.so.3 => /lib/libcrypt.so.3 (0x800c2b000) libpthread.so.2 => /usr/lib/libpthread.so.2 (0x800d44000) libc.so.6 => /lib/libc.so.6 (0x800e70000) $
mod_dbdのマニュアルを読むと、データベースドライバーを指定するディレクティブはDBDriver、接続時に必要なパラメータ(データベース名とかユーザ名、パスワードなど)DBDParamsで指定するようなので、httpd.confに
LoadModule dbd_module modules/mod_dbd.so <IfModule dbd_module> DBDriver pgsql DBDParams dbname=postgres,user=pgsql </IfModule>
と書いてapacheを動かしてみる。
$ sudo /home/apache-trunk/bin/apachectl start $
ぜんぜん問題なく動いた。ように見えただけ、実は:
tail /home/apache-trunk/logs/error_log [Fri Mar 31 17:50:39 2006] [notice] Apache/2.3.0-dev (Unix) mod_ssl/2.3.0-dev OpenSSL/0.9.7e DAV/2 configured [Fri Mar 31 17:50:39 2006] [crit] (20014)Internal error: DBD: Can't connect to pgsql [Fri Mar 31 17:50:39 2006] [crit] (20014)Internal error: DBD: failed to initialise [Fri Mar 31 17:50:39 2006] [crit] (20014)Internal error: DBD: Can't connect to pgsql [Fri Mar 31 17:50:39 2006] [crit] (20014)Internal error: DBD: failed to initialise
mod_dbdさんがエラー出してる。pgsqlに接続できない、と言っているということは、DBDriverディレクティブはたぶんうまく効いているということ。じゃあDBDParamsが悪いのだろうか? マニュアルには、
DBDParams param1=value1[,param2=value2]
と書いてあるので特に問題はなさそう。うーん・・・ソースを見てみますか。httpd-trunk/modules/database/mod_dbd.cのdbd_construct関数の中の
rv = apr_dbd_open(rec->driver, rec->pool, svr->params, &rec->handle); switch (rv) { case APR_EGENERAL: ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool, "DBD: Can't connect to %s", svr->name); return rv; default: ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, rec->pool, "DBD: mod_dbd not compatible with apr in open"); return rv; case APR_SUCCESS: break; }
というあたりがそれっぽい。apr_dbd_openはapr-utilの関数なので、srclib/apr-util/dbd/apr_dbd.cを見てみると
APU_DECLARE(apr_status_t) apr_dbd_open(const apr_dbd_driver_t *driver, apr_pool_t *pool, const char *params, apr_dbd_t **handle) { apr_status_t rv; *handle = driver->open(pool, params); if (*handle == NULL) { return APR_EGENERAL; } rv = apr_dbd_check_conn(driver, pool, *handle); if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) { apr_dbd_close(driver, *handle); return APR_EGENERAL; } return APR_SUCCESS; }
単に引数で与えられたdriverのオープン関数を呼んでるだけ。ということでドライバーであるsrclib/apr-util/dbd/apr_dbd_pgsql.cを見てみる
static apr_dbd_t *dbd_pgsql_open(apr_pool_t *pool, const char *params) { apr_dbd_t *sql; PGconn *conn = PQconnectdb(params); /* if there's an error in the connect string or something we get * back a * bogus connection object, and things like PQreset are * liable to segfault, so just close it out now. it would be nice * if we could give an indication of why we failed to connect... */ if (PQstatus(conn) != CONNECTION_OK) { PQfinish(conn); return NULL; } sql = apr_pcalloc (pool, sizeof (*sql)); sql->conn = conn; return sql; }
ななな、なんと! paramsをPQconnectdbにそのまま渡しているだけ。PQconnectdbの使い方は、マニュアルによると
The passed string can be empty to use all default parameters, or it can contain one or more parameter settings separated by whitespace.
つまり、「key=value」なやつをホワイトスペースで区切って並べる。ってことは、httpd.confでDBDParamsの値を「dbname=postgres,user=pgsql」としたのは間違いで
DBDParams "dbname=postgresql user=pgsql"
とすればいいのかな? これでhttpdを動かしてみたところ、
$ tail /home/apache-trunk/logs/error_log [Fri Mar 31 18:38:58 2006] [notice] Digest: generating secret for digest authentication ... [Fri Mar 31 18:38:58 2006] [notice] Digest: done [Fri Mar 31 18:38:58 2006] [notice] Apache/2.3.0-dev (Unix) mod_ssl/2.3.0-dev OpenSSL/0.9.7e DAV/2 $
エラーは出なくなった。実際、
$ scokstat pgsql postgres 48557 7 stream /tmp/.s.PGSQL.5432 pgsql postgres 48556 7 stream /tmp/.s.PGSQL.5432 www httpd 48555 13 stream -> /tmp/.s.PGSQL.5432 www httpd 48554 13 stream -> /tmp/.s.PGSQL.5432
と、繋がってるもよう。うまくいったなあ。でもmod_dbdのマニュアルは間違いだろう? もしくは、apr_dbd_pgsql.cがサボりすぎか? 他のデータベースドライバーの場合はDBDParamsの値をパースしてそれなりなことをやっているようなので、やっぱりapr_dbd_pgsql.cはサボりすぎだろう。あとでパッチ作って送るかな。
posted at 13:58 | permalink | Comments (0) | TrackBack (0)

2006年03月23日 - httpd

HEADなhttpdのビルド

とても久しぶりにHEADなhttpdをビルドしてみた。
  • BOX: AMD Opteron 1.6GHz x 2, 1GB RAM
  • OS: FreeBSD 7.0-CURRENT #2: Thu Mar 23 12:41:17 JST 2006
  • BUILD TOOLS: autoconf-2.59_2, libtool-1.5.22_2, python=2.4.2
まずはソースをチェックアウトしてconfigureを作る:
$ svn checkout http://svn.apache.org/repos/asf/httpd/httpd/trunk httpd-trunk $ cd httpd-trunk $ AUTOCONF=autoconf259 AUTOHEADER=autoheader259 ./buildconf You don't have a copy of the apr source in srclib/apr. Please get the source using the following instructions, or specify the location of the source with --with-apr=[path to apr] : svn co http://svn.apache.org/repos/asf/apr/apr/trunk srclib/apr You don't have a copy of the apr-util source in srclib/apr-util. Please get one the source using the following instructions, or specify the location of the source with --with-apr-util=[path to apr-util]: svn co http://svn.apache.org/repos/asf/apr/apr-util/trunk srclib/apr-util $
おっとっと。aprとapr-utilを取ってきてもう一度buildconf:
$ svn checkout http://svn.apache.org/repos/asf/apr/apr/trunk srclib/apr $ svn checkout http://svn.apache.org/repos/asf/apr/apr-util/trunk srclib/apr-util $ AUTOCONF=autoconf259 AUTOHEADER=autoheader259 ./buildconf found apr source: srclib/apr found apr-util source: srclib/apr-util rebuilding srclib/apr/configure buildconf: checking installation... buildconf: python version 2.4.2 (ok) buildconf: autoconf version 2.59 (ok) buildconf: libtool version 1.5.22 (ok) ・・・・・・・・ fixing timestamps for mod_ssl sources $
configureができたのでテキトーにオプションをつけて実行:
$ ./configure --prefix=/home/apache-trunk --with-mpm=worker --enable-so --with-port=80 \ --enable-mods-shared="all ssl rewrite info usertrack unique-id" --build=x86_64--freebsd7.0 checking for chosen layout... Apache checking for working mkdir -p... yes checking build system type... x86_64--freebsd7.0 ・・・・・・・・ config.status: include/ap_config_auto.h is unchanged config.status: executing default commands $
無事Makefileもできたと思いきや! configureの出力をよく見てみると、、、
adding "-I/usr/local/include/apr-1" to INCLUDES
せっかくaprとapr-utilのHEADをcheckoutしたのにそれ使ってくれず、portで入れてあるapr/apr-utilを使うようにconfigureされているモヨウ。buildconfはsrclib/aprとsrclib/apr-utilがないと(もしくは--with-apr/--with-apr-utilでソースのある場所を指定しないと)文句を言うくせに、configureは/usr/localにインストールされてるapr/apr-utilを使うとは、どういうこっちゃ!? apr/apr-utilもHEADなやつを使いたい場合、まずはapr, apr-utilをテキトーなところ(ここでは/home/apache-trunk)へインストールして、httpdのconfigureにはそいつを教えてやらないとダメみたい:
$ ./configure --prefix=/home/apache-trunk --with-apr=/home/apache-trunk \ --with-apr-util=/home/apache-trunk --with-mpm=worker --enable-so --with-port=80 \ --enable-mods-shared="all ssl rewrite info usertrack unique-id" --build=x86_64--freebsd7.0 ・・・・・・・・ adding "-I/home/apache-trunk/include/apr-1" to INCLUDES ・・・・・・・・ $
今度はうまくいったのでmake&&make installしてめでたしめでたし。
posted at 16:42 | permalink | Comments (0) | TrackBack (0)