让我们加密更新的 Cron 作业

use*_*600 126 cron lets-encrypt

这是在 Apache2 中设置cron以更新 Let's Encrypt 证书的正确方法吗?我使用 Ubuntu 16.04。

@monthly letsencrypt renew && service apache2 reload
Run Code Online (Sandbox Code Playgroud)

Mic*_*ton 190

每月不够频繁。

此脚本应至少每周运行一次,最好每天运行一次。请记住,除非证书即将到期,否则不会更新证书,并且每月可能会导致您现有的证书在更新之前偶尔已经过期。

该程序的名称是certbot,从letsencrypt. 如果您仍在使用letsencrypt,则需要更新到当前版本。

除了这些问题,它与我的 cron 工作大致相同。

43 6 * * * certbot renew --post-hook "systemctl reload nginx"
Run Code Online (Sandbox Code Playgroud)

注意:在 18.04 LTS 中,letsencrypt包已(最终)重命名为certbot. 它现在包括一个systemd计时器,您可以启用它来安排certbot续订,使用systemctl enable certbot.timersystemctl start certbot.timer。但是,Ubuntu 没有提供指定钩子的方法。在 Canonical 修复此问题之前,您需要设置覆盖certbot.serviceExecStart=使用所需的命令行覆盖。

  • 使用 `--renew-hook` 而不是 `--post-hook` 可能更好,只有在证书成功更新后才重新启动。 (15认同)
  • 对于 apache/httpd,`certbot 更新` 将正常工作™ (10认同)
  • 什么时间窗口“接近到期”? (5认同)
  • 我读到使用 ```--deploy-hook``` 而不是 ```--renew-hook```,但我不确定它是否重要。是否有必要重新加载apache?我认为 python-certbot-apache 包在需要时为我们做这件事。 (2认同)
  • 您不必覆盖服务。您可以将带有“systemctl reload nginx”的可执行脚本放入 /etc/letsencrypt/renewal-hooks/post/100-reload-nginx (2认同)

小智 72

我最近(2017 年 10 月)在 Ubuntu 16.04 服务器上安装并运行了 certbot,并在/etc/cron.d/certbot.

这是创建的 cron 作业:

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew
Run Code Online (Sandbox Code Playgroud)

在创建 crontab 条目之前检查此文件是否已存在是个好主意。

  • 值得注意的是,如果 `/run/systemd/system` 存在,上面的 cron 作业**不会**运行 `certbotrenew` - 这是因为 systemd 计时器正在运行 certbot - [阅读更多关于 certbot 和systemd 计时器在这里](https://community.letsencrypt.org/t/cerbot-cron-job/23895/5)。 (14认同)
  • 我在运行 certbot 后看到我也有这个。非常好,让加密做到了!这是一个伟大的项目。 (4认同)
  • 对于任何想知道的人来说,这使它每 12 小时运行一次。另一个答案 `43 6 * * *` 将使其每天早上 6:43 运行。一天一次就足够了,但任何一个都可以正常工作。 (3认同)

gla*_*aux 54

certbot文档建议运行该脚本每天两次:

笔记:

如果您正在设置 cron 或 systemd 作业,我们建议每天运行两次(在您的证书到期续订或吊销之前它不会做任何事情,但定期运行它会让您的网站有机会保持在线状态)如果出于某种原因发生了 Let's Encrypt 发起的撤销)。请为您的续订任务选择一小时内的随机分钟。

正如 Michael Hampton 提到的,名称已更改为 certbot,但他们仍然提供 -auto 选项以保持自身更新。该certbot-auto命令需要 root 权限才能运行,因此您的 cron 脚本中的行应如下所示:

52 0,12 * * * root /full/path/to/certbot-auto renew --quiet
Run Code Online (Sandbox Code Playgroud)

在我自己的情况下,certbot-auto脚本放置在 git-user 的主目录中。确切的命令是

52 0,12 * * * root /home/git/certbot-auto renew --quiet
Run Code Online (Sandbox Code Playgroud)

请注意,文档中的示例对应于相对路径,如可能会混淆的点所示:

./path/to/certbot-auto renew --quiet

确保事先在 shell 中测试运行更新命令以测试路径,如果证书未到期更新,则不会发生任何事情(不带--quiet标志运行此测试以查看发生了什么)。

以这种方式续订证书时,不一定需要重新加载服务器,因为如果设置正确,活动证书的路径不会更改。

如果您正在运行 apache,这是正确的 - 对于 nginx,请考虑添加一个更新钩子,例如:

52 0,12 * * * root certbot renew --renew-hook 'service nginx reload'
Run Code Online (Sandbox Code Playgroud)

在 docker 环境中(编辑 2020-09-18)

虽然据我所知,以上仍然是正确的,但如果您的应用程序在 docker 环境中运行,您可以让这个代理网络处理您的所有证书 - 无论是在本地还是在实时环境中。我不隶属于该项目,但我已经愉快地使用它好几年了,从那以后就没有接触过 cron(为了这个任务)或 certbot-scripts。

它具有自动强制流量通过端口 443(如果启用它)的额外好处,因此您不必摆弄 apache 或 nginx 配置 - 为 Web 应用程序提供服务的容器只需要为端口 80 提供服务,代理会负责其余的部分。

  • 这不是真的——它*是*必须重新加载服务器,至少对于 Nginx——nginx 似乎缓存了初始证书并且即使文件更改也不会注册新证书。有关使用“--renew-hook”仅在成功续订后重新启动的信息,请参阅此帖子:https://www.guyrutenberg.com/2017/01/01/lets-encrypt-reload-nginx-after-renewing-证书/ (4认同)

Ham*_*ner 40

你不应该设置任何东西。任何最近的 Debian/Ubuntu 安装的 certbot 都应该安装一个 systemd 计时器和一个 cron 作业(并且 cron 作业仅certbot在 systemd 未处于活动状态时才会运行,因此您不会同时运行两者)。

系统定时器

您可以使用命令检查您的 systemd 计时器systemctl list-timers(或者systemctl list-timers --all如果您还想显示非活动计时器)。像这样的东西:

% sudo systemctl list-timers
NEXT                         LEFT        LAST                         PASSED      UNIT                         ACTIVATES
Fri 2018-08-03 06:17:25 UTC  10h left    Thu 2018-08-02 06:27:13 UTC  13h ago     apt-daily-upgrade.timer      apt-daily-upgrade.service
Fri 2018-08-03 11:43:29 UTC  15h left    Thu 2018-08-02 16:54:52 UTC  3h 7min ago certbot.timer                certbot.service
Fri 2018-08-03 12:44:58 UTC  16h left    Thu 2018-08-02 19:14:58 UTC  47min ago   apt-daily.timer              apt-daily.service
Fri 2018-08-03 19:43:44 UTC  23h left    Thu 2018-08-02 19:43:44 UTC  18min ago   systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2018-08-06 00:00:00 UTC  3 days left Mon 2018-07-30 00:00:09 UTC  3 days ago  fstrim.timer                 fstrim.service
Run Code Online (Sandbox Code Playgroud)

certbot 计时器应该在这里/lib/systemd/system/certbot.timer,它将执行中指定的命令/lib/systemd/system/certbot.service

certbot.timer 在最多 12 小时(43200 秒)的随机延迟后,将在上午 12 点和下午 12 点执行 `certbot.service。

# cat /lib/systemd/system/certbot.timer
[Unit]
Description=Run certbot twice daily

[Timer]
OnCalendar=*-*-* 00,12:00:00
RandomizedDelaySec=43200
Persistent=true

[Install]
WantedBy=timers.target
Run Code Online (Sandbox Code Playgroud)

并且certbot.service将执行renew命令。

# cat /lib/systemd/system/certbot.service
[Unit]
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://letsencrypt.readthedocs.io/en/latest/
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot -q renew
PrivateTmp=true
Run Code Online (Sandbox Code Playgroud)

定时任务

正如其他人所提到的,还安装了一个 cron 作业/etc/cron.d/certbot

# 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(43200))' && certbot -q renew
Run Code Online (Sandbox Code Playgroud)

这是在做:

  • test -x /usr/bin/certbot -a \! -d /run/systemd/system-检查,如果/usr/bin/certbot是可执行文件,并且/run/systemd/system不是一个目录。如果此检查成功,则仅继续下一位。
    • 检查的 systemd 部分实际上意味着如果 systemd 正在运行,则不要从 cron 作业运行 certbot - 将其留给计时器。
  • perl -e 'sleep int(rand(43200))' - 在 0 秒到 12 小时之间随机睡眠(43200 = 12 x 60 x 60)。
  • certbot -q renew检查您的证书并在需要时更新任何证书。该-q标志为“安静” -除非有错误不产生任何输出。

我最初对 cron 工作感到困惑,因为它不会因为 systemd 而运行,那么 certbot 将如何运行?我在这个论坛帖子中找到了答案,就是我的答案所依据的。

  • “你不应该设置任何东西”,但我的证书最近过期了,大约 3 个月前我安装了 certbot。`/etc/cron.d/certbot` 存在,`systemctl list-timers` 显示 `certbot.timer`,但我的证书没有更新。手动运行 `certbot` 工作正常,所以我不知道发生了什么。最终添加了一个老式的 `crontab` 条目。 (3认同)

sho*_*hok 5

对于 LetsEncrypt 证书更新,我通常使用getssl。它是一个非常方便的 shell 包装器,它甚至可以通过 SSH 连接在其他机器上安装证书。

cron 条目如下:

01 23 * * * root /root/scripts/getssl/getssl -u -a -q >>/var/log/getssl.log 2>&1 ; /usr/sbin/apache2ctl graceful

正如已经建议的那样,您应该每天运行它,或者更好,每天运行两次。


ttt*_*ttt 5

正如格劳克斯已经提到的:

注意:如果您正在设置 cron 或 systemd 作业,我们建议每天运行两次(在您的证书到期更新或吊销之前它不会执行任何操作,但定期运行它将使您的网站有机会保留下来)在线以防因某种原因发生 Let's Encrypt 发起的撤销)。请在一小时内随机选择一个分钟来执行您的续订任务。

来源:https://certbot.eff.org/all-instructions/#debian-8-jessie-apache

所以我最终使用了这个(每天跑步两次,每天 01:00 和 13:00):

6 1,13 * * * certbot renew --post-hook "service apache2 restart"
Run Code Online (Sandbox Code Playgroud)

甚至更好:

6 1,13 * * * certbot renew --renew-hook "service apache2 restart"
Run Code Online (Sandbox Code Playgroud)

我没有测试,但这也应该有效:

6 1,13 * * * certbot renew --post-hook "/etc/init.d/apache2 restart"
6 1,13 * * * certbot renew --renew-hook "/etc/init.d/apache2 restart"
Run Code Online (Sandbox Code Playgroud)

--pre-hook 和 --post-hook 挂钩在每次更新尝试之前和之后运行。如果您希望挂钩仅在成功续订后运行,请在类似这样的命令中使用 --renew-hook。

来源: https: //certbot.eff.org/docs/using.html


Shi*_*r G 5

其他成员已经提供了更详细的答案。但看起来我应该在这里提到它。

从 certbot 版本 0.21.1--renew-hook标志更改为--deploy-hook 确保您没有使用已弃用的标志。

certbot renew --deploy-hook "systemctl restart myservice"
Run Code Online (Sandbox Code Playgroud)


小智 5

由于 certbot 的首选安装方法已修改为snap,计时器的路径现在为/etc/systemd/system/snap.certbot.renew.timer

您还可以通过运行检查配置systemctl show certbot.timer