Let's Encrypt証明書の自動更新がされていなかった問題


Let's Encrypt証明書の自動更新がされていなかった問題

Let's Encrypt証明書を適用しているWebサイトを確認していたところ、Let's Encrypt証明書の有効期限が切れていることに気づいた。3ヶ月毎に有効期限を更新するように設定していたが、どうやら失敗していたらしい。

状況の確認

自動更新のcronを設定し忘れたのかと思い、cronの設定を確認した。

$ cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

0 0,12 * * * root python3 -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew -q

しっかりと設定は存在している。次に、設定したcronが実行されていないことを疑い、cronログを確認した。

$ cd /var/log
$ grep certbot-auto cron* | tail -n 3
cron-20200830:Aug 29 00:00:01 192-168-1-101 CROND[3724]: (root) CMD (python3 -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew -q)
cron-20200830:Aug 29 12:00:01 192-168-1-101 CROND[4517]: (root) CMD (python3 -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew -q)
cron-20200830:Aug 30 00:00:01 192-168-1-101 CROND[5229]: (root) CMD (python3 -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew -q)

ログを見る限りでは、しっかりと実行されている。つまり、更新処理自体に失敗しているようだ。

そこで、cronではなく手動で更新処理を実行してみた。

$  /usr/local/bin/certbot-auto renew -q


Attempting to renew cert (example.com) from /etc/letsencrypt/renewal/example.com.conf produced an unexpected error: Problem binding to port 80: Could not bind to IPv4 or IPv6.. Skipping.
All renewal attempts failed. The following certs could not be renewed:
  /etc/letsencrypt/live/example.com/fullchain.pem (failure)
1 renew failure(s), 0 parse failure(s)

80番ポートにバインドできないため、更新ができない的なエラーが出力された。どうやら、同サーバでNginxを動作させていたために、80番ポートが埋まってしまっていたことが問題のようだ。

暫定復旧作業

まず、Nginxを止める。

$ systemctl stop nginx
$ systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Sat 2020-09-05 10:54:36 JST; 6s ago
     Docs: http://nginx.org/en/docs/
  Process: 21568 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 19029 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 19032 (code=exited, status=0/SUCCESS)

 9月 05 10:40:08 192-168-1-101 systemd[1]: Starting nginx - high performance web server...
 9月 05 10:40:08 192-168-1-101 systemd[1]: Started nginx - high performance web server.
 9月 05 10:54:36 192-168-1-101 systemd[1]: Stopping nginx - high performance web server...
 9月 05 10:54:36 192-168-1-101 systemd[1]: Stopped nginx - high performance web server.

Let's Encrypt証明書を更新する。

$ /usr/local/bin/certbot-auto renew -q

先ほど止めた、Nginxを起動する。

$ systemctl start nginx

ひとまず、Let's Encrypt証明書の更新に成功した。

恒久復旧作業

前述の内容は暫定復旧であり、自動更新に関しては恒久対処に至っていない。
certbot-autoのポート番号を変更する等の何かしらの根本解決が必要だが、今回は暫定復旧のみとする。また、時間のある際に根本対処については調査する。