Centos 7 自建邮件服务器 前提准备 Https 网站域名
详细搭建 https 参考链接 ➡
添加 MX 记录
优先级 : 通常设置为 10
或 20
,数字越小优先级越高。
验证 MX 记录 1 dig MX mail.wyy.icu # mail.wyy.icu 为你的邮件服务 域名
证书文件 cert
1 /www/server/cert/email.wyy.icu/fullchain.pem
key
1 /www/server/cert/email.wyy.icu/privkey.pem
添加权限 1 2 sudo chmod 640 /www/server/cert/email.wyy.icu/fullchain.pem sudo chmod 640 /www/server/cert/email.wyy.icu/privkey.pem
服务端口 云服务器控制台防火墙
+ 服务器端口
确保两者都要打开
1 2 3 4 5 6 7 8 9 80 # http 443 # https 143 # IMAP 993 # IMAP(SSL) 25 # SMTP 发件服务 110 # POP3 收件服务 465 # SMTP(SSL) 587 # SMTP(SSL) 995 # POP3(SSL)
服务器端口
查看(Centos 7)
firewalld
1 2 3 4 5 6 7 8 # 查看现有规则 sudo firewall-cmd --list-all # 允许 465 端口 sudo firewall-cmd --permanent --add-port=465/tcp sudo firewall-cmd --reload
iptables
1 2 3 4 5 6 7 # 查看现有规则 sudo iptables -L # 允许 465 端口 sudo iptables -A INPUT -p tcp --dport 465 -j ACCEPT
开始搭建 安装 postfix
1 sudo yum install postfix -y
配置 postfix
1 vim /etc/postfix/main.cf
确定如下配置未被注释 或 添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 mail_owner = postfix myhostname = email.wyy.icu mydomain = wyy.icu myorigin = $mydomain inet_interfaces = all inet_protocols = all mydestination = $myhostname , localhost.$mydomain , localhost, $mydomain home_mailbox = Maildir/ smtpd_sasl_auth_enable = yes smtpd_sasl_security_options = noanonymous smtpd_sasl_local_domain = $mydomain broken_sasl_auth_clients = yes smtpd_tls_cert_file = /www/server/cert/email.wyy.icu/fullchain.pem smtpd_tls_key_file = /www/server/cert/email.wyy.icu/privkey.pem smtpd_tls_security_level = may smtpd_tls_session_cache_database = btree:${data_directory} /smtpd_scache
配置 SMTP SSL(465端口) 1 vim /etc/postfix/master.cf
确保以下配置 解开注释
1 2 3 4 5 6 7 8 9 10 11 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
启动 postfix 1 2 3 4 5 6 7 8 # 启动 sudo systemctl start postfix # 开机自启 sudo systemctl enable postfix # 重启(可选) sudo systemctl restart postfix
安装 Dovecot 1 sudo yum install dovecot -y
配置 Dovecot 配置文件一 dovecot.conf
1 vim /etc/dovecot/dovecot.conf
确认有如下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 解开注释 protocols = imap pop3 lmtp listen = *, :: # 添加配置 # 启用 SSL # 证书位置相对 postfix 开头多了个 < ssl = yes ssl_cert = </www/server/cert/email.wyy.icu/fullchain.pem ssl_key = </www/server/cert/email.wyy.icu/privkey.pem disable_plaintext_auth = no mail_location = maildir:~/Maildir
配置文件二 10-ssl.conf
1 vim /etc/dovecot/conf.d/10-ssl.conf
确认有如下配置
1 2 3 4 5 6 ssl = required # 注意文件位置开头多个 < ssl_cert = </www/server/cert/email.wyy.icu/fullchain.pem ssl_key = </www/server/cert/email.wyy.icu/privkey.pem
配置文件三 10-master.conf
1 vim /etc/dovecot/conf.d/10-master.conf
确认有如下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 # IMAP 登录配置 service imap-login { inet_listener imap { port = 143 } inet_listener imaps { port = 993 ssl = yes } } # POP3 登录配置 service pop3-login { inet_listener pop3 { port = 110 } inet_listener pop3s { port = 995 ssl = yes } } # 登录验证 service auth { unix_listener auth-userdb { mode = 0666 user = postfix group = postfix } }
配置文件四 10-mail.conf
前提条件 创建用户 & 目录
1 2 3 4 5 6 7 8 9 10 11 # 添加用户(自定义) mail sudo useradd mail # 设置密码 123456 sudo passwd mail # 测试用户 su mail # 创建用户邮件目录 wyy.icu 对应 服务器域名 mail 对应 刚刚创建的用户名 /var/mail/vhosts 自定义目录 mkdir /var/mail/vhosts/wyy.icu/mail
配置文件四
1 vim vim /etc/dovecot/conf.d/10-mail.conf
确认有如下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 # 解开注释 mail_location = maildir:~/Maildir # 修改配置 passdb { driver = passwd-file args = /etc/dovecot/passwd } # 自定义目录 userdb { driver = static args = uid=mail gid=mail home=/var/mail/vhosts/%d/%n }
配置文件五 10-auth.conf
1 vim /etc/dovecot/conf.d/10-auth.conf
确认有如下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # 解开注释 确保允许 PLAIN 或 LOGIN 认证方法 auth_mechanisms = plain # 修改如下行,允许明文登录 disable_plaintext_auth = no auth_mechanisms = plain login passdb { driver = passwd-file args = /etc/dovecot/passwd } # 自定义目录 /var/mail/vhosts 配置文件要一致 userdb { driver = static args = uid=mail gid=mail home=/var/mail/vhosts/%d/%n }
配置登录
添加 用户名: mail(与前文用户及配置 uid gid 用户名一致) & 密码(创建 mail 用户的密码)
1 2 3 mail@wyy.icu:{PLAIN}123456
启动 Dovecot 1 2 3 4 5 6 7 8 # 启动 sudo systemctl start dovecot # 开机自启 sudo systemctl enable dovecot # 重启(可选) sudo systemctl restart dovecot
检查配置
展示必要参数如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 # 2.2.36 (1f10bfa63): /etc/dovecot/dovecot.conf # OS: Linux 3.10.0-1160.108.1.el7.x86_64 x86_64 CentOS Linux release 7.9.2009 (Core) # Hostname: VM-4-17-centos auth_mechanisms = plain login disable_plaintext_auth = no first_valid_uid = 1000 mail_location = maildir:~/Maildir mbox_write_locks = fcntl passdb { driver = pam } passdb { args = /etc/dovecot/passwd driver = passwd-file } passdb { args = /etc/dovecot/passwd driver = passwd-file } service auth { unix_listener auth-userdb { group = postfix mode = 0666 user = postfix } } 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 } } ssl_cert = </www/server/cert/email.wyy.icu/fullchain.pem ssl_key = # hidden, use -P to show it userdb { driver = passwd } userdb { args = uid=lim gid=lim home=/var/mail/vhosts/%d/%n driver = static } userdb { args = uid=lim gid=lim home=/var/mail/vhosts/%d/%n driver = static }
确保 Dovecot 和 Postfix 可以读取证书和私钥文件。您可以使用以下命令设置权限:
1 2 3 4 5 sudo chown root:postfix /www/server/cert/email.wyy.icu/privkey.pem sudo chmod 640 /www/server/cert/email.wyy.icu/privkey.pem sudo chown root:root /www/server/cert/email.wyy.icu/fullchain.pem sudo chmod 644 /www/server/cert/email.wyy.icu/fullchain.pem
确保 在 465、995 端口(SSL)上监听
其他邮件端口 可根据前文 端口 说明 自行测试
1 2 3 4 5 6 7 8 9 10 sudo netstat -tuln | grep 465 sudo netstat -tuln | grep 995 # 正常结果如下 [root@VM-4-17-centos dovecot]# sudo netstat -tuln | grep 465 tcp 0 0 0.0.0.0:465 0.0.0.0:* LISTEN tcp6 0 0 :::465 :::* LISTEN [root@VM-4-17-centos dovecot]# sudo netstat -tuln | grep 995 tcp 0 0 0.0.0.0:995 0.0.0.0:* LISTEN tcp6 0 0 :::995 :::* LISTEN
客户端登录 以网易大师登录
mail 用户 wyy.icu主域名
mail.wyy.icu 邮件服务域名(SSL)
邮箱协议:POP
邮箱地址:mail@wyy.icu
收信服务器
主机名 :mail.wyy.icu
用户名 :mail
密码:123456
端口:995
加密:SSL/TLS
发信服务器
主机名:mail.wyy.icu
用户名: mail
密码: 123456
端口:465
加密:SSL/TLS
查看日志 1 2 3 4 # 查看 Postfix 邮件日志 sudo cat /var/log/maillog
1 2 3 4 # 最近的 实时日志 sudo tail -f /var/log/maillog
1 2 3 4 # 查看 Dovecot 日志 sudo cat /var/log/maillog | grep dovecot
1 2 3 4 # 检查失败的邮件 sudo cat /var/log/maillog | grep -i 'error\|deferred'