以下是在 Dovecot 设定使用 Let’s Encrypt, 替 POP3 及 IMAP 加密的步骤。
如果安装 Dovecot 的系统已经安装了 HTTPD Server, 执行以下指令:
|
1 |
# certbot certonly --webroot -w /var/www/html -d mail.mydomain.com |
上面的 /var/www/html 要改成 httpd server 的网页目录,而 mail.mydomain.com 则要改成主机名称,如果要加入多个主机名,在后面加入 -d domain 即可。
如果没有安装 httpd server, 可以用 certbot 提供的 standalone 选项:
|
1 |
# certbot certonly --standalone -d mail.mydomain.com |
执行上面指令后,SSL 凭证便会储存在 /etc/letsencrypt/live/mail.mydomain.com/ 目录下,开启 Dovecot 的 ssl 设定档 /etc/dovecot/conf.d/10-ssl.conf:
将 ssl_cert 及 ssl_key 设定成刚刚取得的凭证,注意路径前面加入入 “
|
1 2 |
ssl_cert = </etc/letsencrypt/live/mail.mydomain.com/fullchain.pem ssl_key = </etc/letsencrypt/live/mail.mydomain.com/privkey.pem |
储存盘案后,重新启动 Dovecot:
最后设定自动 renew SSL 凭证
Let’s Encrypt 凭证目前的有效期只有 3 个月,在凭证到期前 1 个月可以 renew以下设定自动 renew 凭证,以下是更新凭证的指令:
|
1 |
certbot renew --quiet --agree-tos --post-hook "systemctl restart dovecot" |
以上指令会自动更新所有 SSL 凭证,如果成功更新,会执行 “systemctl restart dovecot” 这条指令,让 Dovecot 重新加载新凭证。
为了日后方便管理,建立一个 renew SSL 的 Shell Script:
加入以下内容:
|
1 2 |
#!/bin/sh /usr/bin/certbot renew --quiet --agree-tos --post-hook "systemctl restart dovecot" |
然后把上面 Shell Script 加入可执行权限及放到 crontab:
加入 crontab
加入以下一行:
|
1 |
0 3 * * * /root/renew.sh > /dev/null 2>&1 |
这样 certbot 便会在每天凌晨 3:00 自动检查及更新凭证。