環境 CentOS Stream release 9 Apache/2.4.57 (CentOS Stream) Xserver VPS
WEBサーバーとメールサーバーが同一サーバー内にある前提
認証は Dovecot 側で UNIX PAM 認証とする
DNS レコードの設定
| ホスト名 | 種別 | 内容 | TTL | 優先度 |
|---|---|---|---|---|
| example.com | A | 162.xx.xx.xx | 3600 | – |
| example.com | MX | mail.example.com | 3600 | 10 |
| example.com | TXT | v=spf1 mx ~all | 3600 | – |
| mail.example.com | TXT | v=spf1 ip4:162.xx.xx.xx ~all | 3600 | – |
メールサーバー用ディレクトリの作成
mkdir -p /var/www/html/mail && chown -R www:www /var/www/html/mail && chmod 705 /var/www/html/mail
mail.example.com 用の独自 conf 作成
vi /etc/httpd/conf.d/mail.conf
下記を貼り付ける(キー取得用)
<VirtualHost *:80>
DocumentRoot /var/www/html/mail
ServerName mail.example.com
# for Let's Encrypt
Alias /.well-known /var/www/html/mail/.well-known
<Directory /var/www/html/mail>
Require all granted
</Directory>
</VirtualHost>
certbot インストール
dnf -y install certbot
キー取得
certbot certonly --webroot -w /var/www/html/mail -m info@example.com -d mail --agree-tos
各キーが生成されているのを確認
ls /etc/letsencrypt/live/mail.example.com
README cert.pem chain.pem fullchain.pem privkey.pem
mail.conf の編集
vi /etc/httpd/conf.d/mail.conf
下記を貼り付ける
<VirtualHost *:80> ServerName mail.example.com ServerAdmin info@example.com DocumentRoot "/var/www/html/mail" <Directory "/var/www/html/mail"> AllowOverride All Allow from all </Directory> ErrorLog logs/mail-error_log CustomLog logs/mail-access_log combined </VirtualHost>
ssl.confの設定
vi /etc/httpd/conf.d/ssl.conf
## SSL Virtual Host Context 以降をすべて削除してある前提
下記を貼り付ける
<VirtualHost *:443> SSLEngine on SSLProtocol All -SSLv2 -SSLv3 -TLSv1 ServerName mail.example.com DocumentRoot "/var/www/html/mail" SSLCertificateFile /etc/letsencrypt/live/mail.example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/mail.example.com/privkey.pem <Directory "/var/www/html/mail"> AllowOverride All Allow from all </Directory> ErrorLog logs/mail-ssl_error_log CustomLog logs/mail-ssl_access_log combined </VirtualHost>
httpd リスタート
systemctl restart httpd
確認用 php ファイル作成
echo "<?php phpinfo(); ?>" > /var/www/html/mail/index.php
ブラウザから https://mail.example.com でアクセスできれば OK
Postfix(送信サーバー)、Dovecot(受信サーバー)、cyrus-sasl(SMTP 認証用)のインストール
dnf -y install postfix dovecot cyrus-sasl
Postfix 設定ファイル編集
vi /etc/postfix/main.cf
#myhostname = host.domain.tld #↓ myhostname = mail.example.com # FQDNを指定 #mydomain = domain.tld #↓ mydomain = example.com # ドメインを指定) #myorigin = $mydomain #↓ myorigin = $mydomain inet_interfaces = localhost #↓ inet_interfaces = all # 外部からのメール受信を許可 inet_protocols = all #↓ inet_protocols = ipv4 mydestination = $myhostname, localhost.$mydomain, localhost #↓ mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain #local_recipient_maps = unix:passwd.byname $alias_maps #↓ local_recipient_maps = proxy:unix:passwd.byname $alias_maps #mynetworks = 168.100.189.0/28, 127.0.0.0/8 #↓ mynetworks = 192.168.0.0/24, 127.0.0.0/8 #relay_domains = $mydestination #↓ relay_domains = $mydestination #alias_maps = dbm:/etc/aliases #↓ alias_maps = hash:/etc/aliases #home_mailbox = Maildir #↓ home_mailbox = Maildir/ #luser_relay = admin+$local #↓ luser_relay = unknown_user@localhost #smtpd_banner = $myhostname ESMTP $mail_name ($mail_version) #↓ smtpd_banner = $myhostname ESMTP unknown #smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem #↓ smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem #smtpd_tls_key_file = /etc/pki/tls/private/postfix.key #↓ smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem # 最終行へ下記を追加(SMTP認証設定) smtpd_sasl_auth_enable = yes broken_sasl_auth_clients = yes smtpd_sasl_type = dovecot smtpd_sasl_path = private/auth smtp_sasl_security_options = smtpd_sasl_local_domain=$myhostname smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
master.cf 編集
vi /etc/postfix/master.cf
smtps inet n - n - - smtpd # -o syslog_name=postfix/smtps -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes # -o smtpd_reject_unlisted_recipient=no # -o smtpd_client_restrictions=$mua_client_restrictions # -o smtpd_helo_restrictions=$mua_helo_restrictions # -o smtpd_sender_restrictions=$mua_sender_restrictions -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject # -o milter_macro_daemon_name=ORIGINATING
ルックアップテーブル変更を反映
postmap /etc/postfix/access
Postfix 起動と自動起動設定
systemctl start postfix && systemctl enable postfix
Dovecot 設定
vi /etc/dovecot/dovecot.conf
#protocols = imap pop3 lmtp submission #↓ protocols = imap
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
}
}
# SASL 認証ソケットファイルを指定
service auth {
(略)
# Postfix smtp-auth
#unix_listener /var/spool/postfix/private/auth {
# mode = 0666
#}
#↓
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
}
}
vi /etc/dovecot/conf.d/10-auth.conf
#auth_mechanisms = plain #↓ auth_mechanisms = plain login
vi /etc/dovecot/conf.d/10-ssl.conf
# SSL接続有効化 ssl = no #↓ ssl = yes # 証明書と秘密キーを指定 ssl_cert = </etc/letsencrypt/live/mail.example.com/fullchain.pem ssl_key = </etc/letsencrypt/live/mail.example.com/privkey.pem
vi /etc/dovecot/conf.d/10-mail.conf
#mail_location = #↓ Maildir形式にする mail_location = maildir:~/Maildir
ログの出力先を変更
vi /etc/dovecot/conf.d/10-logging.conf
#log_path = syslog ↓ log_path = /var/log/dovecot/dovecot.log
ログ出力先のディレクトリ作成
mkdir /var/log/dovecot
Dovecot 起動、自動起動設定
systemctl start dovecot && systemctl enable dovecot
証明書の自動更新設定
crontab -e
毎朝 5 時に証明書を自動更新し Postfix と Dovecot をリロード
0 5 * * * certbot renew -q --deploy-hook "systemctl restart postfix dovecot"
Postfix ログの設定
出力先ディレクトリ作成
mkdir /var/log/mail
出力先の変更
vi /etc/rsyslog.conf
mail.* -/var/log/maillog #↓変更 mail.* -/var/log/mail/maillog
syslog リスタート
systemctl restart rsyslog
不要なログを削除
rm -f /var/log/maillog*
ローテーション設定
vi /etc/logrotate.d/rsyslog
# 下記を削除 /var/log/maillog
vi /etc/logrotate.d/maillog
# 下記を追加
/var/log/mail/maillog {
daily
missingok
dateext
rotate 30
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
Dovecot ログの設定
vi /etc/logrotate.d/dovecot
vi /etc/logrotate.d/dovecot
# 下記を追加
/var/log/dovecot/dovecot.log {
daily
missingok
dateext
rotate 30
sharedscripts
postrotate
/bin/kill -USR1 `cat /var/run/dovecot/master.pid 2>/dev/null` 2> /dev/null || true
endscript
}
saslauthd 起動、自動起動設定
systemctl start saslauthd && systemctl enable saslauthd
ファイアーウォールの設定(送信用に 465、受信用に 993 のポートを解放)
firewall-cmd --add-service={smtps, imaps} --zone=public --permanent && firewall-cmd --reload
ユーザー追加
UNIX ユーザーがそのままメールユーザーとなる
試しに info というユーザーを追加するが info は特別なユーザー名でデフォルトでは追加できないので下記の手順を踏む
vi /etc/aliases
info をコメントアウトする
#info: postmaster
変更を反映
newaliases
あらためて info ユーザー追加
useradd -s /sbin/nologin info
パスワード設定
passwd info
パスワード入力、Enter を繰りかえして
passwd: 全ての認証トークンが正しく更新できました
が表示されれば OK
あとは Thunderbird 等のメールソフトで設定すればいい
UNIX ユーザー名がそのままメールソフトで設定するユーザー名になる
送信側
| 送信(SMTP)サーバー | |
|---|---|
| サーバー名 | mail.example.com |
| ポート | 465 |
| 接続の保護 | SSL/TSL |
| 認証方式 | 通常のパスワード認証 |
| ユーザー名 | info |
受信側
| サーバーの種類 IMAP サーバー | |
|---|---|
| サーバー名 | mail.webrydays.net |
| ポート | 993 |
| ユーザー名 | info |
| 接続の保護 | SSL/TSL |
| 認証方式 | 通常のパスワード認証 |
