roundcubeとPostgreSQL
2012/08/18(土) 8:21 NetBSD はてブ情報 はてブに登録 はてブ数

メールサーバを新しくして、ちょっと調子に乗りました。

外からもメールが読めると便利だなぁと。

もちろんIMAPのポートを空けるのも考えたのですが、気軽に読めるようにWebメールを入れることにしました。

以前WebメールにSquirrelMailを使ったことがあったのですが、すこしWebをさすらってroundcubeというのを見つけました。pkgsrcからだとpkgsrc/mail/roundcubeを入れると一式入れてくれるようですね。バージョンは少し古いようですが、なにより簡単にはじめられるのでバージョンにはこだわらずにpkgsrcのお世話になることにしました。

roundcubeは今風のアプリケーションらしくDBMSを使うようですね。デフォルトだとMySQLをデータベースとして使うようなのですが、なぜかPostgreSQLを使わなければならないような気がしたのでPostgreSQLを使うことにしました(どうやらこの判断のおかげでずいぶんと時間がかかったようですけど)。

ということで、pkgsrc/database/postgresql91を入れておきます。

PostgreSQLを使う場合、PHPのPostgreSQL用ドライバもあわせて必要なのでpkgsrc/databases/pear-MDB2_Driver_pgsqlもインストールしておきます。これに気づかず、データベースに接続できないというエラーに悩まされました。

そもそもNetBSDでPostgreSQLを使うのが初めてで、データベースクラスタとかどこに作るのかなー、と思っていましたが、起動スクリプトをのぞいてみると、初めて起動した際に、自動的にいろいろやってくれるようです。このへんも、pkgsrcの恩恵を受けています。ありがたい。

ということで、/usr/pkg/example/rc.d/pgsqlを/etc/rc.dにコピー、/etc/rc.d/pgsql onestart でPostgreSQLを起動すると、/usr/pkg/pgsqlにデータベースクラスタを作成してくれました。

/etc/passwdに新しく追加されているユーザ名から想像するとPostgreSQLの特権ユーザはpgsqlというユーザ名のようです。このアカウントはUNIXとしてのログインが(ご丁寧に)禁止されているので、その後のPostgreSQL関連のコマンドを実行する際に-U pgsqlなどと明示的にpgsqlユーザで実行することを指定してあげる必要があります。

roundcubeを展開すると出てくるINSTALLファイルを読みつつ、データベースの初期設定を行います。

ユーザroundcubeを作ります。
root@zbox>createuser -U pgsql roundcube
新しいロールをスーパーユーザにしますか? (y/n)n
新しいロールに対してデータベースを作成する権限を与えますか? (y/n)y
新しいロールに対して別のロールを作成する権限を与えますか? (y/n)n
データベースを作ります。データベース名はroundcubemailです。
root@zbox>createdb -U pgsql -O roundcube -E UNICODE roundcubemail
psqlで接続詞、ユーザroundcubeのパスワードを設定した後、roundcubeが用意しているSQL文を流します。
root@zbox>psql -U pgsql roundcubemail
psql (9.1.2)
"help" でヘルプを表示します.

roundcubemail=# ALTER USER roundcube WITH PASSWORD 'XXX';
ALTER ROLE
roundcubemail=# \c - roundcube
データベース "roundcubemail" にユーザ"roundcube"として接続しました。
roundcubemail=> \i /usr/pkg/share/examples/roundcube/SQL/postgres.initial.sql

実行結果は省略

roundcubemail=> \q
DB接続の設定をします。

/usr/pkg/etc/roundcube/db.inc.phpが目的のファイルです。
PostgreSQLを使う場合には次のようにします。
$rcmail_config['db_dsnw'] = 'pgsql://roundcube:roundcube@localhost/roundcubemail';
これでroundcubeとPostgreSQLがつながりました。

あとは/usr/pkg/etc/roundcube/main.inc.phpをなおします。
といっても、メールサーバ名やポート番号を入れるだけです。
今回は、こんな感じ。
$rcmail_config['smtp_server'] = 'SMTPサーバ名';
$rcmail_config['smtp_port'] = 587;
$rcmail_config['smtp_user'] = 'ユーザ名';
$rcmail_config['smtp_pass'] = '%p';
次に、roundcubeを動かすためのPHPの設定です。
PHPの設定ファイル(/usr/pkg/etc/php.ini)を修正しました。
デバッグ用の設定がonになっているのは、格闘した名残なので不要ですかね。
root@zbox>diff -u php.ini.orig php.ini
--- php.ini.orig        2012-08-18 17:38:48.000000000 +0000
+++ php.ini     2012-08-18 21:35:28.000000000 +0000
@@ -528,7 +528,7 @@
 ; Development Value: On
 ; Production Value: Off
 ; http://php.net/display-errors
-display_errors = Off
+display_errors = On

 ; The display of errors which occur during PHP's startup sequence are handled
 ; separately from display_errors. PHP's default behavior is to suppress those
@@ -539,7 +539,7 @@
 ; Development Value: On
 ; Production Value: Off
 ; http://php.net/display-startup-errors
-display_startup_errors = Off
+display_startup_errors = On

 ; Besides displaying errors, PHP can also log errors to locations such as a
 ; server-specific log, STDERR, or a location specified by the error_log
@@ -769,7 +769,7 @@

 ; PHP's default character set is set to empty.
 ; http://php.net/default-charset
-;default_charset = "iso-8859-1"
+default_charset = "UTF-8"

 ; Always populate the $HTTP_RAW_POST_DATA variable. PHP's default behavior is
 ; to disable this feature.
@@ -971,6 +971,16 @@
 ;extension=php_pgsql.dll
 ;extension=php_pspell.dll
 ;extension=php_shmop.dll
+extension=dom.so
+extension=gd.so
+extension=iconv.so
+extension=json.so
+extension=mbstring.so
+extension=mcrypt.so
+extension=mysql.so
+extension=pgsql.so
+extension=sockets.so
+extension=sqlite.so

 ; The MIBS data available in the PHP distribution must be installed.
 ; See http://www.php.net/manual/en/snmp.installation.php
@@ -993,7 +1003,7 @@
 [Date]
 ; Defines the default timezone used by the date functions
 ; http://php.net/date.timezone
-;date.timezone =
+date.timezone = "Asia/Tokyo"

 ; http://php.net/date.default-latitude
 ;date.default_latitude = 31.7667
@@ -1756,22 +1766,22 @@
 [mbstring]
 ; language for internal character representation.
 ; http://php.net/mbstring.language
-;mbstring.language = Japanese
+mbstring.language = Japanese

 ; internal/script encoding.
 ; Some encoding cannot work as internal encoding.
 ; (e.g. SJIS, BIG5, ISO-2022-*)
 ; http://php.net/mbstring.internal-encoding
-;mbstring.internal_encoding = EUC-JP
+mbstring.internal_encoding = UTF-8

 ; http input encoding.
 ; http://php.net/mbstring.http-input
-;mbstring.http_input = auto
+mbstring.http_input = pass

 ; http output encoding. mb_output_handler must be
 ; registered as output buffer to function
 ; http://php.net/mbstring.http-output
-;mbstring.http_output = SJIS
+mbstring.http_output = pass

 ; enable automatic encoding translation according to
 ; mbstring.internal_encoding setting. Input chars are
PostgreSQLを使うroundcubeが依存するPHP、まで来ました。

あとはPHPを呼び出すApacheの設定です。

微妙にハマったのが/etc/rc.d/httpdでした。NetBSDには標準のHTTPデーモンがインストールされていて、その名もbozohttpdなんですが、/etc/rc.dにhttpdとかがシレっと存在し、あー、Apacheはインストールしてたから、これ起動すりゃいいんだよねー、という感覚で(bozo)httpdを起動し、あっれー? おかしいなー? と悩んでました。

当然ですが、/usr/pkg/share/example/rc.d/apacheから持ってきたやつで起動せんとイカンですね。

ApacheとPHPをつなぐためにはpkgsrc/www/ap-phpを入れねばなりません。
pkgsrc/textproc/php-intlも必要かもしれません。わすれました。

Apacheの設定ファイルも修正しています。
root@zbox>diff -u httpd.conf.orig httpd.conf
--- httpd.conf.orig     2012-08-18 16:36:02.000000000 +0000
+++ httpd.conf  2012-08-18 17:16:38.000000000 +0000
@@ -125,6 +125,8 @@
 LoadModule alias_module lib/httpd/mod_alias.so
 LoadModule rewrite_module lib/httpd/mod_rewrite.so

+LoadModule php5_module lib/httpd/mod_php5.so
+
 <IfModule !mpm_netware_module>
 <IfModule !mpm_winnt_module>
 #
@@ -486,3 +488,6 @@
 SSLRandomSeed startup builtin
 SSLRandomSeed connect builtin
 </IfModule>
+
+AddHandler application/x-httpd-php .php
+Include etc/roundcube/roundcube.conf

Apacheが書き込みできるようにパーミッションを変更。
root@zbox>chmod og+w /var/log/roundcube/errors
で、roundcubeのインストールのページに行くのがポイントです。roundcubeのトップページにアクセスして、うまくいかないとすげー悩んでいたんですが、セットアップ用のページにアクセスするんですね。

名前:  非公開コメント   

  • TB-URL  http://www.tokuda.net/diary/0789/tb/