2024.01.13啊,2024年了啊,稍微记录下吧。查了一圈似乎没有相关的教程,那咱就自己摸索着写一篇吧
2024.01.15有事鸽了一天,今天继续;乐了,不支持SSH,退款了
2024.01.16来了
2024.01.17继续搞;教程有点老了,各种版本更新、服务停止支持,跟扒皮一样一层层去找下一步的解决方案,过程倒是挺有趣,但人也有点麻;Let’s Encrypt的证书申请完了发现网站进不去了,查解决方案的时候发现之前网站配置CDN的时候用的Cloudflare提供自签证书。。。
2024.01.18接着搞;配置完了客户端连接测试 SSL连接错误,还得再测测,从头查查吧
2024.01.25终于有眉目了,服务器厂家没开放465端口,联系下然后加个 TXT 记录防止邮件滥用。问题解决了就发布叭;并没有,被认为是垃圾邮件发出去了收不到;搞定了。
Hostname | Type | TTL |
---|---|---|
A | 3600 | |
www | A | 3600 |
Hostname | Type | Target Hostname | TTL | Distance |
---|---|---|---|---|
@ | MX | mx2.titan.email | 3600 | 20 |
Hostname | Type | Text | TTL |
---|---|---|---|
@ | TXT/SPF | v=spf1 include:spf.titan.email ~all | 3600 |
搞了一下午,最后问客服发现在 NameSilo 上买的服务器不支持 SSH,只能在 cPanel 里点点点,不能用 terminal 敲命令行还玩什么呢。
单纯写博客用来记录的话倒是可以试下,直接用网站模板拖拽修改排版倒是挺不错的(如果涉及到比较复杂的后台逻辑,想要直接部署一个项目运行的话就不推荐了):
Ubuntu 22.04 LTS
sudo apt install mysql-server
apt
已经默认开启服务,因此不需要开启服务以及设置开机自启动service mysql status
sudo service mysql start/stop/restart
sudo cat /etc/mysql/debian.cnf
sudo mysql
Hostname | Type | TTL |
---|---|---|
A | 3600 | |
www | A | 3600 |
Hostname | Type | TTL |
---|---|---|
mx | A | 3600 |
smtp | A | 3600 |
pop3 | A | 3600 |
Hostname | Type | Target Hostname | TTL | Distance |
---|---|---|---|---|
@ | MX | mx.XXX.xxx | 3600 | 20 |
Hostname | Type | Text | TTL |
---|---|---|---|
@ | TXT | v=spf1 mx ~all | 3600 |
Hostname | Type | Text | TTL |
---|---|---|---|
_dmarc | TXT | v=DMARC1; p=reject; fo=1; rua=mailto:a***@XXX.xxx | 3600 |
apt update
apt install postfix postfix-mysql dovecot-core dovecot-pop3d dovecot-imapd dovecot-lmtpd dovecot-mysql
virtual_domains
、virtual_users
、virtual_aliases
:CREATE TABLE `virtual_domains` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `virtual_users` (
`id` INT NOT NULL AUTO_INCREMENT,
`domain_id` INT NOT NULL,
`password` VARCHAR(106) NOT NULL,
`email` VARCHAR(120) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `virtual_aliases` (
`id` int(11) NOT NULL auto_increment,
`domain_id` int(11) NOT NULL,
`source` varchar(100) NOT NULL,
`destination` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into virtual_domains values(1,'XXX.xxx')
insert into virtual_users values(1,域名序号,md5('密码'),'[email protected]');
insert into virtual_aliases values(1,1,'[email protected]','[email protected]')
[email protected]
需要先存在于 virtual_users
中,virtual_aliases
中的转发记录才能生效)sudo apt-get install zlib1g-dev
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
sudo apt install openjdk-17-jre-headless
sudo wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.18/bin/apache-tomcat-10.1.18.tar.gz
sudo tar -xzvf apache-tomcat-10.1.18.tar.gz -C /usr/local/
-C
:指定解压⽬录,解决当前⽬录下找不到 /usr/local
报错cd /usr/local/apache-tomcat-10.1.18/bin
./startup.sh
域名:8080
访问进入下图界面为安装成功:安装 snap:
sudo apt update
sudo apt install snapd
snap version
通过 snap 安装 certbot: sudo snap install --classic certbot
创建的软链接,便于 certbot 命令的使用:ln -s /snap/bin/certbot /usr/bin/certbot
获取证书(Nginx 方式): certbot certonly --nginx --email 1***@qq.com -d ***.top -d www.***.top
更新证书: certbot renew
续订并重启 Nginx:certbot renew --renew-hook "service nginx restart"
自动续订证书: 30 4 * * 1 certbot renew --renew-hook "service nginx restart" --quiet > /dev/null 2>&1 &
sudo timedatectl set-timezone Asia/Shanghai
安装完证书访问网站报错,重定向次数过多,解决方法很简单,只需要将 Cloudflare 中的加密模式由灵活(默认模式)修改为完全即可。详细原因可以参考这篇文章的 三:Cloudflare
。
官网文档:Postfix文档
备份 postfix 的默认配置文件: cp /etc/postfix/main.cf /etc/postfix/main.cf.bak
编辑 main.cf
:
vim /etc/postfix/main.cf
smtpd_tls_auth_only = yes
注释掉表示允许 tls 的 587 端口以及不需要 ssl 验证的 25 端口,用来解决使用 Foxmail 的时候每次登陆都会弹出一个 “安全证书”的问题。# 使用自己的ssl证书
smtpd_tls_cert_file=/etc/letsencrypt/live/000.top/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/000.top/privkey.pem
smtpd_use_tls=yes
#smtpd_tls_auth_only = yes
smtp_tls_cert_file=/etc/letsencrypt/live/000.top/fullchain.pem
smtp_tls_key_file=/etc/letsencrypt/live/000.top/privkey.pem
smtp_use_tls=yes
smtpd_tls_CApath=/etc/letsencrypt/live/000.top
smtpd_tls_security_level=may
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_CApath=/etc/letsencrypt/live/000.top
smtp_tls_security_level=may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
# 使用dovecot来做身份认证
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
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = 000.top
myorigin = $myhostname
mydomain = $myhostname
# 确保将邮件投递给 mysql 表中列出的虚拟域
virtual_transport = lmtp:unix:private/dovecot-lmtp
# Postfix 配置虚拟域、用户和别名
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
mysql-virtual-mailbox-domains.cf
vim /etc/postfix/mysql-virtual-mailbox-domains.cf
user = root
password = 123456
hosts = 127.0.0.1:3306
dbname = mailserver
query = SELECT 1 FROM virtual_domains WHERE name='%s'
mysql-virtual-mailbox-maps.cf
vim /etc/postfix/mysql-virtual-mailbox-maps.cf
user = root
password = 123456
hosts = 127.0.0.1:3306
dbname = mailserver
query = SELECT 1 FROM virtual_users WHERE email='%s'
mysql-virtual-alias-maps.cf
vim /etc/postfix/mysql-virtual-alias-maps.cf
user = root
password = 123456
hosts = 127.0.0.1:3306
dbname = mailserver
query = SELECT destination FROM virtual_aliases WHERE source='%s'
systemctl restart postfix
postmap -q 000.top mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
成功则输出 1
postmap -q [email protected] mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
成功则输出 1
postmap -q [email protected] mysql:/etc/postfix/mysql-virtual-alias-maps.cf
成功则输出 [email protected]
master.cf
:
配置 dovecot.conf
:
vim /etc/dovecot/dovecot.conf
!include conf.d/*.conf
protocols = imap lmtp pop3
配置 10-mail.conf
:
vim /etc/dovecot/conf.d/10-mail.conf
/var/mail
中):
mail_location = maildir:/var/mail/vhosts/%d/%n
mail_privileged_group = mail
创建用户来作为 /var/mail
路径的所属人:
groupadd -g 5000 vmail
useradd -g vmail -u 5000 vmail -d /var/mail
chown -R vmail:vmail /var/mail
配置 10-auth.conf
:
配置 auth-sql.conf.ext
:
vim /etc/dovecot/conf.d/auth-sql.conf.ext
passdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf.ext
}
userdb {
driver = static
args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n
}
配置 dovecot-sql.conf.ext
:
vim /etc/dovecot/dovecot-sql.conf.ext
driver = mysql
connect = host=127.0.0.1 port=3306 dbname=mailserver user=root password=123456
default_pass_scheme = MD5
password_query = SELECT email as user, password FROM virtual_users WHERE email='%u';
将 /etc/dovecot
的拥有者改为 vmail:dovecot
:
chown -R vmail:dovecot /etc/dovecot
chmod -R o-rwx /etc/dovecot
配置 10-master.conf
:
vim /etc/dovecot/conf.d/10-master.conf
imap-login
、 pop3-login
下第一个的 port
设置为 0
,以禁用非 ssl 加密的 imap 和 pop3 协议lmtp
、 auth
、 auth-worker
中的内容修改为以下对应内容:service lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
mode = 0600
user = postfix
group = postfix
}
# Create inet listener only if you can't use the above UNIX socket
#inet_listener lmtp {
# Avoid making LMTP visible for the entire internet
#address =
#port =
#}
}
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
unix_listener auth-userdb {
mode = 0600
user = vmail
#group =
}
user = dovecot
}
service auth-worker {
# Auth worker process is run as root by default, so that it can access
# /etc/shadow. If this isn't necessary, the user should be changed to
# $default_internal_user.
user = vmail
}
配置 10-ssl.conf
以开启 ssl 认证:
vim /etc/dovecot/conf.d/10-ssl.conf
ssl = required
required
:必须要有 sslyes
:开启 ssl 也允许非 sslssl_cert =
ssl_key =
查看当前 Dovecot 配置: dovecot -n
配置完成,重启后可以使用 Foxmail 连接了:
service postfix restart
service dovecot restart
登录失败,到 /var/log
查看日志
终于找到问题了,记得看看自己的主机厂商有没有开启 465 等端口。
sudo apt-get install spamassassin spamc
sudo adduser spamd --disabled-login
cat /etc/passwd
cat /etc/passwd | cut -d: -f1
spamassassin
:
vim /etc/default/spamassassin
ENABLED=1
SAHOME="/var/log/spamassassin/"
OPTIONS="--create-prefs --max-children 5 --username spamd --helper-home-dir /home/spamd/ -s /home/spamd/spamd.log"
PIDFILE="/var/run/spamd.pid"
CRON=1
local.cf
(用于添加规则):
vim /etc/spamassassin/local.cf
rewrite_header Subject ***** SPAM _SCORE_ *****
required_score 5.0
use_bayes 1
bayes_auto_learn 1
master.cf
:
sudo service postfix restart
sudo service spamassassin restart
sudo apt install opendkim opendkim-tools
opendkim.conf
:
vim /etc/opendkim.conf
Domain 000.top
KeyFile /etc/dkimkeys/dkim.key
Selector dkim
SOCKET inet:8891@localhost
opendkim
:
vim /etc/default/opendkim
SOCKET="inet:8891@localhost"
dkim.private
和 dkim.txt
在根目录下):
sudo opendkim-genkey -t -s dkim -d 000.top
dkim.private
:
sudo mv dkim.private /etc/dkimkeys/
cd /etc/dkimkeys/
sudo chown opendkim:opendkim dkim.private
mv dkim.private dkim.key
main.cf
(Postfix):
vim /etc/postfix/main.cf
#DKIM
smtpd_milters = inet:localhost:8891
non_smtpd_milters = inet:localhost:8891
milter_protocol = 2
milter_default_action = accept
sudo service opendkim restart
sudo service postfix restart
dkim.txt
中括号中的内容去除双引号和换行,作为 TXT 记录添加到域名中Hostname | Type | Text |
---|---|---|
dkim._domainkey | TXT | v=DKIM1; h=sha256; k=rsa; t=y;p=… |
官网以及官方文档:
本文记录了如何在一台 Ubuntu 服务器上搭建邮件服务器,主要是安装和配置 Postfix 和 Dovecot,以及 SpamAssassin 和 OpenDKIM 的安装和配置。同时也介绍了如何申请 SSL 证书以及其他服务的相关配置。