クラウドインテグレーションサービス「雲斗」のブログ

芝公園にある創研情報株式会社がAWS を 中心にクラウドの基本から便利な使いかたまでをお伝えしていきます。

Amazon EC2

EC2上にPostfixサーバを構築

2017/04/21

今回はメールサーバ構築のため、EC2(AmazonLinux最新版)にPostfixをインストールしますよ~

【Postfixインストール/送信設定】

・EC2のセキュリティグループで25番ポート(SMTP)と110番ポート(POP3)からのアクセスを許可する

・SSH接続
・Postfixのインストール

$ sudo yum install postfix

・デフォルトで起動しているsendmailを停止する(自動起動設定もOFFにする)

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

⇒起動設定の確認

$ chkconfig --list sendmail

以下のように表示されていればきちんと設定されている

sendmail  0:off 1:off 2:off 3:off 4:off 5:off 6:off

・選択MTAをPostfixに指定

$ sudo alternatives --config mta

・2のpostfixを選択。(左の+マークがついているものが現在選択されているもの)

There are 2 programs which provide 'mta'.Selection Command
-----------------------------------------------
* 1 /usr/sbin/sendmail.sendmail
+ 2 /usr/sbin/sendmail.postfixEnter to keep the current selection[+], or type selection number:2 ←2を選択

・chkconfigコマンドでpostfixの起動設定を行う

$ sudo chkconfig --add postfix
$ sudo chkconfig postfix on

⇒設定確認

$ chkconfig --list postfix

以下のように表示されていればきちんと設定されている

postfix  0:off 1:off 2:on 3:on 4:on 5:on 6:off

・main.cfで設定変更

$ sudo vim /etc/postfix/main.cf

⇒inet_interfaces = allをコメントアウト
・postfixを停止

$ sudo service postfix stop

・postfixを起動する

$ sudo service postfix start

・メール送信テスト

$ sendmail foo@foo.com <=宛先のメールアドレス指定
From:hoge@hoge.com  <=差出人(from)ヘッダを指定
To:foo@foo.com  <=宛先(To)ヘッダを指定
Subject:test  <=件名(subject)ヘッダを指定
テスト送信です。   <=内容(body)を記入.     <=bodyの入力が終わったら「.」を入力

⇒メールアドレス(上記の例だとfoo@foo.com)にメールが送信されているか確認
・ログからも送信確認

$ sudo cat /var/log/maillog

⇒「status=sent」になっていれば、メール送信できている。

送信は完了

【受信設定】

・/etc/postfix/main.cf で設定変更

$ sudo vim /etc/postfix/main.cf

⇒以下の内容を変更する

myhostname = ホスト名
mydomain = ドメイン名
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
home_mailbox = Maildir/
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

・/etc/postfix/master.cf で設定変更
以下の3行をコメントアウトする

submission inet n - n - - smtpd
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject

・SASL認証を使用できる状態にする

$ sudo service saslauthd start
$ sudo chkconfig saslauthd on

・/etc/sasl2/smtpd.confの中身を書き換える

$ sudo vim /etc/sasl2/smtpd.conf

⇒pwcheck_method: auxpropに書き換え

・ユーザの作成

$ sudo useradd user

・パスワードの設定

$ sudo passwd user

・メールディレクトリの作成

$ su - user
$ mkdir Maildir

・ログアウト

$ exit

・postfixで設定したメールアドレス宛てにメールを送る(上記の場合、user@ドメイン名)
⇒/home/user/Maildir/newにメールが届いていれば受信成功

以上でpostfixサーバの構築が完了です!

以下ふちがハマったこと-----------------------------------------------------------------------------------------------------------------------
sendmailコマンドに失敗し、「postdrop: warning: unable to look up public/pickup: No such file or directory」というメッセージが表示されたら・・・
1.postfixを再起動

$ sudo service postfix restart

2.・以下のようになれば再起動できている

Shutting down postfix:[OK]
Starting postfix:[OK]

3.もう一度sendmailコマンドでメール送信を行う

もし2の再起動ができていない場合・・・

Shutting down postfix:[FAILED]
Starting postfix:[OK]

4.sendmailが停止していることを確認

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

以下のように表示されることを確認

sendmail  0:off 1:off 2:off 3:off 4:off 5:off 6:off

5.postfixを停止

$ sudo service postfix stop

6.postfixを起動

$ sudo service postfix start

7.postfixを再起動

$ sudo service postfix restart

8.もう一度sendmailコマンドでメール送信を行う

以上でPostfixサーバ構築完了です(゜◇゜)ゞ

1/5 EC2上にApacheサーバを構築
2/5 ApacheサーバにHTTPSアクセスを追加
3/5 EC2上にMySQLサーバを構築
4/5 EC2上にPostfixサーバを構築
5/5 EC2上にZabbixエージェントをインストール

◆クラウドインテグレーションサービス「雲斗」以下のページからアクセス出来ます。

-Amazon EC2

Bitnami