LoginSignup
20
23

More than 3 years have passed since last update.

【EC2】メールサーバ構築(Postfix + Dovecot)

Posted at

Postfixとは

  • オープンソースの送信用メールサーバー用ソフトウェア
  • メールを送信(配送)するときに利用(SMTPサーバー)
  • 受信機能なし

Dovecotとは

  • オープンソースの受信用メールサーバー用ソフトウェア
  • POP/IMAPサーバー

手順

1)Route53の設定

  • 前提:今回はホストゾーンがすでに作られている
  • メールサーバのAレコードとMXレコードを追加
      ホストゾーン:test.site.
      Aレコード
    名前:mail.test.site.
    値:xx.xxx.xxx.xxx (メールサーバのパブリックIP)

    MXレコード
    名前:空欄
    値:<プライオリティ> <メールサーバのFQDN>
    10 mail.test.site.
    ※. 最後のピリオドを忘れないように

  • ローカルのMac上でdigで登録したレコードが正しく引けるかを確認

$ dig mx test.site.
(略)
;; QUESTION SECTION:
;test.site.         IN  MX

;; ANSWER SECTION:
test.site.      60  IN  MX  10 mail.test.site.

;; ADDITIONAL SECTION:
mail.test.site. 60  IN  A   <メールサーバのパブリックIP>
(略)

参考:Amazon Route53 にMXレコード登録する

2)セキュリティグループの設定

SMTP(25):0.0.0.0/0
IMAPS(993):0.0.0.0/0
POP3(110):0.0.0.0/0
SMTPS(465):0.0.0.0/0
POP3S(995):0.0.0.0/0

3)EC2にPostfixをインストールし設定

  • Amazon Linux 2の場合、デフォルトでPostfixがインストールされてるっぽい
$ yum list installed postfix
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
インストール済みパッケージ
postfix.x86_64                                          2:2.10.1-6.amzn2.0.3                                           installed
  • postfixをインストールする場合はこちら
$ sudo yum -y install postfix
  • postfixの起動を確認
$ systemctl status postfix
    →Active: active (running)
  • postfixの自動起動の確認(Amazon Linux 2はデフォルトで自動起動)
$ systemctl is-enabled postfix
enabled
  • /etc/postfix/main.cf を設定
$ sudo su -
# vi /etc/postfix/main.cf
----------------
以下はコメントアウト
# mydestination = $myhostname, localhost.$mydomain, localhost
# inet_interfaces = localhost

#inet_protocols = all
inet_protocols = ipv4

以下を追加
# add
myhostname = mail.test.site
mydomain = test.site
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
inet_interfaces = all
mynetworks = 10.0.0.0/16, 127.0.0.0/8
relay_domains = $mydestination
home_mailbox = Maildir/
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions =
    permit_mynetworks
    permit_sasl_authenticated
    reject_unauth_destination
----------------

参考:Postfix+Dovecotによるメールサーバ構築

  • saslauthdの起動
# yum list installed | grep sasl
cyrus-sasl-lib.x86_64                 2.1.26-23.amzn2                installed
cyrus-sasl-plain.x86_64               2.1.26-23.amzn2                installed

# yum install cyrus-sasl cyrus-sasl-md5
# yum list installed | grep sasl
cyrus-sasl.x86_64                     2.1.26-23.amzn2                @amzn2-core *追加
cyrus-sasl-lib.x86_64                 2.1.26-23.amzn2                installed
cyrus-sasl-md5.x86_64                 2.1.26-23.amzn2                @amzn2-core *追加
cyrus-sasl-plain.x86_64               2.1.26-23.amzn2                installed

参考:Amazon Linux2 + Postfix + Dovecot + Let's EncryptでSSLメールサーバを構築

# systemctl status saslauthd
# systemctl start saslauthd
# systemctl status saslauthd
   → Active: active (running)
# systemctl is-enabled saslauthd
# systemctl enable saslauthd
# systemctl is-enabled saslauthd
enabled
# alternatives --display mta
mta - status is auto.
link currently points to /usr/sbin/sendmail.postfix *postfxが利用されてる。postfxであればOK 。Sendmailになってれば変更
/usr/sbin/sendmail.postfix - priority 30
slave mta-mailq: /usr/bin/mailq.postfix
slave mta-newaliases: /usr/bin/newaliases.postfix
slave mta-pam: /etc/pam.d/smtp.postfix
slave mta-rmail: /usr/bin/rmail.postfix
slave mta-sendmail: /usr/lib/sendmail.postfix
slave mta-mailqman: /usr/share/man/man1/mailq.postfix.1.gz
slave mta-newaliasesman: /usr/share/man/man1/newaliases.postfix.1.gz
slave mta-sendmailman: /usr/share/man/man1/sendmail.postfix.1.gz
slave mta-aliasesman: /usr/share/man/man5/aliases.postfix.5.gz
Current `best' version is /usr/sbin/sendmail.postfix.
  • postfixの再起動
# systemctl restart postfix
# systemctl status postfix
  • メールアドレスとして利用するためのユーザを作成
# useradd hoge
# passwd hoge
# su - hoge
$ mkdir Maildir
$ pwd
/home/hoge
$ ll
total 0
drwxrwxr-x 2 cloudpack cloudpack 6 Aug 16 05:02 Maildir

4)EC2にDovecotをインストールし設定

  • dovecotをインストール
# yum -y install dovecot
  • dovecotの自動起動設定
# systemctl enable dovecot
# systemctl is-enabled dovecot
enabled
  • /etc/dovecot/conf.d/10-mail.confの設定
    • メールの保存場所をホームディレクトリのMaildirに設定。
    • 認証方法にloginを追加。
# vi /etc/dovecot/conf.d/10-mail.conf
(略)以下を追加
mail_location = maildir:~/Maildir
auth_mechanisms = plain login
  • /etc/dovecot/dovecot.confの設定
# vi /etc/dovecot/dovecot.conf
↓のコメントアウトを外す
protocols = imap pop3 lmtp
  • /etc/dovecot/conf.d/10-master.conf の設定
# vi /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 {
(略)
    mode = 0666
    user = postfix
    group = postfix
  }
  • dovecotを起動
# systemctl start dovecot
# systemctl status dovecot

5)メール受信の確認

  • Gmailなどから「hoge@test.site」にテストメールを送信
  • telnetを使ってメール受信を確認
# telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
user hoge      *入力
+OK
pass hoge     *入力
+OK Logged in.  *ログインOK
list *入力 *メール一覧取得
+OK 1 messages:
1 3049
.
retr 1 *入力 *1番目のメールを表示
+OK 3049 octets

(テストメールの内容が表示される)

.
quit *入力
+OK Logging out.
Connection closed by foreign host.

6)メール送信の確認

  • telnetを使ってGmailにメール送信(localhost)
# telnet localhost 25
helo localhost
mail from: user01@mail.test.site (適当なアドレス。存在しないアドレスでもOK.)
rcpt to: <自分のGmailなど>
data
From: user01@mail.test.site
To: <自分のGmailなど>
Subject: test
X-Mailer: TEST MAILER
test mail
.
quit

 →少し待てばGmailなどの迷惑メールに届いてるはず

20
23
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
20
23