Last Updated on 2013年7月20日 by かんりにん
約2年ぶりくらいにpostfix+mysql+postfixadminでメールサーバーをセットアップしたので
備忘録として。
割とスタンダードな内容なので、参考になるかわかりませんが・・・
概要 †
- サーバー
CentOS5.7 - ドメイン
example.comに置換しています。
メールサーバー名:mail.example.com
構成ミドルウェア †
ライブラリや関連パッケージは省略。postfixadmin以外は、基本的にすべてRPMで。
postifx postfixadmin dovecot php mysql
セットアップ順序 †
DB環境が構築された状態でpostfixの環境を構成する必要があるため
以下の順序で実施。
1.mysql
2.php
3.postfixadmin
4.postifx
5.dovecot
備考 †
ドキュメント内の各種パスワードは例として記載しています。
参考にされる際はご注意下さい。
セットアップログ †
1)mysql †
設定ファイル /etc/my.cnf rootパスワード MYSQLPASSWD
1.文字コードをUTF-8に統一 †
起動後も変更が可能だが、インストール直後なのであらかじめ設定しておく。
0a1,4 > # > [client] > default-character-set=utf8 > 12a17,22 > default-character-set = utf8 > skip-character-set-client-handshake > character-set-server = utf8 > collation-server = utf8_general_ci > init-connect = SET NAMES utf8
2.mysqld起動 †
# /etc/init.d/mysqld start
合わせてデータベースの初期化を実施(postgresのinitdbと同じ)
3.rootパスワード設定 †
設定は乱数で。
- パスワード設定
# mysqladmin -u root password 'MYSQLPASSWD'
- ログイン確認
# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.0.77 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
- 環境確認
mysql> show variables like 'character_set%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) mysql> status -------------- mysql Ver 14.12 Distrib 5.0.77, for redhat-linux-gnu (i686) using readline 5.1 Connection id: 3 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.0.77 Source distribution Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/lib/mysql/mysql.sock Uptime: 1 min 10 sec Threads: 1 Questions: 11 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 6 Queries per second avg: 0.157 -------------- mysql>
- 不要な匿名ユーザーの削除と、全デフォルトDBへのrootパスワード設定
mysql> select host,user,password from mysql.user; +---------------------+------+------------------+ | host | user | password | +---------------------+------+------------------+ | localhost | root | 53f0d3581cd33a0d | | mailsvr.example.com | root | | | 127.0.0.1 | root | | | localhost | | | | mailsvr.example.com | | | +---------------------+------+------------------+ 5 rows in set (0.00 sec) mysql> delete from mysql.user where user=""; Query OK, 2 rows affected (0.00 sec) mysql> select host,user,password from mysql.user; +---------------------+------+------------------+ | host | user | password | +---------------------+------+------------------+ | localhost | root | 53f0d3581cd33a0d | | mailsvr.example.com | root | | | 127.0.0.1 | root | | +---------------------+------+------------------+ 3 rows in set (0.00 sec) mysql> set password for root@'mailsvr.example.com'=password('MYSQLPASSWD'); Query OK, 0 rows affected (0.00 sec) mysql> set password for root@'127.0.0.1'=password('MYSQLPASSWD'); Query OK, 0 rows affected (0.00 sec) mysql> select host,user,password from mysql.user; +---------------------+------+------------------+ | host | user | password | +---------------------+------+------------------+ | localhost | root | 53f0d3581cd33a0d | | mailsvr.example.com | root | 53f0d3581cd33a0d | | 127.0.0.1 | root | 53f0d3581cd33a0d | +---------------------+------+------------------+ 3 rows in set (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql>
- postfixadmin用DB作成
# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 5.0.77 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> CREATE DATABASE postfix; Query OK, 1 row affected (0.00 sec) mysql> CREATE USER 'postfix'@'localhost' IDENTIFIED BY 'POSTFIXADMINPASSWD'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON `postfix` . * TO 'postfix'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye
2)php †
- 初期設定
設定ファイル /etc/php.ini# cd /etc/ # cp -p php.ini php.ini.bak # vi php.ini
- [設定内容]
501c501 < ;default_charset = "iso-8859-1" --- > default_charset = "UTF-8" 639c639 < ;date.timezone = --- > date.timezone = "Asia/Tokyo" 1133c1133 < ;mbstring.http_input = auto --- > mbstring.http_input = pass 1137c1137 < ;mbstring.http_output = SJIS --- > mbstring.http_output = pass 1144c1144 < mbstring.encoding_translation = on --- > mbstring.encoding_translation = Off 1162c1162 < ;mbstring.func_overload = 0 --- > mbstring.func_overload = 0
- [設定内容]
3)httpd †
ドキュメントルートはhttpdのデフォルトの/var/www/html、
postfix管理画面は/var/www/html/postfixとする。
# cd /etc/httpd/conf # cp -p httpd.conf httpd.conf.bak # vi httpd.conf
- [設定内容1]基本設定
44c44 < ServerTokens OS --- > ServerTokens Prod 996a997,998 > > TraceEnable Off
- [設定内容2]ダイジェスト認証
/var/www/html/postfixに対して認証を設定。
ベーシック認証では心もとないのでダイジェスト認証で設定する。- httpd.conf
> Alias /postfixadmin "/var/www/html/postfixadmin" > > <Directory "/var/www/html/postfixadmin"> > > #order allow,deny > #allow from all > > AuthType Digest > AuthName "Digest Auth" > AuthUserFile /etc/httpd/conf/htdigestpasswd > require valid-user > > </Directory> >
- httpd.conf
- [設定内容3]ユーザーおよびパスワード設定
# htdigest -c /etc/httpd/conf/htdigestpasswd "Digest Auth" postfixadmin Adding password for postfixadmin in realm Digest Auth. New password: Re-type new password: #
→設定後、httpdを再起動。
4)postfixadmin †
- ソースアーカイブのダウンロード
# cd /usr/local/src/ # wget http://sourceforge.net/projects/postfixadmin/files/postfixadmin/postfixadmin-2.3.4/postfixadmin_2.3.4.tar.gz/download # tar -zxvf postfixadmin_2.3.4.tar.gz # cp -pR postfixadmin-2.3.4 /var/www/html/ # cd /var/www/html/ # chown -R apache.apache postfixadmin-2.3.4/ # mv postfixadmin-2.3.4 postfixadmin
- 初期設定
# cd postfixadmin/ # cp -p config.inc.php config.inc.php.bak # vi config.inc.php
- [設定内容]
26c26 < $CONF['configured'] = false; --- > $CONF['configured'] = true; 52c52 < $CONF['database_password'] = 'postfixadmin'; --- > $CONF['database_password'] = 'POSTFIXADMINPASSWD'; 82c82 < $CONF['admin_email'] = 'postmaster@change-this-to-your.domain.tld'; --- > $CONF['admin_email'] = 'postmaster@example.com'; 99c99,100 < $CONF['encrypt'] = 'md5crypt'; --- > //$CONF['encrypt'] = 'md5crypt'; > $CONF['encrypt'] = 'cleartext'; 133,136c134,137 < 'abuse' => 'abuse@change-this-to-your.domain.tld', < 'hostmaster' => 'hostmaster@change-this-to-your.domain.tld', < 'postmaster' => 'postmaster@change-this-to-your.domain.tld', < 'webmaster' => 'webmaster@change-this-to-your.domain.tld' --- > 'abuse' => 'abuse@example.com', > 'hostmaster' => 'hostmaster@example.com', > 'postmaster' => 'postmaster@example.com', > 'webmaster' => 'webmaster@example.com' 144c145 < $CONF['domain_path'] = 'NO'; --- > $CONF['domain_path'] = 'YES'; 150c151 < $CONF['domain_in_mailbox'] = 'YES'; --- > $CONF['domain_in_mailbox'] = 'NO'; 216c217 < $CONF['vacation_domain'] = 'autoreply.change-this-to-your.domain.tld'; --- > $CONF['vacation_domain'] = 'autoreply.example.com'; 233c234 < $CONF['alias_control'] = 'NO'; --- > $CONF['alias_control'] = 'YES'; 236c237 < $CONF['alias_control_admin'] = 'NO'; --- > $CONF['alias_control_admin'] = 'YES'; 241c242 < $CONF['special_alias_control'] = 'NO'; --- > $CONF['special_alias_control'] = 'YES';
→設定後、http://DOMAINNAME.TLD/postfixadmin/setup.php へアクセスし
Web上からセットアップを行う。
- [設定内容]
- セットアップパスワードの投入とhash化パスワードの反映
管理画面の最下段にてセットアップパスワードを入力すると、hash化したパスワードが
Web画面へ出力されるので、config.ini.phpへ反映させる。
※セットアップパスワードは管理者パスワードの変更などに必要となるため
メモをとって保管しておくこと。- [追加内容
31c31 < $CONF['setup_password'] = 'changeme'; --- > $CONF['setup_password'] = '53a1d7cbf72b7bdb4d1c72c7f238570f:49aaaf3cf2807a331e0532dc123e387339cda37b';
- [追加内容
5)postifx †
再コンパイル †
postfixadminへ対応するため、mysql対応パッケージをビルドする
- パッケージのダウンロード
理研のサイトからCentOS5.7用のpostfixのソースパッケージをダウンロード。# cd /usr/src/redhat/SRPMS/ # wget http://ftp.riken.go.jp/Linux/centos/5.7/centosplus/SRPMS/postfix-2.3.3-2.3.centos.mysql_pgsql.src.rpm
- ビルド
パッケージのビルドに当たって、VDOからのパッチを当てておく# rpm -Uvh postfix-2.3.3-2.3.centos.mysql_pgsql.src.rpm # cd ../SOURCES/ # wget http://vda.sourceforge.net/VDA/postfix-2.3.3-vda.patch.gz # gunzip -d postfix-2.3.3-vda.patch.gz
- [specファイル編集]
上記のvdaパッチを適用するため、以下をspecファイルへ追記。# cd ../SPECS # cp -p postfix.spec postfix.spec.bak # vi postfix.spec 78a79 > Patch2: postfix-2.3.3-vda.patch 141a143 > %patch2 -p1 -b .trash
- [specファイル編集]
- specファイル編集後、rpmbuild実行。
# rpmbuild -bb postfix.spec
- 作成パッケージを確認。
書き込み完了: /usr/src/redhat/RPMS/i386/postfix-2.3.3-2.3.centos.mysql_pgsql.i386.rpm 書き込み完了: /usr/src/redhat/RPMS/i386/postfix-pflogsumm-2.3.3-2.3.centos.mysql_pgsql.i386.rpm 書き込み完了: /usr/src/redhat/RPMS/i386/postfix-debuginfo-2.3.3-2.3.centos.mysql_pgsql.i386.rpm
- インストール後、yum update時にpostfixが自動アップデートされないよう
無効にしておく。# cd /etc/ # cp -p yum.conf yum.conf.bak # vi yum.conf
- [設定値]
20a21,23 > > # postfix is excepted from update. > exclude=postfix*
- [設定値]
- postfixとpostfixadminを連携するための実行ユーザーを設定
# 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
- インストール
# cd /usr/src/redhat/RPMS/i386/ # rpm --force -Uvh --test postfix-2.3.3-2.3.centos.mysql_pgsql.i386.rpm 準備中... ########################################### [100%] # rpm --force -Uvh postfix-2.3.3-2.3.centos.mysql_pgsql.i386.rpm 準備中... ########################################### [100%] 1:postfix ########################################### [100%]
- インストール確認
# rpm -qa | grep postfix postfix-2.3.3-2.3.centos.mysql_pgsql
初期設定 †
- [main.cf]
# cd /etc/postfix/ # cp -p main.cf main.cf.bak # vi main.cf
- [設定内容]
70c70 < #myhostname = virtual.domain.tld --- > myhostname = mail.example.com 77c77 < #mydomain = domain.tld --- > mydomain = example.com 93c93 < #myorigin = $mydomain --- > myorigin = $mydomain 107c107 < #inet_interfaces = all --- > inet_interfaces = all 110c110 < inet_interfaces = localhost --- > #inet_interfaces = localhost 155c155 < mydestination = $myhostname, localhost.$mydomain, localhost --- > #mydestination = $myhostname, localhost.$mydomain, localhost 158a159 > mydestination = 287c288 < #relay_domains = $mydestination --- > relay_domains = $mydestination 377c378 < alias_maps = hash:/etc/aliases --- > alias_maps = hash:/etc/postfix/aliases 388c389 < alias_database = hash:/etc/aliases --- > alias_database = hash:/etc/postfix/aliases 410c411 < #home_mailbox = Maildir/ --- > home_mailbox = Maildir/ 561a563 > smtpd_banner = $myhostname 667a670,722 > > # postfix advanced configuration > disable_vrfy_command = yes > > > ## postfix and postfixadmin configuration > 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 > > # mailbox size limit 50MB > mailbox_size_limit = 51200000 > > # mail size limit 10MB > message_size_limit = 10240000 > > virtual_mailbox_limit = 51200000 > > virtual_mailbox_limit_maps = hash:/etc/postfix/vquota > > virtual_mailbox_limit_override = yes > > virtual_overquota_bounce = yes > > virtual_mailbox_limit_inbox = yes > > ## smtp auth configutation > smtpd_sasl_auth_enable = yes > > smtpd_sasl_type = dovecot > > smtpd_sasl_path = private/auth > > smtpd_sasl_local_domain = $myhostname > > smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
- [設定内容]
- master.cf
サブミッションポートを有効にしておく。 # cp -p master.cf master.cf.bak # vi master.cf
- [設定値]
10c10 < #submission inet n - n - - smtpd --- > submission inet n - n - - smtpd
- [設定値]
postfixadminの関連設定ファイルを追加 †
- mysql_virtual_alias_maps.cf
# touch mysql_virtual_alias_maps.cf # vi mysql_virtual_alias_maps.cf
- [設定値]
user = postfix password = POSTFIXADMINPASSWD(mysqlのpostfixユーザーのパスワード) hosts = localhost dbname = postfix query = SELECT goto FROM alias WHERE address='%s'
- [設定値]
- mysql_virtual_domains_maps.cf
# touch mysql_virtual_domains_maps.cf # vi mysql_virtual_domains_maps.cf
- [設定値]
user = postfix password = jzbinKrBwssZ hosts = localhost dbname = postfix query = SELECT domain FROM domain WHERE domain='%s'
- [設定値]
- mysql_virtual_mailbox_maps.cf
# touch mysql_virtual_mailbox_maps.cf # vi mysql_virtual_mailbox_maps.cf
- [設定値]
user = postfix password = jzbinKrBwssZ hosts = localhost dbname = postfix query = SELECT maildir FROM mailbox WHERE username='%s'
- [設定値]
- vquota
※ユーザーごとのメールボックスのサイズ上限を設定するファイル。
デフォルトは空ファイルのままdbファイルを作成しておき、後で必要に応じて変更。# touch vquota # postmap vquota # ll vquota* -rw-r--r-- 1 root root 0 9月 28 23:04 vquota -rw-r--r-- 1 root root 12288 9月 28 23:04 vquota.db ←vquota.dbファイルが作成されていることを確認。
設定完了後、postfixを再起動(reloadではない)
#/etc/init.d/postfix restart
6)dovecot †
初期設定 †
- /etc/dovecot.conf
# cd /etc/ # cp -p dovecot.conf dovecot.conf.bak # vi dovecot.conf
- [設定値]
※32ビットOSなので64ビット対応は不要。 211c211 < #mail_location = --- > mail_location = maildir:/usr/local/virtual/example.com/%u 327c327 < #first_valid_uid = 500 --- > first_valid_uid = 10000 334c334 < #first_valid_gid = 1 --- > first_valid_gid = 10000 622c622 < #pop3_uidl_format = %08Xu%08Xv --- > pop3_uidl_format = %08Xu%08Xv 762c762,763 < mechanisms = plain --- > #mechanisms = plain > mechanisms = plain login digest-md5 cram-md5 869c870 < #passdb sql { --- > passdb sql { 871,872c872,873 < #args = < #} --- > ar gs = /etc/dovecot-mysql.conf > } 930c931 < #userdb sql { --- > userdb sql { 932,933c933,934 < #args = < #} --- > args = /etc/dovecot-mysql.conf > } 978c979 < #socket listen { --- > socket listen { 989c990 < #client { --- > client { 994,996c995,1000 < #mode = 0660 < #} < #} --- > path = /var/spool/postfix/private/auth > mode = 0660 > user = postfix > group = postfix > } > }
- [設定値]
- /etc/dovecot-mysql.conf
pop、imap認証のためmysqlへ接続する設定を追加。
設定ファイル内でユーザー認証のためのSQL文も含まれる。# cd /etc/ # touch dovecot-mysql.conf # vi dovecot-mysql.conf
- [設定値]
driver = mysql default_pass_scheme = PLAIN connect = dbname=postfix user=postfix host=/var/lib/mysql/mysql.sock password=POSTFIXADMINPASSWD password_query = SELECT password FROM mailbox WHERE local_part = '%u' AND active = '1' user_query = SELECT concat('/usr/local/virtual/', maildir) as home, 10000 as uid, 10000 as gid FROM mailbox WHERE local_part = '%u'
- [設定値]
→設定完了後、サービスを再起動。
動作検証 †
1)SMTPサーバをsendmailからpostfixへ切り替え †
# system-switch-mail
→CUIウィザードでpostfix変更。切り替えと同時にpostfixサービスが起動される。
- ログ確認
- /var/log/maillog
Sep 28 23:16:23 example.com postfix/postfix-script: starting the Postfix mail system Sep 28 23:16:23 example.com postfix/master[23324]: daemon started -- version 2.3.3, configuration /etc/postfix
- /var/log/maillog
2)dovecot †
# /etc/init.d/dovecot start Dovecot Imap を起動中: [ OK ]
- ログ確認
/var/log/maillog Sep 29 00:41:23 example.com dovecot: Dovecot v1.0.7 starting up Sep 29 00:41:23 example.com dovecot: auth-worker(default): mysql: Connected to /var/lib/mysql/mysql.sock (postfix)
自動起動設定を追加 †
postfixはsystem-switch-mail実行時にchkconfig onとなっているので、dovecotとmysqlのみ設定。
# chkconfig --list | grep dovecot dovecot 0:off 1:off 2:off 3:off 4:off 5:off 6:off # chkconfig dovecot on # chkconfig --list | grep dovecot dovecot 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# chkconfig --list | grep mysqld mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off # chkconfig mysqld on # chkconfig --list | grep mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off