PostfixAdmin + Roundcubeによるバーチャルドメイン対応Webメーラー

バーチャルドメイン対応のWebメーラーを探していて、Roundcubeというものを見つけた。
ドメイン・アカウントの管理にはPostfixAdminを利用するとかなり楽。

【実装環境】

CentOS 5.6 x86_64
PHP 5.3.3
Postfix 2.3.3
Dovecot 1.0.7
MySQL 5.0.77

Postfixのインストール】

RPMからPostfixをインストールをした場合、MySQLプラグインが利用出来ないのでSRPMからビルドする

# 現状入っているPostfixの削除
yum remove postfix
cd /usr/local/src/
wget http://ftp.riken.jp/Linux/centos/5.6/os/SRPMS/postfix-2.3.3-2.1.el5_2.src.rpm
rpm -ivh postfix-2.3.3-2.1.el5_2.src.rpm
wget http://vda.sourceforge.net/VDA/postfix-2.3.3-vda.patch.gz 
gunzip postfix-2.3.3-vda.patch.gz
cp postfix-2.3.3-vda.patch /usr/src/redhat/SOURCES/
cd /usr/src/redhat/SPECS/
vi postfix.spec
--------------------------------------------------------------------------
%define MYSQL 0 
↓ 
%define MYSQL 1 

Patch10の後に以下を追加 
Patch11: postfix-2.3.3-vda.patch 

%patch10の後に以下を追加 
%patch11 -p1 -b .vda
--------------------------------------------------------------------------

# ビルドに必要なパッケージのインストール
yum install gcc rpm-build db4-devel pkgconfig zlib-devel openldap-devel cyrus-sasl-devel pcre-devel mysql-devel openssl-devel

# SPECファイルを元にビルドする
rpmbuild -ba postfix.spec

# RPMパッケージが出来るのでインストール
rpm -Uvh /usr/src/redhat/RPMS/x86_64/postfix-2.3.3-2.1.x86_64.rpm

# 下記を実行した際にmysqlが結果に入っていればOK
postconf -m
--------------------------------------------------------------------------
btree
cidr
environ
hash
ldap
mysql
nis
pcre
proxy
regexp
static
unix
--------------------------------------------------------------------------

# yumのアップデートで上書きされないように変更
vi /etc/yum.conf
--------------------------------------------------------------------------
[main]セクションの最終行へ以下を追加

exclude=postfix*
--------------------------------------------------------------------------

# Postfix用アカウントを作成
mysql -u root -p
--------------------------------------------------------------------------
grant all privileges on postfix.* to postfix@localhost identified by '<password>'; 
quit
--------------------------------------------------------------------------
groupadd -g 10000 vuser
useradd -g vuser -u 10000 vuser
mkdir /usr/local/virtual
chown vuser:vuser /usr/local/virtual
chmod 771 /usr/local/virtual

# Postfixの設定
vi /etc/postfix/main.cf
--------------------------------------------------------------------------
myhostname = mail.example.com 
mydomain = example.com 
myorigin = $myhostname 
inet_interfaces = all 
mydestination = 
relay_domains = $mydestination 
smtpd_banner = $myhostname ESMTP unknown 

local_transport = virtual 
virtual_transport = virtual 
virtual_mailbox_base = /usr/local/virtual 
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf 
virtual_alias_domains = $virtual_alias_maps 
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf 
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf 
virtual_minimum_uid = 10000 
virtual_uid_maps = static:10000 
virtual_gid_maps = static:10000 

maximal_backoff_time = 800s 
minimal_backoff_time = 100s 
bounce_queue_lifetime = 60m 
maximal_queue_lifetime = 60m 

message_size_limit = 20480000 

virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_limit_maps.cf 
virtual_mailbox_limit_override = yes 
virtual_overquota_bounce = yes 
virtual_mailbox_limit_inbox = yes 

smtpd_sasl_auth_enable = yes 
smtpd_sasl_type = dovecot 
smtpd_sasl_path = private/auth 
smtpd_sasl_local_domain = $myhostname 
smtpd_client_restrictions = reject_rbl_client bl.spamcop.net 
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
--------------------------------------------------------------------------

vi /etc/postfix/mysql_virtual_alias_maps.cf
--------------------------------------------------------------------------
user = postfix
password = <password>
hosts = localhost
dbname = postfix
table = alias
select_field = goto
where_field = address
--------------------------------------------------------------------------

vi /etc/postfix/mysql_virtual_domains_maps.cf
--------------------------------------------------------------------------
user = postfix
password = <password>
hosts = localhost
dbname = postfix
table = domain
select_field = domain
where_field = domain
additional_conditions = and active = '1'
--------------------------------------------------------------------------

vi /etc/postfix/mysql_virtual_mailbox_maps.cf
--------------------------------------------------------------------------
user = postfix
password = <password>
hosts = localhost
dbname = postfix
table = mailbox
select_field = maildir
where_field = username
--------------------------------------------------------------------------

vi /etc/postfix/mysql_virtual_mailbox_limit_maps.cf 
--------------------------------------------------------------------------
user = postfix
password = <password>
hosts = localhost
dbname = postfix
table = mailbox
select_field = quota
where_field = username
--------------------------------------------------------------------------

// サブミッションポートの利用設定
vi /etc/postfix/master.cf
--------------------------------------------------------------------------
#submission inet n - n - - smtpd
↓
submission inet n - n - - smtpd
--------------------------------------------------------------------------

# 設定を反映
/etc/init.d/postfix restart

Dovecotのインストール】

Dovecotyumからインストールしたもので問題ない

yum install dovecot
vi /etc/dovecot.conf
--------------------------------------------------------------------------
protocols = pop3 imap 
mail_location = maildir:/usr/local/virtual/%d/%u 
first_valid_uid = 10000 
first_valid_gid = 10000 

#passdb pam { 
#} 

passdb sql { 
    args = /etc/dovecot-mysql.conf 
}

#userdb passwd { 
#}

userdb sql {
    args = /etc/dovecot-mysql.conf 
} 

mechanisms = plain login digest-md5 cram-md5 
pop3_uidl_format = %08Xu%08Xv 

socket listen { 
    client { 
        path = /var/spool/postfix/private/auth 
        mode = 0660 
        user = postfix 
        group = postfix 
    }
}
--------------------------------------------------------------------------

vi /etc/dovecot-mysql.conf
--------------------------------------------------------------------------
driver = mysql 
default_pass_scheme = PLAIN 
connect = dbname=postfix user=postfix host=/var/lib/mysql/mysql.sock password=<password> 
password_query = SELECT password FROM mailbox WHERE username = '%u' AND active = '1' 
user_query = SELECT concat('/usr/local/virtual/', maildir) as home, 10000 as uid, 10000 as gid FROM mailbox WHERE username = '%u' AND active = '1'
--------------------------------------------------------------------------
yum install cyrus-sasl-plain cyrus-sasl-md5 
/etc/rc.d/init.d/saslauthd stop
chkconfig saslauthd off
/etc/init.d/dovecot restart

phpのインストール】
CentOS 5.6だと初期の状態からyumでphp53が利用出来るけど、DOMが利用できないのと、mcryptも利用出来ないので、SRPMからビルドします。
最初はソースからphpをインストールしたんですけど、libc-clientがどうしても入らなかった為、rpmに変更しました。

// 必要なもののインストール
# yum install libmcrypt-devel libtool-ltdl-devel

# cd /usr/local/src/

// php53のSRPMを取得
# wget http://ftp.riken.jp/Linux/centos/5.6/os/SRPMS/php53-5.3.3-1.el5.src.rpm

// 警告: グループ mockbuild は存在しません - root を使用します
// 上記のように表示されることがあるが、スルーでOK
# rpm -ivh php53-5.3.3-1.el5.src.rpm
# cd /usr/src/redhat/SPECS/
# vi php53.spec
--------------------------------------------------------------------------
// without_sharedの中に【--disable-dom】が入っているので削除
// 以下のようになるように変更

without_shared="--without-mysql --without-gd \
      --disable-dba --without-unixODBC \
      --disable-pdo --disable-xmlreader --disable-xmlwriter \
      --without-sqlite3 --disable-phar --disable-fileinfo \
      --disable-json --without-pspell --disable-wddx \
      --without-curl --disable-posix \
      --disable-sysvmsg --disable-sysvshm --disable-sysvsem"


// configureの中に【--with-mcrypt \】を追加
// 以下のようになるように変更

%configure \
        --cache-file=../config.cache \
        --with-libdir=%{_lib} \
        --with-config-file-path=%{_sysconfdir} \
        --with-config-file-scan-dir=%{_sysconfdir}/php.d \
        --disable-debug \
        --with-pic \
        --disable-rpath \
        --without-pear \
        --with-bz2 \
        --with-exec-dir=%{_bindir} \
        --with-freetype-dir=%{_prefix} \
        --with-png-dir=%{_prefix} \
        --with-xpm-dir=%{_prefix} \
        --enable-gd-native-ttf \
        --without-gdbm \
        --with-gettext \
        --with-gmp \
        --with-iconv \
        --with-jpeg-dir=%{_prefix} \
        --with-openssl \
        --with-pcre-regex=%{_prefix} \
        --with-zlib \
        --with-layout=GNU \
        --enable-exif \
        --enable-ftp \
        --enable-magic-quotes \
        --enable-sockets \
        --enable-sysvsem --enable-sysvshm --enable-sysvmsg \
        --with-kerberos \
        --enable-ucd-snmp-hack \
        --enable-shmop \
        --enable-calendar \
        --without-sqlite \
        --without-sqlite3 \
        --with-libxml-dir=%{_prefix} \
        --enable-xml \
        --with-system-tzdata \
        --with-mcrypt \
--------------------------------------------------------------------------
# SPECファイルを元にビルドする
# エラーが発生した場合、表示されている必要なパッケージをyumでインストールしてから再実行
rpmbuild -bb --target=x86_64 --clean php53.spec

# 既に入っているphpを削除
yum remove "php53*"

# ビルドしたRPMをインストール
cd /usr/src/redhat/RPMS/x86_64/
rpm -ivh php53-5.3.3-1.x86_64.rpm \
	php53-cli-5.3.3-1.x86_64.rpm \
	php53-common-5.3.3-1.x86_64.rpm \
	php53-devel-5.3.3-1.x86_64.rpm \
	php53-gd-5.3.3-1.x86_64.rpm \
	php53-imap-5.3.3-1.x86_64.rpm \
	php53-intl-5.3.3-1.x86_64.rpm \
	php53-mbstring-5.3.3-1.x86_64.rpm \
	php53-mysql-5.3.3-1.x86_64.rpm \
	php53-pdo-5.3.3-1.x86_64.rpm

# pearのインストール
yum install php-pear

# php.iniの編集
vi /etc/php.ini
--------------------------------------------------------------------------
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED
memory_limit = 256M
upload_max_filesize = 32M
post_max_size = 32M
display_errors = On
include_path = ".:/usr/share/pear"
date.timezone = "Asia/Tokyo"
--------------------------------------------------------------------------

# 必要なPEARパッケージのインストール
yum install php-pear-MDB2 php-pear-Net-SMTP php-pear-Mail-Mime
/etc/init.d/httpd restart

【PostfixAdminのインストール】

ここまで出来てようやくPostfixAdminとRoundcubeを利用する準備が整う
下記がPostfixAdminのインストール手順

cd /usr/local/src
wget "http://downloads.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-2.3.3/postfixadmin-2.3.3.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fpostfixadmin%2Ffiles%2F&ts=1304236367&use_mirror=jaist"
tar zxvf postfixadmin-2.3.3.tar.gz
mv postfixadmin-2.3.3 /srv/postfixadmin

# 設定ファイルの編集
vi /srv/postfixadmin/config.inc.php
--------------------------------------------------------------------------
$CONF['configured'] = true; 
$CONF['default_language'] = 'ja'; 

$CONF['database_type'] = 'mysql'; 
$CONF['database_host'] = 'localhost'; 
$CONF['database_user'] = 'postfix'; 
$CONF['database_password'] = '<password>'; 
$CONF['database_name'] = 'postfix'; 
$CONF['database_prefix'] = ''; 

$CONF['encrypt'] = 'cleartext'; 
$CONF['domain_path'] = 'YES'; 
$CONF['aliases'] = '100'; 
$CONF['mailboxes'] = '100'; 
$CONF['maxquota'] = '300'; 
$CONF['quota'] = 'NO'; 
$CONF['backup'] = 'NO'; 
$CONF['sendmail'] = 'NO'; 
$CONF['fetchmail'] = 'NO'; 

$CONF['footer_text'] = 'Postfix Admin'; 
$CONF['footer_link'] = 'http://www.example.com/postfixadmin/'; 

$CONF['emailcheck_resolve_domain']='NO';
--------------------------------------------------------------------------

# HTTPで閲覧出来るように設定 
vi /etc/httpd/conf/httpd.conf
--------------------------------------------------------------------------
Alias /postfixadmin/ "/srv/postfixadmin/" 
<Directory "/srv/postfixadmin"> 
Options All -Indexes 
AllowOverride None 
Order deny,allow 
Deny from all 
Allow from 127.0.0.1 
</Directory> 
--------------------------------------------------------------------------

ここまで出来たら、http://www.example.com/postfixadmin/setup.phpへアクセスし、
システムパスワードを設定した後、管理者ユーザーの作成
この時点で作成した管理者ユーザーでログインできるかチェック
ログイン出来なかった場合はmaillogなり見て修正

【Roundcubeのインストール】

下記サイトよりRoundcubeをダウンロードし、インストールする
http://roundcube.net/

cd /usr/local/src/
wget "http://downloads.sourceforge.net/project/roundcubemail/roundcubemail/0.5.2/roundcubemail-0.5.2.tar.gz?r=http%3A%2F%2Froundcube.net%2Fdownload&ts=1304236682&use_mirror=jaist"
tar zxvf roundcubemail-0.5.2.tar.gz
mv roundcubemail-0.5.2 /srv/www/roundcube
cd /srv/www/roundcube
chmod 775 temp
chmod 775 logs

# DBの作成
mysql -u root -p
--------------------------------------------------------------------------
create database roundcube character set utf8 collate utf8_bin;
grant all privileges on roundcubemail.* to roundcubemail@localhost identified by 'パスワード'
quit
--------------------------------------------------------------------------

# テーブルの作成
cd /srv/www/roundcube/
mysql -u root -p roundcube < SQL/mysql.initial.sql

ここまで出来たら、インストーラー画面へアクセスし、設定する(面倒臭いので説明は省略する)
http://www.example.com/roundcube/installer/

設定が完了後、config/main.inc.phpを編集

<?php
// Use this charset as fallback for message decoding
$rcmail_config['default_charset'] = 'ISO-2022-JP';

// automatically create the above listed default folders on login
$rcmail_config['create_default_folders'] = true;

// log driver:  'syslog' or 'file'.
#$rcmail_config['log_driver'] = 'file';
$rcmail_config['log_driver'] = 'syslog';

// Log sent messages to <log_dir>/sendmail or to syslog
$rcmail_config['smtp_log'] = true;

// Log successful logins to <log_dir>/userlogins or to syslog
$rcmail_config['log_logins'] = true;

// IMAP AUTH type (DIGEST-MD5, CRAM-MD5, LOGIN, PLAIN or empty to use
// best server supported one)
//$rcmail_config['imap_auth_type'] = null;
$rcmail_config['imap_auth_type'] = 'PLAIN'

// default setting if preview pane is enabled
$rcmail_config['preview_pane'] = true;;

機種依存文字による文字化け対策、CP932を利用して表示する設定に変更
program/include/rcube_shared.incを編集

<?php
// FIXME: the order is important, because sometimes
// iso string is detected as euc-jp and etc.
$enc = array(
	'UTF-8', 'CP932', 'SJIS', 'BIG5', 'GB2312',
	'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4',
	'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9',
	'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16',
	'WINDOWS-1252', 'WINDOWS-1251', 'EUC-JP', 'EUC-TW', 'KOI8-R',
	'ISO-2022-KR', 'ISO-2022-JP'
);

とりあえずこれでPostfixAdminで作成したユーザーでログイン出来るかテスト