Help us understand the problem. What is going on with this article?

さくらVPS(CentOS6.6)にメールサーバーを構築する

More than 3 years have passed since last update.

管理も楽であるばかりか、バーチャルドメインにも対応できるので、dovecot, postfix, MySQL, postfixAdmin を使った運用にします。
2014年12月20日現在、それぞれのバージョンは以下の通りです。

参考文献にはPostfix はソースからビルドしないといけないと書いてあるものがありましたが、yumインストールしても問題なく動作確認できたので、yum を使ってインストールします。

  • dovecot.x86_64 1:2.0.9-8.el6_6.1
  • postfix.x86_64 2:2.6.6-6.el6_5
  • dovecot-mysql.x86_64 1:2.0.9-8.el6_6.1
  • cyrus-sasl.x86_64 2.1.23-15.el6_6.1
  • postfix admin 2.92

では、独自ドメイン example.com を利用して自前のメールサーバーを構築する最低限の構成する手順を紹介します。http, mysql-server はインストール済みであることを前提として進めてまいります。SSL や スパムフィルターの設定は省略しているので、ここに書かれている方法だけでは不十分です。iptables の設定もさくらインターネットのFAQに書かれているので省略。

すくなくとも vi って何?とか、ログってどーやって確認するの?という人はとてもサーバー管理なんてできませんし、スパム業者や架空請求メールの配信元に乗っ取られてしまうのがオチです。素直にプロに頼みましょう。

ここまで言われても作業を進めたい方はどうぞ。

まずは必要なモジュールをインストールです。

$ sudo yum -y install postfix dovecot dovecot-mysql cyrus-sasl

postfix admin に必要なので、こちらも。

$ sudo yum -y install php-imap

バーチャルドメインで受けたメールを処理するユーザの設定

$ sudo groupadd -g 10000 vuser
$ sudo useradd -g vuser -u 10000 vuser
$ sudo mkdir /mail
$ sudo chown vuser:vuser /mail
$ sudo chmod 771 /mail
$ mysql -u root -p
----------------------------------------------------
SELECT host,user,password FROM mysql.user;
DELETE FROM mysql.user WHERE user="";
SELECT host,user,password FROM mysql.user;
CREATE DATABASE postfix;
CREATE USER 'postfix'@'localhost' IDENTIFIED BY 'MySQLパスワード';
GRANT ALL PRIVILEGES ON `postfix` . * TO 'postfix'@'localhost';
exit
----------------------------------------------------
$ cd /etc/postfix/

$ sudo cat <<EOF > mysql_virtual_alias_maps.cf
user = postfix
password = MySQLパスワード
hosts = localhost
dbname = postfix
table = alias
select_field = goto
where_field = address
EOF

$ sudo cat <<EOF > mysql_virtual_domains_maps.cf
user = postfix
password = MySQLパスワード
hosts = localhost
dbname = postfix
table = domain
select_field = domain
where_field = domain
additional_conditions = and active = '1'
EOF

$ sudo cat <<EOF > mysql_virtual_mailbox_maps.cf
user = postfix
password = MySQLパスワード
hosts = localhost
dbname = postfix
table = mailbox
select_field = maildir
where_field = username
EOF

$ sudo cat <<EOF > mysql_virtual_mailbox_limit_maps.cf
user = postfix
password = MySQLパスワード
hosts = localhost
dbname = postfix
table = mailbox
select_field = quota
where_field = username
EOF

$ sudo chown root:postfix mysql_virtual_*
$ sudo chmod 640 mysql_virtual_*

postfix(送信サーバー)の設定

main.cf
$ sudo vi /etc/postfix/main.cf

-------------------------------------------------------------------------------
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
relay_domains = $mydestination
home_mailbox = Maildir/
smtpd_banner = $myhostname ESMTP unknown

local_transport = local
virtual_transport = virtual
virtual_mailbox_base = /mail
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_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
$ sudo vi /etc/postfix/master.cf

-------------------------------------------------------------------------------
smtp      inet  n       -       n       -       -       smtpd
submission inet n       -       n       -       -       smtpd
-------------------------------------------------------------------------------

dovecot(受信サーバー)の設定

sudo cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.org
sudo vi /etc/dovecot/dovecot.conf
-------------------------------------------------------------------------------
#protocols = imap imaps pop3 pop3s
protocols = pop3 imap

mail_location = maildir:/mail/%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
  mechanisms = plain login digest-md5 cram-md5


protocol pop3 {
  pop3_uidl_format = %08Xu%08Xv
}

  socket listen {
    client {
      path = /var/spool/postfix/private/auth
      mode = 0660
      user = postfix
      group = postfix
    }
  }
-------------------------------------------------------------------------------
sudo cat < /etc/dovecot-mysql.conf
driver = mysql
default_pass_scheme = PLAIN
connect = dbname=postfix user=postfix host=/var/lib/mysql/mysql.sock password=MySQLパスワード
password_query = SELECT password FROM mailbox WHERE username = '%u' AND active = '1'
user_query = SELECT concat('/mail/', maildir) as home, 10000 as uid, 10000 as gid FROM mailbox WHERE username = '%u' AND active = '1'
EOF

postfix admin のインストール

cd /usr/local/src
sudo wget http://downloads.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-2.92/postfixadmin-2.92.tar.gz
sudo tar zxvf postfixadmin-2.92.tar.gz
sudo mv postfixadmin-2.92/ /var/www/postfixadmin

sudo cd /var/www/postfixadmin
sudo vi config.inc.php
----------------------------------------------------
//$CONF['configured'] = false;
$CONF['configured'] = true;

//$CONF['default_language'] = 'en';
$CONF['default_language'] = 'ja';

$CONF['database_type'] = 'mysql';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'postfix';
$CONF['database_password'] = 'MySQLパスワード';
$CONF['database_name'] = 'postfix';
$CONF['database_prefix'] = '';

//$CONF['encrypt'] = 'md5crypt';
$CONF['encrypt'] = 'cleartext';

$CONF['page_size'] = '100';

//$CONF['domain_path'] = 'NO';
$CONF['domain_path'] = 'YES';

// Default Domain Values
// Specify your default values below. Quota in MB.
$CONF['aliases'] = '100';
$CONF['mailboxes'] = '100';
$CONF['maxquota'] = '300';

// Quota
// When you want to enforce quota for your mailbox users set this to 'YES'.
$CONF['quota'] = 'NO';

$CONF['backup'] = 'NO';

$CONF['sendmail'] = 'NO';

$CONF['fetchmail'] = 'NO';

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

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

apache の設定

sudo vi /etc/httpd/conf.d/postfixadmin.conf
----------------------------------------------------
Alias /postfixadmin "/var/www/postfixadmin"

<Directory "/var/www/postfixadmin">
    Order allow,deny
    Allow from all
</Directory>
----------------------------------------------------
apachectl -t
apachectl restart

http://example.com/postfixadmin/ にアクセスしてログインできるかどうか確認。管理者パスワードなど設定。

sudo rm -f /var/www/postfixadmin/setup.php
sudo rm -f /var/www/postfixadmin/motd*

参考リンク

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away