Postfix: Postfix 是一个邮件传输代理 (Mail Transfer Agent,MTA),主要用于邮件的发送和路由。它使用 SMTP(Simple Mail Transfer Protocol)协议来接收、发送和路由电子邮件,监听端口 25(SMTP)、465(SMTP secure)和587(SMTP Submission)。Postfix 提供了与外部邮件服务器通信的接口,以及与本地邮件交付代理(如 Dovecot)通信的接口。
Dovecot: Dovecot 是一个安全的邮件接收代理 (Mail Delivery Agent,MDA),用于存储和提供用户的电子邮件。它支持 IMAP(Internet Message Access Protocol)和 POP3(Post Office Protocol version 3)协议,监听端口 143(IMAP)和 110(POP3)。Dovecot 提供了接收邮件的接口,允许用户通过这些协议来访问和管理他们的收件箱。
Roundcube: Roundcube 是一个基于 Web 的邮件客户端,用于访问和管理电子邮件。它提供了一个直观和易于使用的界面,允许用户通过 Web 浏览器发送、接收和组织电子邮件。Roundcube 支持多种邮件协议,包括 IMAP 和 SMTP,可以与不同的邮件服务器集成使用。它具有强大的搜索、过滤和组织邮件的功能,支持多语言界面和插件扩展。
LEMP堆栈: LEMP堆栈(Linux、Nginx、MySQL/MariaDB、PHP)的组合使其成为一种强大的Web应用程序开发和托管环境。Nginx提供了快速、可靠的Web服务器和反向代理功能,MySQL或MariaDB提供了可靠的数据库存储,而PHP-FPM允许处理PHP脚本,从而构建出强大的Web应用程序。整个堆栈在Linux操作系统上运行,确保了稳定性和安全性。
本文示例域名使用a.com,已配置好域名的A和MX记录。示例服务器OS是Ubuntu 20.04 LTS x64,IP使用1.2.3.4。如果还没拥有域名和服务器,可以参考我的其另一篇文章:
Apache+Flask在云服务器搭建个人网站
可以直接使用Vultr提供noVNC客户端登录服务器:
更新软件包列表:
sudo apt update
开启防火墙:
sudo ufw enable
sudo ufw allow 25/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
1.安装并启动Nginx
sudo apt install nginx
sudo systemctl start nginx
2.安装和配置MySQL
sudo apt install mysql-server
sudo systemctl start mysql
sudo mysql_secure_installation
向导完成之后,登录MySQL的root用户并新增用户:
# 使用root身份登录数据库
mysql -u root -p
# 创建数据库新用户
CREATE USER 'user'@'localhost' IDENTIFIED BY 'User.555';
# 授予用户权限
GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost';
# 刷新权限并退出数据库
FLUSH PRIVILEGES;
EXIT
3.安装PHP和一些常用的 PHP 扩展
sudo apt install php-fpm php-mysql php-common php-gd php-json php-cli php-curl php-mbstring php-xml php-zip php-intl
1.下载 Roundcube Webmail 1.6.2并解压
wget https://github.com/roundcube/roundcubemail/releases/download/1.6.2/roundcubemail-1.6.2-complete.tar.gz
tar -zxvf roundcubemail-1.6.2-complete.tar.gz
2.将 Roundcube 移动到 Web 服务器根目录
sudo mv roundcubemail-1.6.2 /var/www/roundcube
3.授予www-data的权限转给 Roundcube 目录
sudo chown -R www-data:www-data /var/www/roundcube
4.登录user用户初始化Roundcube数据库
mysql -u user -p
CREATE DATABASE roundcube;
USE roundcube;
source /var/www/roundcube/SQL/mysql.initial.sql
EXIT
5.配置Roundcube
修改config.inc.php
cp /var/www/roundcube/config/config.inc.php.sample /var/www/roundcube/config/config.inc.php
vim /var/www/roundcube/config/config.inc.php
修改配置文件内容,将数据库更改为之前设置的信息
$config['db_dsnw'] = 'mysql://user:User.555@localhost/roundcube';
$config['smtp_host'] = 'tls://localhost:587';
修改defaults.inc.php
cp /var/www/roundcube/config/defaults.inc.php.sample /var/www/roundcube/config/defaults.inc.php
vim /var/www/roundcube/config/defaults.inc.php
修改以下内容
$config['smtp_conn_options'] = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
];
6.移除Roundcube安装向导
sudo rm -r /var/www/roundcube/installer/
7.通过 Let’s Encrypt 安装TLS证书
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d a.com -d www.a.com
通常在以下位置生成证书
/etc/letsencrypt/live/a.com/fullchain.pem
/etc/letsencrypt/live/a.com/privkey.pem
8.配置Nginx
vim /etc/nginx/sites-available/roundcube
server {
listen 80;
server_name a.com;
return 301 https://www.$host$request_uri;
}
server {
listen 80;
server_name www.a.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name a.com;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_certificate /etc/letsencrypt/live/a.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/a.com/privkey.pem;
ssl_prefer_server_ciphers off;
return 301 https://www.$host$request_uri;
}
server {
listen 443 ssl;
server_name www.a.com;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_certificate /etc/letsencrypt/live/a.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/a.com/privkey.pem;
ssl_prefer_server_ciphers off;
root /var/www/roundcube;
index index.php;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
9.创建符号链接激活自定义的配置
rm -f /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/roundcube /etc/nginx/sites-enabled/
sudo systemctl reload nginx
10.此时可以通过https://a.com或者https://www.a.com访问到roundcube客户端的登录界面
1.安装Dovecot
sudo apt install dovecot-core dovecot-imapd dovecot-lmtpd dovecot-mysql
2.新增 Dovecot 配置文件
mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.bak
vim /etc/dovecot/dovecot.conf
添加以下内容
protocols = imap lmtp
listen = 127.0.0.1
mail_location = maildir:/var/mail/%d/%n
mail_privileged_group = mail
ssl = no
default_login_user = nobody
default_internal_user = vmail
first_valid_uid = 5000
last_valid_uid = 5000
first_valid_gid = 5000
last_valid_gid = 5000
auth_username_format = %Lu
auth_mechanisms = plain
auth_verbose = yes
disable_plaintext_auth = no
userdb {
driver = static
args = uid=vmail gid=vmail home=/var/mail/%d/%n
}
passdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf.ext
}
!include conf.d/*.conf
3.修改 Dovecot 数据库配置文件
cp /etc/dovecot/dovecot-sql.conf.ext /etc/dovecot/dovecot-sql.conf.ext.bak
vim /etc/dovecot/dovecot-sql.conf.ext
以下内容需要一致
driver = mysql
connect = host=127.0.0.1 dbname=webmail user=user password=User.555
default_pass_scheme = SHA512-CRYPT
password_query = SELECT email AS user, password FROM user_tab WHERE email = '%Lu'
userdb_warning_disable = yes
4.取消系统用户登录,使用数据库中用户进行登录
cp /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.bak
vim /etc/dovecot/conf.d/10-auth.conf
注释下面这一行
#!include auth-system.conf.ext
5.开启 Dovecot 的 SASL 配置
cp /etc/dovecot/conf.d/10-master.conf /etc/dovecot/conf.d/10-master.conf.bak
vim /etc/dovecot/conf.d/10-master.conf
修改以下内容,允许Postfix进行SASL认证
service lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
mode = 0660
user = postfix
group = postfix
}
}
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
}
unix_listener auth-userdb {
mode = 0660
user = vmail
group = vmail
}
}
6.指定 Dovecot 的 auth socket 路径
cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.bak
vim /etc/dovecot/conf.d/10-mail.conf
取消下面这行的注释
auth_socket_path = /var/run/dovecot/auth-userdb
7.创建系统用户和组
sudo groupadd -g 5000 vmail
sudo useradd -u 5000 -g vmail -s /sbin/nologin -d /var/mail vmail
sudo chown -R vmail:vmail /var/mail
8.创建数据库表和新建一条邮箱数据
mysql -u user -p
CREATE DATABASE webmail;
USE webmail;
CREATE TABLE domain_tab (
id INT AUTO_INCREMENT PRIMARY KEY,
domain VARCHAR(255) NOT NULL UNIQUE
);
INSERT INTO domain_tab (domain) VALUES ('a.com');
CREATE TABLE user_tab (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
INSERT INTO user_tab (email, password)
VALUES ('[email protected]', '$6$rounds=45529$ADZYJ4a7OefGBfVa$Za0hqfIWWkQkg3GPeL3qqX68Q/tTQQdC2f1Uzz9wfUxV5SvG1tAz1gG6/Lyyqyfj2RQtIbimD3lCGzcx/u.Hz.');
EXIT
其中密码是经过 SHA512-CRYPT 算法生成的哈希值,可以使用Python脚本或其他方式来生成哈希值
import random
from passlib.handlers.sha2_crypt import sha512_crypt
def get_crypt_password(password=None):
rounds = random.randint(10000, 50000)
crypt_password = sha512_crypt.using(rounds=rounds).hash(password)
return crypt_password
if __name__ == '__main__':
print(get_crypt_password(password="Test.555"))
9.启动Dovecot服务
sudo systemctl start dovecot
10.此时就可以通过邮箱用户名[email protected]和密码Test.555登录到roundcube
1.安装Postfix
sudo apt install postfix postfix-mysql
2.在Postfix配置中选择 “Internet Site” 并按照提示输入系统邮件名称a.com
3.编辑 Postfix 主配置文件
cp /etc/postfix/main.cf /etc/postfix/main.cf.bak
vim /etc/postfix/main.cf
添加或者修改以下字段:
myhostname = mail.a.com
mydomain = a.com
myorigin = $mydomain
mydestination = localhost
mynetworks = 127.0.0.0/8, [::1]/128
inet_protocols = ipv4
smtpd_use_tls = yes
smtpd_tls_auth_only = yes
smtpd_tls_cert_file = /etc/letsencrypt/live/a.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/a.com/privkey.pem
smtpd_tls_security_level = may
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination
virtual_transport = lmtp:unix:private/dovecot-lmtp
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf
4.创建 /etc/postfix/mysql-virtual-mailbox-domains.cf 配置文件
vim /etc/postfix/mysql-virtual-mailbox-domains.cf
添加以下字段
user = user
password = User.555
hosts = 127.0.0.1
dbname = webmail
query = SELECT 1 FROM domain_tab WHERE domain='%s'
5.创建 /etc/postfix/mysql-virtual-mailbox-maps.cf 配置文件
vim /etc/postfix/mysql-virtual-mailbox-maps.cf
添加或者修改以下字段
user = user
password = User.555
hosts = 127.0.0.1
dbname = webmail
query = SELECT 1 FROM user_tab WHERE email='%s'
6.创建 /etc/postfix/mysql-virtual-alias-maps.cf 配置文件
vim /etc/postfix/mysql-virtual-alias-maps.cf
添加或者修改以下字段
user = user
password = User.555
hosts = 127.0.0.1
dbname = webmail
query = SELECT email FROM user_tab WHERE email='%s'
7.编辑 /etc/postfix/master.cf 配置文件
cp /etc/postfix/master.cf /etc/postfix/master.cf.bak
vim /etc/postfix/master.cf
取消以下一行注释,开启SMTP Submission端口587
submission inet n - y - - smtpd
8.重启Postfix服务
sudo systemctl restart postfix
9.此时可以通过roundcube正常收发邮件
可能出现发送成功,但是目标邮箱没有收到邮件的情况,此时在服务器/var/log/mail.log中大概率会有Connection timed out的日志,然后使用telnet命令测试目标邮件服务器是否连通,根据成功连通和无法连通分为两种处理方案:
1.成功连通目标邮件服务器的25端口:通过DNS提供商设置SPF(Sender Policy Framework)、DKIM(DomainKeys Identified Mail)和DMARC (Domain-based Message Authentication, Reporting, and Conformance)策略来提高电子邮件的可信度和安全性,减少垃圾邮件和欺诈邮件的传递,从而提高接收方对合法邮件的信任。
2.无法连通目标邮件服务器的25端口:ISP(Internet Service Provider)阻止了端口25的流量,需要找到ISP提供的邮件中继服务器转发邮件,只需要将邮件中继服务器的域名填写到/etc/postfix/main.cf文件中的relayhost字段,这样Postfix将使用指定的邮件中继服务器来转发邮件,绕过了ISP对端口25的阻止。