如何在CentOS上部署邮件服务器

287
2025/3/11 18:32:06
栏目: 云计算
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在CentOS上部署邮件服务器是一个复杂但非常有意义的任务,以下是详细的步骤和注意事项:

基础环境准备

  • 操作系统:CentOS Linux 7.9 x86_64。
  • 公网IP:运营商分配的公网IP或弹性公网IP。
  • 域名:需要有一个有效的域名。

安装邮件服务

  1. 更新系统
sudo yum update -y
  1. 安装必要的软件包
sudo yum install postfix dovecot mariadb-server mariadb -y
  1. 配置MySQL
  • 初始化MySQL数据库:
sudo mysql_secure_installation
  • 创建邮件数据库和用户:
sudo mysql -u root -p
CREATE DATABASE mail;
USE mail;
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL);
GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mailuser'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
EXIT;
  1. 配置Postfix
  • 编辑 /etc/postfix/main.cf 文件,设置以下参数:
myhostname mail.example.com
mydomain example.com
myorigin mydomain
inet_interfaces all
inet_protocols ipv4
mydestination myhostname, localhost.mydomain, localhost, mydomain
home_mailbox Maildir/
smtpd_banner myhostname ESMTP
smtpd_recipient_restrictions permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
smtpd_sasl_auth_enable yes
smtpd_sasl_security_options noanonymous
smtpd_sasl_type dovecot
smtpd_sasl_path private/auth
smtpd_sasl_local_domain myhostname
  • 启动并设置Postfix开机自启动:
sudo systemctl start postfix
sudo systemctl enable postfix
  1. 配置Dovecot
  • 编辑 /etc/dovecot/dovecot.conf 文件,设置以下参数:
protocols imap pop3
mail_location maildir:/Maildir
  • 编辑 /etc/dovecot/conf.d/10-mail.conf 文件,设置以下参数:
mail_location maildir:/Maildir
  • 启动并设置Dovecot开机自启动:
sudo systemctl start dovecot
sudo systemctl enable dovecot
  1. 配置防火墙
  • 允许邮件服务通过:
sudo firewall-cmd --permanent --add-services=smtp
sudo firewall-cmd --permanent --add-service=pop3
sudo firewall-cmd --permanent --add-service=imap
sudo firewall-cmd --reload
  1. 配置RoundCube(可选):
  • 下载并安装RoundCube:
wget https://github.com/roundcube/roundcube/releases/download/v1.3.12/roundcube-1.3.12.tar.gz
tar xvf roundcube-1.3.12.tar.gz
cd roundcube-1.3.12
./configure
make
sudo make install
  • 配置RoundCube:
sudo cp config/config.inc.php.sample config/config.inc.php

测试邮件服务器

  1. 查看端口监听状态
netstat -ntlp | grep :25
netstat -ntlp | grep :110
  1. 使用邮件客户端测试
  • 使用邮件客户端(如Outlook、Thunderbird)配置SMTP服务器为 mail.example.com,端口为 587,并使用配置的邮箱和密码进行测试。

常见问题及解答

  • 无法发送邮件:检查Postfix日志文件 /var/log/maillog,查找具体的错误信息,常见的问题包括DNS解析失败、防火墙拦截等。
  • 邮件附件大小限制:修改 /etc/postfix/main.cf 文件,添加或修改以下行:
message_size_limit 10485760 # 例如10MB

然后重启Postfix服务以使更改生效。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: centos sftp传输速度慢如何优化