云服务器
Postfix 是一个标准的 MTA「Mail Transfer Agent」服务器,它负责通过 SMTP 协议管理发送到本机的邮件以及由本机发向外界的邮件;Dovecot 是一个优秀的 IMAP/POP 服务器用以接收外界发送到本机的邮件。本实验带您一步步搭建 Postfix、Dovecot 邮件服务。
首次可免费使用云主机 45 分钟 ,到期后云主机将被重置并退库,若想保留成果请及时留用。
# 搭建 Postfix、Dovecot 邮件服务
## 准备域名
> <time>15min ~ 20min</time>
### 域名注册
如果您还没有域名,可以[在腾讯云上选购][buy_link],过程可以参考下面的视频。
* [视频 - 在腾讯云上购买域名][buy_domain_video]
> <link for="buy_link" href="https://dnspod.qcloud.com/?fromSource=lab"></link>
> <video for="buy_domain_video" platform="qq" vid="p05077pwelw" name="域名选购"></video>
### 域名解析
域名购买完成后, 需要将域名解析到实验云主机上,实验云主机的 IP 为:
```
${runtime.vars.cvmIpAddress}
```
在腾讯云购买的域名,可以[到控制台添加解析记录][https://console.qcloud.com/domain],过程可参考下面的视频:
* [视频 - 如何在腾讯云上解析域名][video_resolve_domain]
> <video for="video_resolve_domain" platform="qq" vid="t0507ps9kxo" name="域名解析"></video>
完成该实验共需要添加两条记录:
#### A 记录
记录类型:A
主机记录:@
记录值:${runtime.vars.cvmIpAddress}
#### MX 记录
记录类型:MX
主机记录:@
记录值:`yourdomain.com`(替换为自己域名)
### 生效检查
域名设置解析后需要过一段时间才会生效,通过 `ping` 命令检查域名是否生效 [:question][replace],如:
> <bubble for="replace">注意替换下面命令中的 `yourdomain.com` 为您自己的注册的域名</bubble>
```
ping yourdomain.com
```
如果 ping 命令返回的信息中含有你设置的解析的 IP 地址,说明解析成功。
(使用 `ctrl + c` 停止)
#### 检查 MX 记录
替换下面命令中的 `yourdomain.com` 为您自己的注册的域名:
```
nslookup -q=mx yourdomain.com
```
如果 nslookup 命令返回的信息中含有你设置的域名的记录值,说明解析成功。
## 实验之前
> <time>5min</time>
配置一个功能完善的邮件服务器并不是一项容易的工作,本实验只是搭建了一个简单的邮件服务器,一些更为强大的功能还需要你去探索。
### Postfix、Dovecot 简介
#### Postfix
Postfix 是一个标准的 MTA「Mail Transfer Agent」服务器,它负责通过 SMTP 协议管理发送到本机的邮件以及由本机发向外界的邮件。
#### Dovecot
Dovecot 是一个优秀的 IMAP/POP 服务器用以接收外界发送到本机的邮件。
### Postfix、Dovecot 安装
在 CentOS 7 上,我们可以直接使用 `yum` 进行下载安装:
```
yum -y install postfix dovecot
```
> <checker type="output-contains" command="ls -la /etc" hint="安装postfix">
> <keyword regex="postfix" />
> </checker>
> <checker type="output-contains" command="ls -la /etc" hint="安装dovecot">
> <keyword regex="dovecot" />
> </checker>
## Postfix
> <time>5min ~ 10min</time>
### 配置 Postfix
有关教程中配置参数的具体含义,请参照 [Postfix 配置文档][Postfix]。
> <link for="Postfix" href="http://www.postfix.org/documentation.html"></link>
#### 配置
在终端中输入以下命令以修改 Postfix 相关配置:
注意:**记得将 `yourdomain.com` 替换为你自己的域名**
```
postconf -e 'myhostname = server.yourdomain.com'
postconf -e 'mydestination = localhost, localhost.localdomain'
postconf -e 'myorigin = $mydomain'
postconf -e 'mynetworks = 127.0.0.0/8'
postconf -e 'inet_interfaces = all'
postconf -e 'inet_protocols = all'
postconf -e 'mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain'
postconf -e 'home_mailbox = Maildir/'
postconf -e 'smtpd_sasl_type = dovecot'
postconf -e 'smtpd_sasl_path = private/auth'
postconf -e 'smtpd_sasl_auth_enable = yes'
postconf -e 'broken_sasl_auth_clients = yes'
postconf -e 'smtpd_sasl_authenticated_header = yes'
postconf -e 'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination'
postconf -e 'smtpd_use_tls = yes'
postconf -e 'smtpd_tls_cert_file = /etc/pki/dovecot/certs/dovecot.pem'
postconf -e 'smtpd_tls_key_file = /etc/pki/dovecot/private/dovecot.pem'
```
配置中 Postfix 使用 **sasl** 和 **tls** 来完成身份认证和传输信息加密。
试验中使用了 Dovecot 默认的 **ssl** 证书和私钥,如果你需要修改为自己的,请替换最后两行配置的路径。
#### 配置 smtps
部分邮件客户端依赖于使用 465 端口提供加密连接,所以我们修改配置,允许 Postfix 使用 465 端口发送邮件。
打开 [/etc/postfix/master.cf][master_cf] 文件,将如下两行前的 `#` 去除:
> <locate for="master_cf" path="/etc/postfix/master.cf" hint="master.cf"></locate>
```
smtps inet n - n - - smtpd
-o smtpd_tls_wrappermode=yes
```
(注意:** `-o` 前要保留空格**)
然后 `ctrl + s` 保存文件。
#### 启动
使用以下命令,将 Postfix 设为自动启动并首次启动该服务:
```
systemctl enable postfix.service
systemctl start postfix.service
```
> <checker type="output-contains-no" command="cat /etc/postfix/master.cf" hint="配置 smtps">
> <keyword regex="#smtps" />
> </checker>
> <checker type="output-contains" command="cat /var/log/maillog" hint="启动 Postfix">
> <keyword regex="started" />
> </checker>
### Postfix 日志
Postfix 系统的日志文件在系统的这个目录下的 [/var/log/maillog][locate_log] 文件,此文件记录了 Postfix 服务器的运行状态信息。
> <locate for="locate_log" path="/var/log/maillog" hint="Postfix 日志文件"></locate>
## Dovecot
> <time>5min ~ 10min</time>
### 配置 Dovecot
#### 修改 dovecot.conf
打开 [/etc/dovecot/dovecot.conf][dovecot_conf] 文件,在最下方加入以下配置:
> <locate for="dovecot_conf" path="/etc/dovecot/dovecot.conf" hint="dovecot.conf"></locate>
```
/// <example verb="edit" file="/etc/dovecot/dovecot.conf" />
ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
ssl_key = </etc/pki/dovecot/private/dovecot.pem
protocols = imap pop3 lmtp
listen = *
mail_location = Maildir:~/Maildir
disable_plaintext_auth = no
```
如果前面你修改为了自己的 ssl 证书和私钥,请替换开始两行配置的路径。
然后 `ctrl + s` 保存文件。
#### 修改 10-master.conf
打开 [/etc/dovecot/conf.d/10-master.conf][10-master_conf] 文件,找到 `service auth` 部分,将以下行前面的 `#` 去除:
> <locate for="10-master_conf" path="/etc/dovecot/conf.d/10-master.conf" hint="10-master.conf"></locate>
```
unix_listener /var/spool/postfix/private/auth {
mode = 0666
}
```
然后 `ctrl + s` 保存文件。
> <checker type="output-contains" command="cat /etc/dovecot/dovecot.conf" hint="修改 dovecot.conf">
> <keyword regex="dovecot.pem" />
> </checker>
> <checker type="output-contains-no" command="cat /etc/dovecot/conf.d/10-master.conf" hint="修改 10-master.conf">
> <keyword regex="#unix_listener /var/spool/postfix/private/auth" />
> </checker>
### 启动 Dovecot
使用以下命令,将 Dovecot 设为自动启动并首次启动该服务:
```
systemctl enable dovecot.service
systemctl start dovecot.service
```
查看 [/var/log/maillog][locate_log] 文件,查看服务是否成功启动。
> <locate for="locate_log" path="/var/log/maillog" hint="Postfix 日志文件"></locate>
如成功启动,日志里应包含如下信息:
```
Jun 26 12:00:28 localhost postfix/postfix-script[28338]: starting the Postfix mail system
Jun 26 12:00:29 localhost postfix/master[28340]: daemon started -- version 2.10.1, configuration /etc/postfix
Jun 26 12:28:40 localhost dovecot: master: Dovecot v2.2.10 starting up for imap, pop3, lmtp (core dumps disabled)
```
> <checker type="output-contains" command="cat /var/log/maillog" hint="启动 Dovecot">
> <keyword regex="Dovecot" />
> </checker>
## 创建账户
> <time>5min ~ 10min</time>
该配置下邮箱账户依赖于系统用户,所以通过添加系统用户的方式创建邮箱账户。
### 添加用户
在终端中使用 `useradd` 命令添加用户:
```
useradd test
```
使用 `passwd` 命令设置对应用户密码:
```
passwd test
```
## 测试
> <time>10min ~ 15min</time>
如测试中遇到异常,请查看 maillog 日志文件中的错误信息。
### 服务器端发送测试
使用 `su` 命令切换用户:
```
su test
```
我们可以使用 `mail` 命令发送邮件,将 `xxxx@xxx.com` 替换为你的其他邮箱。
```
echo "Mail Content" | mail -s "Mail Subject" xxxx@xxx.com
```
然后可以前往你的接收方邮箱查收。
### 邮件客户端
你可以将该邮箱账户添加至邮件客户端使用,推荐使用 [Foxmail][foxmail_link] 客户端。
> <link for="foxmail_link" href="http://www.foxmail.com"></link>
可以参考以下配置进行设置:
```
服务器类型:`POP3`
邮箱账户: `test@yourdomain.com`
收件(POP3)服务器: `yourdomain.com`
端口: `995`
安全连接(SSL): `是`
用户名: `test`
密码: `test用户密码`
发件(SMTP)服务器: `yourdomain.com`
端口: `465`
安全连接(SSL): `是`
用户名: `test`
密码: `test用户密码`
```
然后就可以使用邮件客户端通过该账户收发邮件了。
(该配置在某些邮件客户端下可能因为使用了通用证书而出现使用异常)