AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 790772
Accepted
user3448600
user3448600
Asked: 2016-07-20 11:07:32 +0800 CST2016-07-20 11:07:32 +0800 CST 2016-07-20 11:07:32 +0800 CST

让我们加密更新的 Cron 作业

  • 772

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

@monthly letsencrypt renew && service apache2 reload
cron lets-encrypt
  • 12 12 个回答
  • 281987 Views

12 个回答

  • Voted
  1. Best Answer
    Michael Hampton
    2016-07-20T11:33:34+08:002016-07-20T11:33:34+08:00

    每月的频率不够。

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

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

    除了这些问题,它和我的 cron 工作差不多。

    43 6 * * * certbot renew --post-hook "systemctl reload nginx"
    

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

    • 211
  2. ishigoya
    2017-10-23T07:34:38+08:002017-10-23T07:34:38+08:00

    我最近(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
    

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

    • 77
  3. glaux
    2017-01-10T01:07:27+08:002017-01-10T01:07:27+08:00

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

    笔记:

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

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

    52 0,12 * * * root /full/path/to/certbot-auto renew --quiet
    

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

    52 0,12 * * * root /home/git/certbot-auto renew --quiet
    

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

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

    请务必事先在 shell 中测试运行 renew 命令以测试路径,如果证书未到期更新,则不会发生任何事情(在没有--quiet标志的情况下运行此测试以查看发生了什么)。

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

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

    52 0,12 * * * root certbot renew --renew-hook 'service nginx reload'
    

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

    尽管据我所知,上述内容仍然正确,但如果您的应用程序在 docker 环境中运行,您可以让这个代理网络处理您的所有证书——无论是在本地还是在实时环境中。我不隶属于该项目,但几年来我一直在愉快地使用它,并且从那时起就没有接触过 cron(用于此任务)或 certbot-scripts。

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

    • 56
  4. Hamish Downer
    2018-08-03T12:14:38+08:002018-08-03T12:14:38+08:00

    你不应该设置任何东西。任何最近的 Debian/Ubuntu 安装的 certbot 都应该安装一个 systemd 计时器和一个 cron 作业(并且 cron 作业只有在 systemd 不活动时才会运行certbot,所以你不会同时运行)。

    系统定时器

    您可以使用命令检查您的 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
    

    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
    

    并将certbot.service执行更新命令。

    # 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
    

    定时任务

    正如其他人所提到的,还有一个 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
    

    这是在做:

    • 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 将如何运行?我在这个论坛帖子中找到了答案,这是我这个答案的依据。

    • 50
  5. Shinebayar G
    2019-07-24T08:35:39+08:002019-07-24T08:35:39+08:00

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

    从 certbot 版本 0.21.1 开始,--renew-hook标志更改为--deploy-hook 确保您没有使用不推荐使用的标志。

    certbot renew --deploy-hook "systemctl restart myservice"
    
    • 7
  6. shodanshok
    2017-01-10T01:46:17+08:002017-01-10T01:46:17+08:00

    对于 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

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

    • 5
  7. Tadej
    2017-07-06T01:49:06+08:002017-07-06T01:49:06+08:00

    正如 glaux 已经提到的:

    注意:如果您正在设置 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"
    

    甚至更好:

    6 1,13 * * * certbot renew --renew-hook "service apache2 restart"
    

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

    6 1,13 * * * certbot renew --post-hook "/etc/init.d/apache2 restart"
    6 1,13 * * * certbot renew --renew-hook "/etc/init.d/apache2 restart"
    

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

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

    • 5
  8. Rmatt
    2021-06-12T07:07:17+08:002021-06-12T07:07:17+08:00

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

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

    • 3
  9. Pavel Niedoba
    2016-10-11T03:50:46+08:002016-10-11T03:50:46+08:00

    这就是我使用的:

    /opt/letsencrypt/letsencrypt-auto renew
    

    输出为:

    Upgrading certbot-auto 0.8.1 to 0.9.1...
    Replacing certbot-auto...
    Creating virtual environment...
    ...
    new certificate deployed with reload of apache server; fullchain is
    /etc/letsencrypt/live/host.simplecoin.cz/fullchain.pem
    -------------------------------------------------------------------------------
    
    Congratulations, all renewals succeeded. The following certs have been renewed:
      /etc/letsencrypt/live/host.simplecoin.cz/fullchain.pem (success)
    

    并且它说apache已经重新启动,所以不需要再做一次。如果我再次运行它:

    Cert not yet due for renewal
    

    因此每天更新证书不是问题,我的 cron 是:

    @daily /opt/letsencrypt/cronautorenew.sh
    

    我使用脚本将日志记录调整到单独的文件,所以这是我的 cronautorenew.sh:

    #!/usr/bin/env bash
    printf "\nattempt to renew certificates" >>/var/log/letsencrypt_cron.log 2>&1
    date >>/var/log/letsencrypt_cron.log 2>&1
    /opt/letsencrypt/letsencrypt-auto renew >>/var/log/letsencrypt_cron.log 2>&1
    printf "renew finished\n" >>/var/log/letsencrypt_cron.log 2>&1
    
    • 2
  10. Grant_Bailey
    2021-03-31T04:41:10+08:002021-03-31T04:41:10+08:00

    如果您的 Certbot 证书是使用standalone插件获得的,那么您可能需要停止 Web 服务器才能使续订成功。这可以使用--pre-hook和--post-hook运算符在 cron 中完成:Certbot 文档。

    • 2

相关问题

  • 每 4 小时运行一次 cron 的正确语法是什么?[复制]

  • 用于 mysql 数据库清理和备份的 crontab

  • Windows cron 作业

  • 使用 crontab 和 /etc/cron.hourly,daily,weekly 的区别

  • crontab ifconfig 什么都不输出

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve