0%

ubuntu18.04自建邮件服务器

前言

在freenom新申请了一个顶级域名(一年的免费使用时间),恰好申请到.ml后缀的,就理解为mail的缩写好咯……我打算用这个域名建一个邮件服务系统。

方法

安装mysql并配置

安装命令:

1
apt install mysql-server

登录至mysql:(root默认无密码)

1
mysql -u root -p

创建数据库:

1
2
3
4
create database mailserver character set utf8;
create user mailserver@'localhost' identified by 'dana5haw';
grant all on mailserver.* to mailserver@'localhost' identified by 'dana5haw';
exit;

登录mailserver用户,创建数据表:

1
mysql -u mailserver -p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use mailserver;
CREATE TABLE `virtual_domains` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`))
ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `virtual_users` (
`id` int(11) NOT NULL auto_increment,
`domain_id` int(11) NOT NULL,
`password` varchar(106) NOT NULL,
`email` varchar(100) 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

插入数据到数据表:

1
2
3
4
5
6
7
8
insert into virtual_domains(id,name) values(1,'mail.xiaokunjia.ml');     
insert into virtual_domains(id,name) values(2,'xiaokunjia.ml');
insert into virtual_users(id,domain_id,password,email) values (1,2,'dana5haw','dana@xiaokunjia.ml');
insert into virtual_users(id,domain_id,password,email) values (2,2,'dana5haw','shaw@xiaokunjia.ml');
insert into virtual_aliases(id,domain_id,source,destination)
values (1,2,'all@xiaokunjia.ml','dana@xiaokunjia.ml');
insert into virtual_aliases(id,domain_id,source,destination)
values (2,2,'all@xiaokunjia.ml','shaw@xiaokunjia.ml');

测试插入数据是否成功:

1
2
3
select * from virtual_domains;  
select * from virtual_users;
select * from virtual_aliases;

申请https证书:

1
certbot certonly --manual --preferred-challenge dns -d mail.xiaokunjia.ml

安装postfix并配置

安装命令:

1
apt install postfix postfix-mysql

备份原配置文件/etc/postfix/main.cf之后,

1
root@vultr:~# cp /etc/postfix/main.cf /etc/postfix/main.cf.bak

修改配置文件:
image96c870a39ace11e1.png
imageb851413a64f7fdf7.png
imagea08d2dae9001c479.png
imagef8abfe481ffc8277.png
新增配置文件:

1
root@vultr:~# vi /etc/postfix/mysql-virtual-mailbox-domains.cf

配置文件内容如下:

1
2
3
4
5
user = mailserver
password = dana5haw
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_domains WHERE name='%s'

重启postfix服务:

1
root@vultr:~# service postfix restart

测试配置文件是否正确:(输出结果为1,表示配置文件正确)

1
2
root@vultr:~# postmap -q xiaokunjia.ml mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
1

新增配置文件:

1
root@vultr:~# vi /etc/postfix/mysql-virtual-mailbox-maps.cf

配置文件内容:

1
2
3
4
5
user = mailserver
password = dana5haw
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_users WHERE email='%s'

重启postfix服务,测试配置文件是否正确:

1
2
3
root@vultr:~# service postfix restart
root@vultr:~# postmap -q dana@xiaokunjia.ml mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
1

新增配置文件:

1
root@vultr:~# vi /etc/postfix/mysql-virtual-alias-maps.cf

配置文件内容:

1
2
3
4
5
user = mailserver
password = dana5haw
hosts = 127.0.0.1
dbname = mailserver
query = SELECT destination FROM virtual_aliases WHERE source='%s'

重启postfix服务,测试配置文件是否正确:

1
2
3
root@vultr:~# service postfix restart
root@vultr:~# postmap -q all@xiaokunjia.ml mysql:/etc/postfix/mysql-virtual-alias-maps.cf
dana@xiaokunjia.ml,shaw@xiaokunjia.ml

备份配置文件/etc/postfix/master.cf之后,

1
root@vultr:~# cp /etc/postfix/master.cf /etc/postfix/master.cf.bak

修改配置文件内容:
image150e0bbeed21267d.png
重启postfix服务:

1
root@vultr:~# service postfix restart 

安装Dovecot并配置

安装命令:

1
apt install dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-mysql

修改配置文件/etc/dovecot/dovecot.conf

1
root@vultr:~# cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.bak

imagea69d69106b434122.png
修改配置文件/etc/dovecot/conf.d/10-mail.conf

1
root@vultr:~# cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.bak

image663c00de1025865d.png
检查/var/mail的权限:

1
2
root@vultr:~# ls -ld /var/mail
drwxrwsr-x 2 root mail 4096 Feb 3 18:22 /var/mail

创建文件夹:

1
root@vultr:~# mkdir -p /var/mail/vhosts/xiaokunjia.ml

新建vmail用户组并修改/var/mail的权限:

1
2
3
root@vultr:~# groupadd -g 5000 vmail
root@vultr:~# useradd -g vmail -u 5000 vmail -d /var/mail
root@vultr:~# chown -R vmail:vmail /var/mail

修改配置文件/etc/dovecot/conf.d/10-auth.conf

1
root@vultr:~# cp /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.bak

image76858b32b99deb51.png
image8664f29d795d982b.png
image5bd7d21edc97a9c3.png
修改配置文件/etc/dovecot/conf.d/auth-sql.conf.ext

1
root@vultr:~# cp /etc/dovecot/conf.d/auth-sql.conf.ext /etc/dovecot/conf.d/auth-sql.conf.ext.bak

image817f679b76d9dae7.png
修改配置文件/etc/dovecot/dovecot-sql.conf.ext

1
root@vultr:~# cp /etc/dovecot/dovecot-sql.conf.ext /etc/dovecot/dovecot-sql.conf.ext.bak

image0354ff625a0a3636.png
image514ada480d2a125b.png
image4c9c8aca7bfc34dc.png
image1c15171fa6395599.png
修改目录权限:

1
2
root@vultr:~# chown -R vmail:dovecot /etc/dovecot
root@vultr:~# chmod -R o-rwx /etc/dovecot

修改配置文件/etc/dovecot/conf.d/10-master.conf

1
root@vultr:~# cp /etc/dovecot/conf.d/10-master.conf /etc/dovecot/conf.d/10-master.conf.bak

imagea20ec47b88fca2ce.png
image4e9687811f105179.png
imagebfca00ca7cd7a7b0.png
image0d518babe9e18595.png
imagefa2838c1502bea54.png
image7e8916c4d34b7247.png
修改配置文件/etc/dovecot/conf.d/10-ssl.conf

1
root@vultr:~# cp /etc/dovecot/conf.d/10-ssl.conf /etc/dovecot/conf.d/10-ssl.conf.bak

imagedffa5da9c1693a03.png
重启devecot:

1
root@vultr:~# service dovecot restart

测试

用foxmail进行测试,测试失败了。

  • 修改FQDN为邮件域名
    参考文章
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    root@vultr:~# hostnamectl set-hostname mail.xiaokunjia.ml
    root@vultr:~# hostnamectl
    Static hostname: mail.xiaokunjia.ml
    Icon name: computer-vm
    Chassis: 略
    Machine ID: 略
    Boot ID: 略
    Virtualization: 略
    Operating System: Ubuntu 18.04.4 LTS
    Kernel: Linux 4.15.0-88-generic
    Architecture: x86-64
    修改/etc/hosts文件的回环地址解析,替换掉原本的主机名。
    imageb01eda0ff9eabb62.png
    总算是搭建完了一个私人邮局……
    明天再整理一下这个过程

    foxmail配置信息:
    image0a7388bfc4d3d950.png
    因为只有收件服务器用了ssl,发件服务器没有配置ssl。
    主要参考这篇文章操作的。
-------------本文结束感谢您的阅读-------------