メールサーバ構築
環境情報
・さくらのVPS CentOS7
# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
準備
さくらインターネットの会員メニューのゾーン情報でMXレコードに独自ドメインが割り当てられているかの確認も必要です。
また、さくらインターネット以外で取得したドメインの場合はレジストラ側でDNSの設定変更が必要です。
設定に必要な情報を用意する
下記の情報を用意します。
・独自ドメイン
・ホスト名
お使いのサーバのホスト名はnetwork managerのテキストUIで設定できます。
設定の際は下記の形式で入力して下さい。
webサーバなら「www」とかメールサーバなら「mail」とかがよくありますね。
hostname.example.com
ホスト名.ドメイン名
network manager TUI を起動するコマンド
# nmtui
必要なソフトウェアが入っているか確認する。
さくらのVPSだとpostfixは標準でインストールされているようです。
$ rpm -q postfix fix
postfix-2.10.1-6.el7.x86_64
管理者権限に移行しDovecotとcyrus-saslをインストールして下さい。
$ su -
# yum -y install dovecot
# yum -y install cyrus-sasl
firewalldの設定を確認・変更する
次にfirewalldのステータスを確認します。
ACTIVE(running)と表示されれば正常に稼働しています。
# systemctl status firewalld.service
メールサーバ構築にあたり必要なプロトコルをfirewalld 許可リストに追加します。
暗号化されたサービスのみの追加するので110や143は追加しません。
# firewall-cmd --add-service=smtp --zone=public --permanent
success
# firewall-cmd --permanent --add-service=pop3s --zone=public
success
# firewall-cmd --permanent --add-service=imaps --zone=public
success
# firewall-cmd --permanent --add-port=465/tcp --zone=public
success
# firewall-cmd --reload
success
# firewall-cmd --list-services --permanent --zone=public
dhcpv6-client http ssh smtp pop3s imaps
SELinuxの設定を確認する
続けて、SELinuxの設定を確認します。
SELinuxが有効になっている場合、メールの送受信の妨げになる可能性があるのでステータスを確認します。
$ sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
~略~
$ getenforce
Permissive
getenforceの結果が enforcing(有効)の場合はselinuxにpostfixのメールスプールへの書き込みが許可されているか確認します。
下記のコマンドでonになっていれば書き込みOKです。
(getenforce の結果が Permissive、またはdisableの場合は確認してもいいですが、対応不要です。)
getsebool -a | grep postfix
postfix_local_write_mail_spool --> on
Postfixの設定
次にpostfixの設定を行います。
まず現在のステータスを確認します。
こちらもACTIVE(running)となっていればOKです。
# systemctl status postfix
postfixの主な設定ファイルは /etc/postfix の配下にあります。
$ cd /etc/postfix/
$ ls
access generic main.cf relocated virtual
canonical header_checks master.cf transport
準備した情報を基に/etc/postfix/main.cfを編集します。
vi /etc/postfix/main.cf
76 #myhostname = virtual.domain.tld
↓
76 myhostname = mail.example.com
84 #mydomain = domain.tld
↓
84 mydomain = example.com
100 #myorigin = $myhdomain
↓
100 myorigin = $mydomain
115 #inet_interfaces = all
116 #inet_interfaces = $myhostname
117 #inet_interfaces = $myhostname, localhost
118 inet_interfaces = localhost
↓
115 inet_interfaces = all
116 #inet_interfaces = $myhostname
117 #inet_interfaces = $myhostname, localhost
118 #inet_interfaces = localhost
166 mydestination = $myhostname, localhost.$mydomain, localhost
167 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
168 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
169 # mail.$mydomain, www.$mydomain, ftp.$mydomain
↓
166 #mydestination = $myhostname, localhost.$mydomain, localhost
167 mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
168 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
169 # mail.$mydomain, www.$mydomain, ftp.$mydomain
210 #local_recipient_maps = unix:passwd.byname $alias_maps
↓
210 local_recipient_maps = unix:passwd.byname $alias_maps
421 #home_mailbox = Maildir/
↓
421 home_mailbox = Maildir/
ファイルの末尾に下記を追記する。
# message_size_limit
message_size_limit = 2048000 ← ココに任意の最大容量を記載
684 # Enable SMTP Auth
685 smtpd_sasl_auth_enable = yes
686 smtpd_sasl_security_options = noanonymous
687 broken_sasl_auth_clients = yes
688 smtpd_recipient_restrictions = permit_mynetworks, permit_auth_destination, permi t_sasl_authenticated, reject
[:wq]で保存してファイルを閉じてください。
Dovecotの設定
/etc/dovecot/dovecot.conf の編集を行います。
# vi /etc/dovecot/dovecot.conf
24 #protocols = imap pop3 lmtp
↓
24 protocols = imap pop3 lmtp
[:wq]で保存してファイルを閉じてください。
次に/etc/dovecot/conf.d/10-mail.conf の編集を行います。
# vi /etc/dovecot/conf.d/10-mail.conf
30 #mail_location =
↓
30 mail_location = maildir:~/Maildir
[:wq]で保存してファイルを閉じてください。
次に/etc/dovecot/conf.d/10-auth.conf の編集を行います。
# vi /etc/dovecot/conf.d/10-auth.conf
10 #disable_plaintext_auth = yes
↓
10 disable_plaintext_auth = no
100 auth_mechanisms = plain
↓
100 auth_mechanisms = plain login
[:wq]で保存してファイルを閉じてください。
次に /etc/dovecot/conf.d/10-master.conf の編集を行います。
vi /etc/dovecot/conf.d/10-master.conf
17 service imap-login {
18 inet_listener imap {
19 #port = 143
20 }
21 inet_listener imaps {
22 #port = 993
23 #ssl = yes
24 }
↓
17 service imap-login {
18 inet_listener imap {
19 port = 143
20 }
21 inet_listener imaps {
22 port = 993
23 ssl = yes
24 }
38 service pop3-login {
39 inet_listener pop3 {
40 #port = 110
41 }
42 inet_listener pop3s {
43 #port = 995
44 #ssl = yes
45 }
46 }
↓
38 service pop3-login {
39 inet_listener pop3 {
40 port = 110
41 }
42 inet_listener pop3s {
43 port = 995
44 ssl = yes
45 }
46 }
95 # Postfix smtp-auth
96 #unix_listener /var/spool/postfix/private/auth {
97 # mode = 0666
98 }
99
100
↓
95 # Postfix smtp-auth
96 unix_listener /var/spool/postfix/private/auth {
97 mode = 0666
98 user = postfix
99 group = postfix
100 }
[:wq]で保存してファイルを閉じてください。
通信の暗号化設定
# cd /etc/pki/tls/certs
# openssl genrsa -des3 1024 > mailserver.key
※パスフレーズの入力を求められるので任意のパスフレーズを2回入力して下さい。(後で消すのでなんでもいいです。)
暗号鍵の所有者をrootに設定する
# chown root.root mailserver.key
# chmod 640 mailserver.key
パスフレーズの削除
openssl rsa -in mailserver.key -out mailserver.key
※先ほど設定したパスフレーズを入力する。
証明書を作成する
下記のコマンドを実行し必要な情報を入力して下さい。
# openssl req -utf8 -new -key mailserver.key -x509 -days 3650 -out mailserver.pem -set_serial 0
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:JP ←国
State or Province Name (full name) []:Tokyo ←都道府県
Locality Name (eg, city) [Default City]:kitaku 市区町村
Organization Name (eg, company) [Default Company Ltd]:example company ←組織名
Organizational Unit Name (eg, section) []:example ←組織の部署名(空欄可)
Common Name (eg, your name or your server's hostname) []:example.com ←メールサーバ名(ドメイン名で良い)
Email Address []:hoge@gmail.com ←メールアドレス
暗号化の設定をPostfixに反映させる
/etc/postfix/main.cfの編集を行います。
vi /etc/postfix/main.cf
ファイルの末尾に下記を追記する。
690 smtpd_use_tls = yes
691 smtpd_tls_cert_file = /etc/pki/tls/certs/mailserver.pem
692 smtpd_tls_key_file = /etc/pki/tls/certs/mailserver.key
693 smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache
[:wq]で保存してファイルを閉じてください。
/etc/postfix/master.cf の編集を行います。
vi /etc/postfix/master.cf
26 #smtps inet n - n - - smtpd
27 # -o syslog_name=postfix/smtps
28 # -o smtpd_tls_wrappermode=yes
29 # -o smtpd_sasl_auth_enable=yes
↓
26 smtps inet n - n - - smtpd
27 -o syslog_name=postfix/smtps
28 -o smtpd_tls_wrappermode=yes
29 -o smtpd_sasl_auth_enable=yes
Dovecotの暗号化を有効化する
/etc/dovecot/conf.d/10-ssl.confの編集を行います。
vi /etc/dovecot/conf.d/10-ssl.conf
14 ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
15 ssl_key = </etc/pki/dovecot/private/dovecot.pem
↓
14 ssl_cert = </etc/pki/tls/certs/mailserver.pem
15 ssl_key = </etc/pki/tls/certs/mailserver.key
各サービスを有効化する
設定変更を反映させる為、各サービスを再起動します。
インストール後でサービスがまだ開始されていない場合はrestartをstartに変えて実行しstatusをチ確認してみて下さい。
# systemctl restart postfix
# systemctl restart dovecot
# systemctl restart saslauthd
自動起動を有効化する
postfixは既に有効化されているかもしれませんが、とりあえず全部やっときましょう。
# systemctl enable postfix
# systemctl enable postfix
# systemctl enable postfix
Tips
もしpostfixのstatusを確認して正常に起動していない場合は設定に誤りがある可能性があります。
例)ホスト名やDNSの設定が誤っている場合等