【问题标题】:LetsEncrypt certbot multiple renew-hooksLetsEncrypt certbot 多个更新挂钩
【发布时间】:2017-07-07 03:16:01
【问题描述】:

我正在从 LetsEncrypt 的 certbot 自动更新 SSL 证书。实际续订工作正常,但我需要自动重新启动服务,以便它们加载续订的证书。我想知道您是否可以在 cronjob 中为 letsencrypt renew 使用多个 --renew-hook 参数?

如何在证书更新时自动重启服务?

【问题讨论】:

    标签: lets-encrypt


    【解决方案1】:

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

    如果您将任何脚本放入“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
    

    【讨论】:

      【解决方案2】:

      是的,您可以使用多个 --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 正在运行。

      【讨论】:

      • 看起来 Ubuntu 16.04 的默认包管理器带有不接受 --renew-hook 参数的旧版 Certbot。
      • 是的,repo 有一个非常旧的 Let's Encrypt 版本,无论如何去年它已重命名为 certbot。您可以简单地卸载 repo 版本,然后简单地 mkdir /opt/certbot 然后 cd /opt/certbot 然后 sudo wget https://dl.eff.org/certbot-auto 然后从那里运行您的 certbot-auto 命令行。如果没记错的话,Ubuntu repo 中的版本是 let's encrypt 的 v0.4。 Certbot 现在是 0.11。
      • @MitchellK - 与其每天收到一封 cron 电子邮件,不如使用像 IsItWorking.info(我的)这样的 cron 报告服务。您的 cron 脚本可以在每次成功运行时签入。如果它没有签入,则服务会提醒您。 (可能除了一封电子邮件)
      • 你真的可以使用多个更新钩子命令吗?我不这么认为,有an issue
      • @W.M.对,那是正确的。当你在没有--force-renew 选项的命令行上运行它时,你会在输出的末尾看到No renewals were attempted. No hooks were run.
      【解决方案3】:

      您还可以在文件/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 文件夹中创建证书特定版本。

      【讨论】:

      • 是的,可以,但首选另一个答案中的--renew-hook 建议。当您使用--renew-hook 运行命令时,它会将钩子信息添加到 /etc/letsencrypt/renewal 中的域中,因此钩子每次都会自动运行。这是一个首选的解决方案,而不是直接编辑 /etc/letsencrypt/cli.ini。
      【解决方案4】:

      根据我在 Ubuntu 16.04 的 CertBot 的全新安装中看到的,它创建了一个 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/

      【讨论】:

      • 他们实际上更改了 certbot cronjob 以不再包含那些运行部分参数。现在推荐的方法是创建一个/etc/letsencrypt/cli.ini 并在其中指定挂钩参数,请参阅github.com/certbot/certbot/issues/1706#issuecomment-302774426
      • 是的,可以在 cli.ini 中,也可以在每个 /etc/letsencrypt/renewal/*.conf 文件中的 [renewalparams] 区域中(如果您不想要全局的话)。我混合了 route53 和独立的,所以我更喜欢编辑每个更新配置文件。您不需要在每个更新区域中都有 cli.ini。只需编辑更新文件。
      【解决方案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"
      

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

      【讨论】:

      • 或者,如果您不想要全局 ini,则在每个 /etc/letsencrypt/renewal/*.conf 文件的 [renewalparams] 区域中。
      【解决方案6】:

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

      1) 系统定时器

      运行命令: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
      

      这是符号链接

      /lib/systemd/system/certbot.timer
      

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

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

      【讨论】:

        猜你喜欢
        • 2017-09-05
        • 2017-02-23
        • 2018-08-05
        • 2018-09-13
        • 2017-02-21
        • 2016-11-30
        • 2019-05-29
        • 2019-09-04
        • 2016-10-25
        相关资源
        最近更新 更多