LoginSignup
17
9

More than 5 years have passed since last update.

Amazon Linux on EC2にPostfix+Dovecotでメールサーバー構築

Posted at

前提

ドメインはexample.com、メールサーバーはmail.example.com、メールアドレスはuser@example.com とする。
また、認証はLinuxのユーザ認証と同じユーザ名/パスワードを利用する。

インスタンスの準備

22(SSH)、25(SMTP)、465(SMTPS)、993(IMAPS)ポートを開けておく。

$ sudo yum update

postfix

▪️postfixのインストールのインストール

$ sudo yum -y install postfix

▪️postfixの設定

$ sudo vim /etc/postfix/main.cf

以下設定内容

# メールサーバのホスト名(mail.example.com)を設定
myhostname = mail.example.com
# メールアドレスで利用するドメイン(example.com)を設定
mydomain = example.com
# ローカルから配信されたメールは、example.comのメールであると認識させるための設定
myorigin = $mydomain
# 自ドメイン宛メールを受信できるようにする
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
# 外部からのメール受信を許可するように変更
inet_interfaces = all
# VPC全体を信頼するネットワークとして設定
mynetworks = 192.168.0.0/24, 127.0.0.0/8
# メールの保存形式をメールディレクトリ形式に設定。 メールは宛先ユーザのホームディレクトリ(user@example.cpmならば、/home/user/)のMaildirディレクトリに保存される。
home_mailbox = Maildir/
# メールサーバを隠蔽する設定を追加
smtpd_banner = $myhostname ESMTP unknown
# 受信メールサイズの制限の設定を追加
message_size_limit = 10485760
# saslauthdを利用するための設定
smtpd_sasl_auth_enable = yes
broken_sasl_auth_clients = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_sender_restrictions = reject_unknown_sender_domain
smtpd_client_restrictions = permit_mynetworks,reject_unknown_client,permit
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination

▪️submissionポートとsmptsポートを使うための設定

$ sudo vim /etc/postfix/master.cf

以下をコメントイン

submission inet n       -       n       -       -       smtpd
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING
smtps     inet  n       -       n       -       -       smtpd
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

▪️認証用のSMTP-Authのためのsaslauthdを起動。また、自動起動の設定も行う。

$ sudo service saslauthd start
$ sudo chkconfig saslauthd on

▪️デフォルトMTAの変更

$ sudo alternatives --config mta

※ 2を選択

▪️sendmailの停止

$ sudo /etc/rc.d/init.d/sendmail stop
$ sudo chkconfig sendmail off

▪️postfixの起動

$ sudo service postfix start
$ sudo chkconfig postfix on

dovecot

▪️dovecotのインストール

$ sudo yum -y install dovecot

▪️dovecotの設定

$ sudo vim /etc/dovecot/conf.d/10-mail.conf

以下追加

mail_location = maildir:~/Maildir

▪️受信の際の認証方法にloginを追加

$ sudo vim /etc/dovecot/conf.d/10-auth.conf

以下追加

auth_mechanisms = plain login

▪️ポートの設定

$ sudo vim /etc/dovecot/conf.d/10-master.conf

以下コメントイン、追記

service imap-login {
  inet_listener imap {
    port = 143
  }
  inet_listener imaps {
    port = 993
    ssl = yes
  }
}

service pop3-login {
  inet_listener pop3 {
    port = 110
  }
  inet_listener pop3s {
    port = 995
    ssl = yes
  }
}

service auth {
  # Postfix smtp-auth
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
  }
}

その他の設定

▪️新規ユーザ追加時に、ホームディレクトリにMaildir形式のメールボックスを作成するようにする。

$ sudo mkdir -p /etc/skel/Maildir/{new,cur,tmp}
$ sudo chmod -R 700 /etc/skel/Maildir/

▪️ユーザーの作成

$ sudo useradd user
$ sudo passwd user

▪️証明書を指定

$ sudo vim /etc/postfix/main.cf

TLS を利用するための設定、証明書を指定する

smtpd_tls_cert_file = /etc/pki/tls/certs/server.crt
smtpd_tls_key_file = /etc/pki/tls/certs/server.key
smtpd_use_tls = yes

▪️SSLを使えるようにする

$ sudo vim /etc/dovecot/conf.d/10-ssl.conf
# (変更)
# SSL証明書
ssl_cert = </etc/pki/tls/certs/server.crt
ssl_key = </etc/pki/tls/certs/server.key
# (追記)
ssl = yes
ssl_key_password = "前述の証明書作成時のパスフレーズ"

▪️dovecotの起動

$ sudo service dovecot start
$ sudo chkconfig dovecot on

▪️postfixの再起動

$ sudo service postfix restart

おわりです。
うまくいかない場合は/var/log/maillogを確認してみてください。

Thunderbirdなどと連携できます。

17
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
9