在 CentOS 上搭建邮件服务器,常见方案是使用 Postfix(MTA)+ Dovecot(IMAP/POP3)+ MariaDB/MySQL(可选用户管理)。下面以 CentOS 7/8 为例,给你一个可运行的基础邮件服务器安装教程(支持发信、收信,不含反垃圾高级配置)。
# 关闭 SELinux(生产建议配置策略而非关闭)
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 防火墙放行端口
firewall-cmd --permanent --add-port=25/tcp
firewall-cmd --permanent --add-port=110/tcp
firewall-cmd --permanent --add-port=143/tcp
firewall-cmd --permanent --add-port=465/tcp
firewall-cmd --permanent --add-port=993/tcp
firewall-cmd --reload
⚠️ 云服务器通常封禁 25 端口,只能发信到同平台或需申请解封。
yum install -y postfix dovecot
设置 Postfix 开机启动:
systemctl enable postfix
systemctl start postfix
编辑配置:
vi /etc/postfix/main.cf
关键配置(示例域名 mail.example.com):
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 127.0.0.0/8
home_mailbox = Maildir/
重启:
systemctl restart postfix
vi /etc/dovecot/dovecot.conf
启用协议:
protocols = imap pop3
编辑邮箱路径:
vi /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir
启动 Dovecot:
systemctl enable dovecot
systemctl start dovecot
useradd -m user1
echo "password" | passwd --stdin user1
测试发送:
echo "test" | mail -s "hello" user1@example.com
查看邮件:
ls ~/user1/Maildir/new
域名解析需添加:
example.com. MX 10 mail.example.com.
mail.example.com. A 你的服务器IP
可选(推荐):
| 需求 | 推荐 |
|---|---|
| 轻量 | Postfix + Dovecot |
| 虚拟用户 | Postfix + MariaDB |
| 图形管理 | iRedMail / Mailcow |
| 企业级 | Exchange(非 CentOS) |
不想手动配置可用:
# iRedMail
bash iRedMail.sh
如果你告诉我:
我可以给你更精确的方案。