ubuntu lamp邮件服务设置
小樊
36
2025-11-24 13:36:36
Ubuntu LAMP 邮件服务设置
一 架构与准备
- 组件选型:使用 Postfix 作为 MTA(负责发送/转发邮件),Dovecot 作为 IMAP/POP3(负责接收与本地投递),LAMP 中的 Apache/PHP 用于运行网站与发送通知邮件。
- 前提条件:一个已备案/可解析的域名(如:example.com)、服务器可达的公网 IP、开放相关端口(见下文)、以及用于发信的PTR 反向解析与 TLS 证书(推荐 Let’s Encrypt)。
- 基础安装(示例域名:mail.example.com):
- 更新与安装:sudo apt update && sudo apt install postfix dovecot-core dovecot-imapd dovecot-pop3d
- Postfix 安装时选择“Internet Site”,系统主机名填入:mail.example.com。
二 Postfix 核心配置
- 编辑 /etc/postfix/main.cf(按需调整以下关键项):
- myhostname = mail.example.com
- mydomain = example.com
- myorigin = $mydomain
- inet_interfaces = all
- inet_protocols = ipv4(或 all)
- mydestination = $myhostname, localhost.$mydomain, $mydomain
- mynetworks = 127.0.0.0/8 [::1]/128
- home_mailbox = Maildir/(推荐 Maildir 结构)
- smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
- smtpd_sasl_auth_enable = yes
- smtpd_sasl_security_options = noanonymous
- smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated reject_unauth_destination
- 启用 TLS(证书路径以实际为准):
- smtpd_tls_security_level = may
- smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
- smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem
- smtpd_use_tls = yes
- 应用与验证:
- 重启服务:sudo systemctl restart postfix
- 本机连通性测试:telnet localhost 25,应看到 “220 … ESMTP Postfix”。
三 Dovecot 配置
- 启用协议与邮箱位置(/etc/dovecot/dovecot.conf 或 conf.d 中相应文件):
- protocols = imap pop3
- mail_location = maildir:~/Maildir
- mail_privileged_group = mail
- 认证与 SASL(与 Postfix 对接):
- /etc/dovecot/conf.d/10-auth.conf:
- disable_plaintext_auth = yes
- auth_mechanisms = plain login
- /etc/dovecot/conf.d/10-master.conf(为 Postfix SASL 提供 socket):
- service auth { unix_listener /var/spool/postfix/private/auth { mode = 0660 user = postfix group = postfix } }
- SSL/TLS(/etc/dovecot/conf.d/10-ssl.conf):
- ssl = yes
- ssl_cert = </etc/letsencrypt/live/mail.example.com/fullchain.pem
- ssl_key = </etc/letsencrypt/live/mail.example.com/privkey.pem
- 应用与验证:
- 重启服务:sudo systemctl restart dovecot
- 本机连通性测试:telnet localhost 143,应看到 “* OK Dovecot ready”。
四 DNS 与安全加固
- 必要 DNS 记录(在域名 DNS 控制台添加):
- MX:example.com. IN MX 10 mail.example.com.
- A:mail.example.com. IN A 你的服务器IP
- SPF(TXT):v=spf1 mx a ~all
- DKIM:使用 opendkim 生成密钥对(opendkim-genkey -t -s mail -d example.com),将生成的 mail.txt 内容添加为 default._domainkey.example.com 的 TXT 记录;配置 OpenDKIM 并启动服务。
- DMARC(TXT):_dmarc.example.com. IN TXT “v=DMARC1; p=none; rua=mailto:postmaster@example.com”
- 端口与防火墙(UFW 示例):
- 入站放行:sudo ufw allow 25/tcp(MTA)、587/tcp(Submission)、465/tcp(SMTPS)、143/tcp(IMAP)、993/tcp(IMAPS)、110/tcp(POP3)、995/tcp(POP3S)
- 反垃圾与合规:
- 建议启用 SpamAssassin、反向 DNS(PTR)、HELO/EHLO 主机名一致性、速率限制与黑名单检查。
五 测试与 PHP 发信
- 命令行测试:
- 发送测试:echo “This is a test email.” | mail -s “Test Email” recipient@example.com
- 查看日志:sudo tail -f /var/log/mail.log
- PHP 发信(示例,使用本地 Postfix 投递):
- 方法 A(sendmail_path,php.ini):sendmail_path = /usr/sbin/sendmail -t -i
- 方法 B(PHP 代码):
- mail(‘recipient@example.com’, ‘Subject’, ‘Body’, ‘From: sender@example.com’);
- 客户端与 Webmail:
- 桌面客户端(如 Thunderbird)配置:SMTP 587/STARTTLS,IMAP 993/SSL;用户名通常为完整邮箱地址。
- Webmail(可选):部署 Roundcube 或 SquirrelMail,通过 Apache/PHP 访问,使用 Dovecot 账号进行收发。