LetsEncrypt certbot 多个更新挂钩

LetsEncrypt certbot multiple renew-hooks

我正在通过 LetsEncrypt 的 certbot 自动更新 SSL 证书。实际续订工作正常,但我需要自动重新启动服务,以便它们加载更新的证书。我想知道您是否可以在 letsencrypt renew?

的 cronjob 中使用多个 --renew-hook 参数

如何在证书续订时自动重启服务?

是的,您可以使用多个 --renew-hook 语句。还可以使用 -q 标志,这样它会通过电子邮件向您发送空白通知,直到实际发生续订为止。在更新发生之前,它也不会重新启动您的任何服务。如果您愿意,这也会将日志文件附加到电子邮件中。

我有一个每天运行 bash 的 cron。

我的 bash (certbotrenew.sh) 里面就是这个

#!/bin/bash
cd /opt/certbot
sudo ./certbot-auto renew --renew-hook "service postfix reload" --renew-hook "service dovecot restart" --renew-hook "service apache2 reload" -q >> /var/log/certbot-renew.log | mail -s "CERTBOT Renewals" me@myemail.com  < /var/log/certbot-renew.log
exit 0

我的 cron 是

00 20 * * 1 /bin/certbotrenew.sh

有些人质疑为什么我不管什么事都发邮件,我只是想知道我每天的 crons 是 运行。

从我在 CertBot Ubuntu 16.04 中的全新安装中看到的情况来看,它创建了一个 cron 作业:

# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates haven't been revoked, etc.  Renewal will only occur if expiration is within
# 30 days.
SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --pre-hook 
'/bin/run-parts /etc/letsencrypt/pre-hook.d/' --post-hook '/bin/run-parts /etc/letsencrypt/post-hook.d/' --renew-hook '/bin/run-parts
/etc/letsencrypt/renew-hook.d/'

所以它在许多目录上执行run-parts,包括/etc/letsencrypt/renew-hook.d/

你只需要在任何一个挂钩目录中添加一个可执行文件(选择你需要的那个)。

例如,在我的 renew-hook.d 中,我创建了一个包含以下内容的文件 restart-nginx

#!/bin/bash
/etc/init.d/nginx restart

注意:您可以使用 --test 选项知道 run-parts 将调用哪些文件。 (示例 run-parts --test /etc/letsencrypt/renew-hook.d/

您还可以在文件 /etc/letsencrypt/cli.ini (see documentation) 中将挂钩(以及其他选项,如果您愿意)设置为全局选项,如下所示:

# Global config for letsencrypt runs
#
# Note that these options apply automatically to all use of Certbot for
# obtaining or renewing certificates, so options specific to a single
# certificate on a system with several certificates should not be placed
# here.

renew-hook = service postfix reload
post-hook = service nginx reload

您必须先在大多数系统上创建文件。 Letsencrypt 没有。

如果您不喜欢全球化,您也可以在每个 renewal 文件夹中创建证书特定版本。

还有一个控制 certbot 运行ning 的地方(在 ubuntu 16.04 + nginx — 下面的设置文件)

1) systemd 定时器

运行 命令:sudo systemctl list-timers

并查看输出:

Sun 2018-07-08 00:46:59 EEST 7h left Sat 2018-07-07 12:36:26 EEST 4h 51min ago certbot.timer certbot.service

然后

2)

检查systemctl 运行 Certbot

时控制时间的文件
/etc/systemd/system/timers.target.wants/certbot.timer

这是

的符号link
/lib/systemd/system/certbot.timer

注意定义时间 + 随机秒数的这一行(设置随机时间是为了不给 letsencrypt 服务器造成压力)

OnCalendar=--* 00,12:00:00

RandomizedDelaySec=3600

运行 钩子脚本的最新推荐方法来自 /etc/letsencrypt/cli.ini。如果该文件不存在,您可以自己创建。另一件事是您应该使用 --deploy-hook 而不是 --renew-hook。 --renew-hook 仍然存在,但将被逐步淘汰,因为它甚至没有在最新文档中提及。

所以只要创建 /etc/letsencrypt/cli.ini 如果它不存在并添加以下行:

deploy-hook = "service postfix reload ; service dovecot restart ; service apache2 reload"

重新加载那些特定的服务。

不确定这是否仅适用于较新的版本,但希望有人会发现它有用。 当您至少添加了 1 个域时,certbot 将创建 "renewal-hooks" 目录,其中包含 3 个子目录 "deploy"、"post"、"pre".

如果您将任何脚本放入"post" 文件夹,该脚本将在更新后自动执行。 不要忘记通过在脚本中添加 +x 使其可执行。

我只使用了一个“001-restart-nginx.sh”,内容如下:

#!/bin/bash
echo "ssl certs updated" && service nginx restart

/etc/letsencrypt/renewal-hooks/post/001-restart-nginx.sh

这样您就不必手动提供 --post-hook 带有某些指令的参数。

在实际续订过程中,您会看到如下内容:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/<your-domain-name>/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Running post-hook command: /etc/letsencrypt/renewal-hooks/post/001-restart-nginx.sh
Output from post-hook command 001-restart-nginx.sh:
ssl certs updated