Postfixのインストール
インストール
環境: Debian 11.1.0 Postfix asl2-bin amd64 2.1.27+dfsg-2.1+deb11u1 postfix amd64 3.5.13-0+deb11u1
参照したサイト https://www.server-world.info/query?os=Debian_11&p=mail&f=1
バイナリーパッケージのインストール #apt -y install postfix sasl2-bin
Postfix Configuration はNo Configuration を選択
参考にしたサイト:
Build Email Server From Scratch on Debian – Basic Postfix Setup
- 1.Set up a basic Postfix SMTP server
 - 2.Set up Dovecot IMAP server and TLS encryption
 - 3.Create Virtual Mailboxes with PostfixAdmin (MariaDB/MySQL, PostgreSQL)
 - 4.Create SPF and DKIM Records to Get Through Spam Filters
 - 5.Create DMARC Record to Protect Your Domain Reputation
 - 15.How to Host Multiple Mail Domains in PostfixAdmin
 
Tips
- 
Postfixのパラメーター myorigin について
 
From: ドメイン名は、myhostname パラメータではなく、Postfix の myorigin パラメータ (別名システム メール名) によって決定されます。
- 
TelnetでTCPポート25(outbound)がブロックされてるか確認
 
sudo apt install telnet
	sudo apt install telnet
	telnet gmail-smtp-in.l.google.com 25
	(Type in quit and press Enter to close the connection)
- 
Postfixで必要になる主なTCPポートは
 
sudo ufw allow 25,80,110,143,443,587,465,143,993,995/tcp
- 
TCPポートスキャン
 
https://www.ipvoid.com/port-scan/
- 
Postfix のバージョン確認(postconfコマンド)
 
	$ sudo postconf | grep mail_version
	mail_version = 3.5.13
	milter_macro_v = $mail_name $mail_version
- 
Postfixのデフォルトサイズの値確認(postconfコマンド)
 
	$ sudo postconf | grep size_limit
	body_checks_size_limit = 51200
	bounce_size_limit = 50000
	header_size_limit = 102400
	mailbox_size_limit = 51200000
	message_size_limit = 10240000
- 
postfix[2199]: fatal: bad string length 0 < 1: html_directory = のエラー
 
main.cf で html_directory = no
- 
dovecotでユーザ認証ができない場合
 
デフォルトがユーザーID500 以上です。/etc/dovecot/conf.d/10-mail.confにて確認しましょう。
	# Valid UID range for users, defaults to 500 and above. 
	# be done even if first_valid_uid is set to 0.
	first_valid_uid = 600
	#last_valid_uid = 0
dovecotのステータスの確認 sudo journalctl -eu dovecot
ログの確認 /var/log/mail.log
ログにも記録がない場合はdovecotのジャーナルを確認 sudo journalctl -eu dovecot
- 
dovecotでメールボックスを使えるユーザ一覧表示
 
sudo doveadm user '*'
- 
dovecotを自動的にリスタートさせる
 
新規にディレクトリを作成 sudo mkdir -p /etc/systemd/system/dovecot.service.d/
以下のリスタート設定ファイルを作成(dovecotが停止後5秒後に自動的にdovecotを再起動)
	sudo nano /etc/systemd/system/dovecot.service.d/restart.conf
	[Service]
	Restart=always
	RestartSec=5s
デーモンを再起動 sudo systemctl daemon-reload
動作検証 dovecotを強制停止 sudo pkill dovecot その後ステータス確認 systemctl status dovecot
- 
電子メールとSMTPサーバーの設定を確認
 
email checker で確認します。 https://network-tools.com/email-tests/
- 
Virtualドメインの設定
 
/etc/postfix/main.cf に以下の行を追加
virtual_alias_maps = hash:/etc/postfix/virtual
mydomain mydestination に指定したドメインを virtual_alias_domains に含めない、メール配送先ユーザーアカウントが存在することが条件です。
	/etc/postfix/virtual に以下の内容を記載
	# Virtual Domains
	hogehoge.jp     virtual
	# Virtual alias
	[email protected]      user1
	[email protected]    user2
# postmap /etc/postfix/virtual
ハッシュ化された中身を見る場合は # strings /etc/postfix/virtual.db
- 
Apacheのデフォルトドメインを停止する(仮想ドメインを使う場合)
 
他の仮想ホストに干渉する可能性があるため、デフォルトの仮想ホストを無効にします。
sudo a2dissite 000-default.conf
- 
CertbotコマンドでLetsencryptの証明書を取得(Apacheの場合)
 
	$ sudo certbot certonly -a apache --agree-tos --no-eff-email --staple-ocsp --email 名前@gmail.com -d mail.ドメイン名.biz
	Saving debug log to /var/log/letsencrypt/letsencrypt.log
	Account registered.
	Requesting a certificate for mail.ドメイン名.biz
	Successfully received certificate.
	Certificate is saved at: /etc/letsencrypt/live/mail.ドメイン名.biz/fullchain.pem
	Key is saved at:         /etc/letsencrypt/live/mail.ドメイン名.biz/privkey.pem
- 
CertbotコマンドでLetsencryptの証明書を取得(Nginxの場合)
 
	$ sudo certbot certonly -a nginx --agree-tos --no-eff-email --staple-ocsp --email 名前@gmail.com -d mail.ドメイン名.biz
	Saving debug log to /var/log/letsencrypt/letsencrypt.log
	Account registered.
	Requesting a certificate for mail.ドメイン名.biz
	Successfully received certificate.
	Certificate is saved at: /etc/letsencrypt/live/mail.ドメイン名.biz/fullchain.pem
	Key is saved at:         /etc/letsencrypt/live/mail.ドメイン名.biz/privkey.pem
- 
Certbotの証明書再発行を自動化
 
crontabに毎日certbot renew を実行し、postfixとdovecot を再起動、Apacheの場合
	sudo crontab -e
	@daily certbot renew --quiet && systemctl reload postfix dovecot apache2
Nginxの場合
	sudo crontab -e
	@daily certbot renew --quiet && systemctl reload postfix dovecot nginx
- 
PostfixとDovecotで複数のドメインの証明書を設定
 
一つのTLS証明書で複数のドメインの証明書をまとめる場合
TLS証明書を取得 Apacheの場合
sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp -d mail.ドメイン1.com,mail.ドメイン2.com --cert-name mail.ドメイン1.com --email [email protected]
TLS証明書を取得 Nginxの場合
sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp -d mail.ドメイン1.com,mail.ドメイン2.com --cert-name mail.ドメイン1.com --email [email protected]
Certbotで既存の証明書を更新して新しいドメインを含めるかどうかを尋ねられたら、U (Update)と答えて Enter キーを押します。
※ 上記のコマンドでは、最初のメール ドメイン1を使用して証明書名を指定したことに注意してください。これはファイル パスで使用されるため、Postfix または Dovecot 構成ファイルでファイル パスを変更する必要はありません。
	sudo systemctl reload apache2
	sudo systemctl reload nginx 
	sudo systemctl reload postfix dovecot
各ドメインごとでTLS証明書を取得して設定する場合(複数のTLS証明書)
この設定はまだ動作未検証です!
次のコマンドを使用するだけで、2 番目のメール ドメイン用の別の TLS 証明書を取得できます。
Apacheの場合 sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp -d mail.ドメイン2.com
Nginxの場合 sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp -d mail.ドメイン2.com
1. Postfixの設定(Postfixバージョン3.4以上)
sudo nano /etc/postfix/main.cf
最後に以下の行を追加
tls_server_sni_maps = hash:/etc/postfix/sni_maps
sudo nano /etc/postfix/sni_maps
	mail.ドメイン1.com   /etc/letsencrypt/live/mail.ドメイン1.com/privkey.pem    /etc/letsencrypt/live/mail.ドメイン1.com/fullchain.pem
	mail.ドメイン2.com   /etc/letsencrypt/live/mail.ドメイン2.com/privkey.pem     /etc/letsencrypt/live/mail.ドメイン2.com/fullchain.pem
※ 最初のドメインの証明書が /etc/postfix/main.cf ファイルで既に使用されている場合は、それを sni_maps ファイルに再度含める必要はありません。
sudo postmap -F /etc/postfix/sni_maps
sudo systemctl restart postfix
2. Dovecotの設定
sudo nano /etc/dovecot/conf.d/10-ssl.conf
既存の以下の行を変更します。
	ssl_cert =</etc/letsencrypt/live/mail.ドメイン1.com/fullchain.pem 
	ssl_key =</etc/letsencrypt/live/mail.ドメイン1.com/privkey.pem
次のように変更します。
	local_name mail.ドメイン1.com {
	ssl_cert =</etc/letsencrypt/live/mail.ドメイン1.com/fullchain.pem
	ssl_key =</etc/letsencrypt/live/mail.ドメイン1.com/privkey.pem
	}
	local_name mail.ドメイン2.com {
	ssl_cert =</etc/letsencrypt/live/mail.ドメイン2.com/fullchain.pem
	ssl_key =</etc/letsencrypt/live/mail.ドメイン2.com/privkey.pem
	}
sudo systemctl restart dovecot
- 
マイクロソフト社のOutlookをメールソフトとして使う場合のSMTPサブミッションサービスの設定(master.cf)
 
一般的な設定とOutlook用を追加します。 sudo vim /etc/postfix/master.cf
一般的な設定
submission inet n - y - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_security_level=encrypt -o smtpd_tls_wrappermode=no -o smtpd_sasl_auth_enable=yes -o smtpd_relay_restrictions=permit_sasl_authenticated,reject -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject -o smtpd_sasl_type=dovecot -o smtpd_sasl_path=private/auth
Outlook用の設定
smtps inet n - y - - smtpd -o syslog_name=postfix/smtps -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes -o smtpd_relay_restrictions=permit_sasl_authenticated,reject -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject -o smtpd_sasl_type=dovecot -o smtpd_sasl_path=private/auth
- 
メールサイズとメールボックス容量の設定
 
	message_size_limit = 20480000
	メッセージサイズを20MBに変更
	mailbox_size_limit = 0
	メールボックス容量を無制限に。message_size_limit を変更する場合、mailbox_size_limit を超えないようにしないといけない。
	message_size_limit が mailbox_size_limit より大きい場合、メールが一切受信できなくなる。
	mailbox_size_limit のデフォルトは51200000(50MB)。




