如何使用Postfix,MariaDB,Dovecot和Roundcube设置邮件服务器

时间:2020-03-05 15:32:45  来源:igfitidea点击:

今天我们要在Ubuntu 16.04 LTS上使用Postfix,Devcot和MariaDB设置邮件服务器。
在本文中,我们将向我们展示如何设置Postfix(smtp服务器),Dovecot(imap/pop服务器)和MariaDB来存储有关虚拟域和用户的信息。
smtp服务器的任务是接受传入邮件并中继系统上授权用户的传出邮件。
而Dovecot允许授权用户访问其收件箱并阅读其中的任何邮件。
因此,本文的主要重点是使用虚拟用户设置快速安全的邮件服务器。

设置准备工作

在进行邮件服务器设置之前,必须满足一些准备工作。
这包括将域转发到服务器,并设置到服务器的静态IP地址。
打开此文件“/etc/hostname”以设置适当的主机名。

# vim /etc/hosts
172.25.10.171 ubuntu-16.theitroad.com ubuntu-16

为了在更广泛的网络上使用邮件服务器,必须正确配置主机域的DNS和MX记录。
然后,确保iptables防火墙或者任何其他外部防火墙没有阻止服务器上的任何标准邮件端口(25、465、587、110、995、143和993)。

设置域和主机名后,请在安装其他必需的软件包之前,运行以下命令以使用最新更新来更新系统。

# apt-get update

安装套件

现在,安装所有必需的软件包(包括“ Postfix”,“ Devcot”和“ MySQL”)以及一些其他必需的软件包,以使用root用户凭据通过以下命令来设置我们的邮件服务器。

# aptitude update && aptitude install apache2 postfix dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-mysql spamassassin clamav clamav-daemon clamav-base mariadb-client mariadb-server php

一旦按“ y”键继续,将提示我们配置后缀配置。
在后缀配置下选择“ Internet站点”,然后单击“确定”键继续。

接下来,将要求我们键入系统电子邮件名称,该名称将包含在电子邮件中。

单击“确定”按钮后,系统将处理一段时间以完成所有软件包的安装。

设置MariaDB数据库

安装完成并启用并运行上述服务后,我们将开始设置数据库和表来存储有关Postfix邮件帐户的信息。

运行以下命令以在MariaDB上配置root passowrd。

# mysql_secure_installation
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

现在连接到MariDB并运行以下命令来创建新数据库和MySQL用户,并授予新用户对新创建的数据库的权限。

# mysql -u root -p
MariaDB [(none)]> create database mailserver;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> GRANT SELECT ON mailserver.* TO 'mailuser'@'127.0.0.1' IDENTIFIED BY '*';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

现在,我们将使用以下命令创建一些表。
对于要在我们的域上接收邮件的域,请首先在命令下方运行。

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;

然后使用以下查询为电子邮件别名再创建一个表。

创建表virtual_aliases
idINT NOT NULL AUTO_INCREMENT,
domain_idINT NOT NULL,
sourcevarchar(100)NOT NULL,
destinationvarchar(100)NOT NULL,
主键(id),
删除级联上的外键(domain_id)参考virtual_domains(id)
)ENGINE = InnoDB DEFAULT CHARSET = utf8;

我们已经创建了一个数据库和一些必要的表,现在将通过使用以下查询将一些测试域添加到virtual_domains表中,从而向MySQL添加一些数据。

INSERT INTO `mailserver`.`virtual_domains`
(`id` ,`name`)
VALUES
('1', 'test.com'),
('2', 'ubuntu-16.test.com'),
('3', 'ubuntu-16'),
('4', 'localhost.test.com');

然后通过将电子邮件地址值替换为我们希望在邮件服务器上配置的地址,并将电子邮件地址值添加到virtual_users表中,并使用我们拥有的强密码更新密码值。

INSERT INTO `mailserver`.`virtual_users`
(`id`, `domain_id`, `password` , `email`)
VALUES
('1', '1', ENCRYPT('password', CONCAT('$', SUBSTRING(SHA(RAND()), -16))), '[email protected]'),
('2', '1', ENCRYPT('password', CONCAT('$', SUBSTRING(SHA(RAND()), -16))), '[email protected]');

运行以下查询以将数据插入表中以进行电子邮件别名设置。

INSERT INTO `mailserver`.`virtual_aliases`
(`id`, `domain_id`, `source`, `destination`)
VALUES
('1', '1', '[email protected]', '[email protected]');

测试MariaDB数据

在将所有必需的信息输入到MariaDB之后,检查数据是否在那里。

让我们在查询下面运行,以首先检查virtual_domains表的内容。

MariaDB [mailserver]> SELECT * FROM mailserver.virtual_domains;
+----+--------------------------+
| id | name |
+----+--------------------------+
| 1 | test.com |
| 2 | ubuntu-16.test.com |
| 3 | ubuntu-16 |
| 4 | localhost.test.com |
+----+--------------------------+
4 rows in set (0.00 sec)

如图所示,使用以下查询检查“ virtual_users”和“ virtual_aliases”表以验证哈希密码是否应该存在。

MariaDB [mailserver]> SELECT * FROM mailserver.virtual_users;
+----+-----------+---------------------------------------------+-----------------------+
| id | domain_id | password | email |
+----+-----------+--------------------+-----------------------+
| 1 | 1 | $b4046061316dbf73$eth.RbLdk2Z1ArUti2yYzoF0T8/xz1wbVrrX1RticBNTeKz4wWKj23zj49UOSW95njitEWv65tlketGVvzRz01 | [email protected] |
| 2 | 1 | $a193a0a79bd30c14$RN1HiqeeuwJ2uvjY43VO0vLLj2GFpJpPtOu3rCZH66qVWIlFUcRDg/7gr9cpVuyYSGejXoF7D69YgI/vQPR17. | [email protected] |
+----+-----------+--------------------------------------------------+-----------------------+
2 rows in set (0.00 sec)
MariaDB [mailserver]> SELECT * FROM mailserver.virtual_aliases;
+----+-----------+----------------+-----------------------+
| id | domain_id | source | destination |
+----+-----------+----------------+-----------------------+
| 1 | 1 | [email protected] | [email protected] |
+----+-----------+----------------+-----------------------+
1 row in set (0.00 sec)

设置Postfix配置

在MySQL数据库设置之后,现在我们将设置Postfix,以便服务器可以接受域的传入消息。
如果需要恢复为默认配置,请创建默认Postfix配置文件的副本。

# cp /etc/postfix/main.cf /etc/postfix/main.cf.org

然后使用vi或者vim编辑器打开文件以匹配以下配置,但不要忘记更新域和主机名。

# vi /etc/postfix/main.cf
# See /usr/share/postfix/main.cf.dist for a commented, more complete version
# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
# appending .domain is the MUA's job.
append_dot_mydomain = no
# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h
readme_directory = no
# TLS parameters
#smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
#smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
#smtpd_use_tls=yes
#smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
#smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_tls_cert_file=/etc/dovecot/dovecot.pem
smtpd_tls_key_file=/etc/dovecot/private/dovecot.pem
smtpd_use_tls=yes
smtpd_tls_auth_only = yes
#Enabling SMTP for authenticated users, and handing off authentication to 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
# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.
myhostname = ubuntu-16.test.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
#mydestination = test.com, ubuntu-16.test.com, localhost.test.com, localhost
mydestination = localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
#Handing off local delivery to Dovecot's LMTP, and telling it where to store mail
virtual_transport = lmtp:unix:private/dovecot-lmtp
#Virtual domains, users, and aliases
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:/etc/postfix/mysql-virtual-email2email.cf

进行保存的更改后,关闭文件,然后为虚拟域创建一个新文件,并确保我们根据自己的设置更新这些更改。

#vim /etc/postfix/mysql-virtual-mailbox-domains.cf
user = mailuser
password = mailuser_pass
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_users WHERE email='%s'

创建下面的文件,并使用密码将下面的内容放入其中。

# vim /etc/postfix/mysql-virtual-mailbox-maps.cf
user = mailuser
password = mailuser_pass
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_users WHERE email='%s'

保存并关闭文件,然后创建新文件'/etc/postfix/mysql-virtual-alias-maps.cf'并输入如下所示的值。

# vi /etc/postfix/mysql-virtual-alias-maps.cf
user = mailuser
password = mailuser_pass
hosts = 127.0.0.1
dbname = mailserver
query = SELECT destination FROM virtual_aliases WHERE source='%s'

然后创建“ /etc/postfix/mysql-virtual-email2email.cf”文件并输入以下值。

# vi /etc/postfix/mysql-virtual-email2email.cf
user = mailuser
password = mailuser_pass
hosts = 127.0.0.1
dbname = mailserver
query = SELECT email FROM virtual_users WHERE email='%s'

保存并关闭文件,然后运行以下命令重新启动postfix服务。

# service postfix restart

运行以下命令,以确保Postfix可以找到第一个域,并且该域应成功返回“ 1”。

# postmap -q test.com mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf

现在通过输入以下命令来测试Postfix,以验证它是否可以找到别名,方法是将[email protected]替换为我们使用的实际别名。

# postmap -q [email protected] mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf

我们应该再次收到1作为输出。
然后通过输入以下命令来测试Postfix以验证它是否可以找到别名。

# postmap -q [email protected] mysql:/etc/postfix/mysql-virtual-alias-maps.cf

这将返回别名转发到的电子邮件地址,如下图所示。

接下来,我们将配置“ /etc/postfix/master.cf”文件。
在进行更改之前,请先保存副本。

# cp /etc/postfix/master.cf /etc/postfix/master.cf.org

然后打开配置文件进行编辑,并取消注释以'submission'和'smtps'开头的两行以及以'-o'开头的两行,分别如下所示。

# vim /etc/postfix/master.cf

保存配置,然后在下面运行命令以重新启动postfix服务,并且我们已成功完成postfix配置。

# service postfix restart

Dovecot配置设置

Dovecot允许用户使用POP3和IMAP登录并检查其电子邮件。
其中我们将配置Dovecot以强制用户在连接时使用SSL,这样就永远不会将其密码以纯文本形式发送到服务器。
让我们从复制所有原始配置开始,然后进行任何更改。

root@ubuntu-16:~# cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.org
root@ubuntu-16:~# cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.org
root@ubuntu-16:~# cp /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.org
root@ubuntu-16:~# cp /etc/dovecot/dovecot-sql.conf.ext /etc/dovecot/dovecot-sql.conf.ext.org
root@ubuntu-16:~# cp /etc/dovecot/conf.d/10-master.conf /etc/dovecot/conf.d/10-master.conf.org
root@ubuntu-16:~# cp /etc/dovecot/conf.d/10-ssl.conf /etc/dovecot/conf.d/10-ssl.conf.org
# vim /etc/dovecot/dovecot.conf
# Enable installed protocols
!include_try /usr/share/dovecot/protocols.d/*.protocol

保存此文件后,打开“ /etc/dovecot/conf.d/10-mail.conf”文件以修改配置文件中的以下变量,该变量控制Dovecot如何与服务器的文件系统交互以存储和检索消息。

# vim /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:/var/mail/vhosts/%d/%n
mail_privileged_group = mail

运行以下命令以创建域的文件夹,然后创建用户和组ID为“ 5000”的“ vmail”用户。
该用户将负责从服务器读取邮件。

# mkdir -p /var/mail/vhosts/test.com
# groupadd -g 5000 vmail
# useradd -g vmail -u 5000 vmail -d /var/mail

然后将“/var/mail /”文件夹及其内容的所有者更改为属于vmail用户。

如下所示打开用户身份验证文件,并通过取消注释以下行来禁用纯文本身份验证。

#vim /etc/dovecot/conf.d/10-auth.conf
auth_mechanisms = plain login

在同一文件中,注释系统用户登录行,并通过取消对“ auth-sql.conf.ext”行的注释来启用MySQL身份验证,如图所示。

#!include auth-system.conf.ext
!include auth-sql.conf.ext

保存配置文件,然后通过将以下行粘贴到文件中来编辑带有身份验证信息的“ /etc/dovecot/conf.d/auth-sql.conf.ext”文件,如下所示。

# vim /etc/dovecot/conf.d/auth-sql.conf.ext

保存后,使用自定义MySQL连接信息更新'/etc/dovecot/dovecot-sql.conf.ext'文件。

# vim /etc/dovecot/dovecot-sql.conf.ext
# Database driver: mysql, pgsql, sqlite
driver = mysql
connect = host=127.0.0.1 dbname=mailserver user=mailuser password=mailuser_pass
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

通过将协议端口设置为0来禁用未加密的IMAP和POP3,如下所示。
确保IMAPS和pop3s条目下方的port和ssl条目未注释。

service imap-login {
inet_listener imap {
#port = 143
}
inet_listener imaps {
port = 993
ssl = yes
}
}
service pop3-login {
inet_listener pop3 {
port = 0
}
inet_listener pop3s {
port = 995
ssl = yes
}
}
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 {
# auth_socket_path points to this userdb socket by default. It's typically
# used by dovecot-lda, doveadm, possibly imap process, etc. Its default
# permissions make it readable only by root, but you Jan need to relax these
# permissions. Users that have access to this socket are able to get a list
# of all usernames and get results of everyone's userdb lookups.
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
unix_listener auth-userdb {
mode = 0600
user = vmail
#group =
}
# Postfix smtp-auth
user = dovecot
}

取消注释用户行,并在服务auth-section下将其设置为vmail。

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 = root
user = vmail
}

就是这样,现在保存配置文件并在以下命令中运行以重新启动Dovecot服务。

# service dovecot restart

现在,在电子邮件客户端中设置任何测试帐户,以确保一切正常。
提供完整的电子邮件地址,包括域名和为该电子邮件地址添加到MariaDB表中的密码。
然后尝试从外部电子邮件帐户向该帐户发送电子邮件,然后回复。
之后,检查/var/log/mail.log中的邮件日志文件以了解传入消息,并检查第二个块以了解传出消息。

为Webmail界面设置Roundcube

我们可以使用支持“ smtp”和“ pop/imap”的电子邮件客户端,但是webmail部分是完全可选的。

运行以下命令在Ubuntu 16.04服务器上安装Roundcube及其插件,然后按“ Y”键开始安装过程。

# apt-get install roundcube roundcube-plugins roundcube-plugins-extra

在安装过程中,将要求我们提供一个已安装和配置的数据库,然后才能使用该数据库。
这是可选的,我们将在这里选择“否”,因为我们已经设置了数据库。

安装后,使用任何编辑器打开其配置文件,并将以下参数放入其中,如图所示。

# vim /etc/roundcube/config.inc.php
$rcmail_config['default_host'] = 'localhost';
$rcmail_config['imap_cache'] = memcache;
$rcmail_config['messages_cache'] = db

进行更改后,保存并关闭文件。
然后在Web服务器中创建虚拟主机,并启动我们喜欢的Web浏览器以开始配置Roundcube。
Roundcube的图形配置的第一步是环境检查。
单击页面底部的NEXT按钮继续。