大网数据广告活动
服务器运维问答

When using Let's Encrypt certbot, how do I restart/reload a network service only once and only if the cerificate was actually renewed?

· 52CE 我爱测

The certbot command provides two hooks that run after automated renewals, from the docs: --post-hook POST_HOOK Command to be run in a shell after attempting to obtain/renew certificates. Can be used to deploy renewed certificates…

问题原文

原帖链接 · Walf · CC BY-SA 4.0

The certbot command provides two hooks that run after automated renewals, from the docs:

 --post-hook POST_HOOK
                       Command to be run in a shell after attempting to
                       obtain/renew certificates. Can be used to deploy
                       renewed certificates, or to restart any servers that
                       were stopped by --pre-hook. This is only run if an
                       attempt was made to obtain/renew a certificate. If
                       multiple renewed certificates have identical post-
                       hooks, only one will be run. (default: None)
 --deploy-hook DEPLOY_HOOK
                       Command to be run in a shell once for each
                       successfully issued certificate. For this command, the
                       shell variable $RENEWED_LINEAGE will point to the
                       config live subdirectory (for example,
                       "/etc/letsencrypt/live/example.com") containing the
                       new certificates and keys; the shell variable
                       $RENEWED_DOMAINS will contain a space-delimited list
                       of renewed certificate domains (for example,
                       "example.com www.example.com" (default: None)

This issue is outlined in this (now closed) LE thread and is basically about minimising interruption to services. POST_HOOK executes every time an attempt to renew is made even if no certificates were issued, though only once. This makes it possible to unnecessarily restart services. DEPLOY_HOOK runs for each and every successful certificate renewal. If one uses DEPLOY_HOOK, and has multiple certificates, each service may restart multiple times when once is enough. More info on renewal hooks here.

I use an issuance method that does not interrupt my services at all, e.g.:

certbot certonly --webroot ...

or

certbot certonly --dns-PROVIDER ...

I want to restart/reload each dependent service only once, and only if its certificate actually changed.

已采纳解答

原帖链接 · Walf · CC BY-SA 4.0

I was able to overcome this limitation by using both hooks in combination with a simple script (published in this Gist). Instead of each successful renewal immediately triggering a reload/restart, its --deploy-hook/renew_hook only marks the service for an action (by creating temporary files under /run). Then when the --post-hook/post_hook runs, it checks for marked services and performs the necessary actions only once. This guarantees that nothing happens to a service unless a certificate it depends on actually changed, and it only happens once.

For example, to reload nginx, and restart vsftpd when a certificate they share is renewed:

certbot certonly ... \
    --deploy-hook '/usr/local/sbin/read-new-certs-services nginx --restart vsftpd' \
    --post-hook /usr/local/sbin/read-new-certs-services

Another certificate may be web-only, so it would have these hook parameters:

certbot certonly ... \
    --deploy-hook '/usr/local/sbin/read-new-certs-services nginx' \
    --post-hook /usr/local/sbin/read-new-certs-services

The script must be used on both hooks for every certificate issue so that none use a conflicting method of restarting services.

You can change existing certificate renewals to use this method by editing their /etc/letsencrypt/renewal/*.conf files to contain hooks like this in the [renewalparams] section:

renew_hook = /usr/local/sbin/read-new-certs-services nginx -s vsftpd
post_hook = /usr/local/sbin/read-new-certs-services

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-05-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。