Let's Encrypt の自動更新


Let's Encrypt の自動更新の設定方法です。

1) クローンが動いているかの確認

Ubuntu

$ sudo systemctl status cron
● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: >
     Active: active (running) since Fri 2020-07-24 07:16:12 JST; 4 weeks 0 days>
       Docs: man:cron(8)
   Main PID: 416 (cron)

Arch Linux

$ sudo systemctl status cronie
● cronie.service - Periodic Command Scheduler
     Loaded: loaded (/usr/lib/systemd/system/cronie.service; enabled; vendor pr>
     Active: active (running) since Thu 2020-08-20 18:38:41 JST; 21h ago
   Main PID: 31026 (crond)

2) 簡単なクローンを設定してみる

クローンの設定

crontab -e

クローンの確認

$ crontab -l
#
14 * * * * date > /tmp/t208.txt
#
15 16 * * * date > /tmp/t209.txt
#
16 16 21 * * date > /tmp/t210.txt

上記の意味は、
毎時 14 分に実行
毎日 16時15分に実行
毎月 21日16時16分に実行

3) ドライランでコマンドを走らせてみる

$ sudo certbot renew --dry-run

4) ドライランをクローンに設定する

sudo crontab -e
$ sudo crontab -l
#
PATH=/usr/sbin:/usr/bin:/bin
#
45 18 20 * * /usr/bin/certbot renew --dry run >& /tmp/test_dry.log
#

クローンが動くことを確認

5) 更新コマンドをクローンに設定

sudo crontab -e
$ sudo crontab -l
#
PATH=/usr/sbin:/usr/bin:/bin
#
45 19 20 * * /usr/bin/certbot renew --post-hook "systemctl restart nginx" >& /tmp/certbot.log

上記は、月に1回ですが、同じコマンドを3つ程書いて、月に3回程動かす方が良い。
たまたま,ネットワークなりが不調の時に実行されると、更新がされないので。

PATH=/usr/sbin:/usr/bin:/bin を入れないと次のようなエラーが出ます。

Could not choose appropriate plugin: The nginx plugin is not working; there may 
be problems with your existing configuration.
The error was: NoInstallationError("Could not find a usable 'nginx' binary. Ensu
re nginx exists, the binary is executable, and your PATH is set correctly.",)