LetsEncrypt certbot多个续订钩子

Att*_*nen 12 lets-encrypt

我正在从LetsEncrypt的certbot自动更新SSL证书.实际更新正在运行,但我需要自动重新启动服务,以便加载续订的证书.我想知道你是否可以--renew-hook在cronjob中使用多个参数letsencrypt renew

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

Dev*_*net 21

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

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

我只使用了一个带有以下内容的“001-restart-nginx.sh”:

#!/bin/bash
echo "ssl certs updated" && service nginx restart
Run Code Online (Sandbox Code Playgroud)

/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
Run Code Online (Sandbox Code Playgroud)

  • 最好使用“nginx reload”,因为重新启动会导致不必要的停机。 (5认同)

Mit*_*llK 13

是的,您可以使用多个--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
Run Code Online (Sandbox Code Playgroud)

而我的cron是

00 20 * * 1 /bin/certbotrenew.sh
Run Code Online (Sandbox Code Playgroud)

有些人质疑我发送电子邮件的原因,无论是否发生任何事情,我总是想知道我的日常用品是在运行.

  • @MitchellK - 而不是每天收到cron电子邮件,如何通过https://IsItWorking.info(我的)来获取cron报告服务.您的cron脚本可以在每次成功运行时签入.如果没有签入,则服务会提醒您.(可能除了电子邮件之外) (2认同)

Jor*_*eFG 8

从我在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 Code Online (Sandbox Code Playgroud)

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

您只需要在任何这些钩子目录中添加一个可执行文件(选择您需要的那个).

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

#!/bin/bash
/etc/init.d/nginx restart
Run Code Online (Sandbox Code Playgroud)

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

  • 他们实际上将certbot cronjob更改为不再包含那些运行部件参数.现在推荐的方法是创建一个`/ etc/letsencrypt/cli.ini`并在那里指定钩子参数,参见https://github.com/certbot/certbot/issues/1706#issuecomment-302774426 (3认同)

lor*_*key 6

您还可以将钩子(如果需要,还可以将钩子和其他选项)设置为文件中的全局选项/etc/letsencrypt/cli.ini请参阅文档),如下所示:

# 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
Run Code Online (Sandbox Code Playgroud)

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

renewal如果您不希望进入全局目录,则还可以在每个文件夹中创建特定于证书的版本。


zox*_*xxx 5

运行钩子脚本的最新推荐方法来自/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"
Run Code Online (Sandbox Code Playgroud)

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