Let’s Encryptで更新エラーその2(webrootでドキュメントルート指定)

Let’s Encryptの更新がまた失敗している。
以下の記事で更新が失敗してからの対応策は実施していたが、、、

Let’s Encryptで更新エラー - myMemoBlog

Let’s Encryptからのドメイン有効期限切れリマインド

Let’s Encryptからドメイン有効期限が20日を切るとリマインドメールが来る。
SSL証明書の更新はcronで実施していて、3つあるドメインのうち1つが成功して、残り2つが失敗という状況だった。

Let’s Encryptからのメール

原因調査

/var/log/cron.log で前回のcron実行日時を確認し、実行自体はされていた。
cronの結果をログ出力しない(/dev/null)としていたため、手動で実行する。

cronの実行コマンド

これがうまくいっていなかった。
# certbot-auto renew --post-hook "systemctl restart nginx" > /dev/null 2>&1

実行結果(エラー内容)

コマンドを直接打ってみる。

# certbot-auto renew --post-hook "systemctl restart nginx"
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/tanebox.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal

すでに更新済みのため、更新可能期限になっていない、とのアラート。これはすでにcronで更新が成功していたドメイン。

問題は以降の2つのドメイン。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/blog.tanebox.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert is due for renewal, auto-renewing...
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for blog.tanebox.com
Waiting for verification...
Cleaning up challenges
Attempting to renew cert (blog.tanebox.com) from /etc/letsencrypt/renewal/blog.tanebox.com.conf produced an unexpected error: Failed authorization procedure. blog.tanebox.com (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://blog.tanebox.com/.well-known/acme-challenge/s1Fzcm7dh3m8EfWGzGmFW6Xxb_zwXPR1Jorg-98ZJVc: "<html>\r\n<head><title>404 Not Found</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>404 Not Found</h1></center>\r\n<hr><center>". Skipping.

証明書の更新期限になっているものの、ドメイン保有者かどうか確認するための「.well-known」以下に設置する認証ファイルが見つからなかった、との結果。

Invalid response from http://blog.tanebox.com/.well-known/acme-challenge/sDFzcm7dh3m8EfWGzGmFW6Xxb_zWXPR1JOrg-Y8ZJVc: "<html>\r\n<head><title>404 Not Found

もう1つのドメインも同様のエラー。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/keyboards.tanebox.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert is due for renewal, auto-renewing...
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for keyboards.tanebox.com
Waiting for verification...
Cleaning up challenges
Attempting to renew cert (keyboards.tanebox.com) from /etc/letsencrypt/renewal/keyboards.tanebox.com.conf produced an unexpected error: Failed authorization procedure. keyboards.tanebox.com (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://keyboards.tanebox.com/.well-known/acme-challenge/QkfD6gg_BpGlE-5NTa07F119yv-f9pHFulj-yPtaeK0: "<html>\r\n<head><title>404 Not Found</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>404 Not Found</h1></center>\r\n<hr><center>". Skipping.
All renewal attempts failed. The following certs could not be renewed:
  /etc/letsencrypt/live/blog.tanebox.com/fullchain.pem (failure)
  /etc/letsencrypt/live/keyboards.tanebox.com/fullchain.pem (failure)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/tanebox.com/fullchain.pem expires on 2019-02-22 (skipped)
All renewal attempts failed. The following certs could not be renewed:
  /etc/letsencrypt/live/blog.tanebox.com/fullchain.pem (failure)
  /etc/letsencrypt/live/keyboards.tanebox.com/fullchain.pem (failure)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Running post-hook command: systemctl restart nginx
2 renew failure(s), 0 parse failure(s)

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: blog.tanebox.com
   Type:   unauthorized
   Detail: Invalid response from
   http://blog.tanebox.com/.well-known/acme-challenge/s1Fzcm7dh3m8EfWGzGmFW6Xxb_zwXPR1Jorg-98ZJVc:
   "<html>\r\n<head><title>404 Not Found</title></head>\r\n<body
   bgcolor=\"white\">\r\n<center><h1>404 Not
   Found</h1></center>\r\n<hr><center>"

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address.
 - The following errors were reported by the server:

   Domain: keyboards.tanebox.com
   Type:   unauthorized
   Detail: Invalid response from
   http://keyboards.tanebox.com/.well-known/acme-challenge/QkfD6gg_BpGlE-5NTa07F119yv-f9pHFulj-yPtaeK0:
   "<html>\r\n<head><title>404 Not Found</title></head>\r\n<body
   bgcolor=\"white\">\r\n<center><h1>404 Not
   Found</h1></center>\r\n<hr><center>"

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address.

調べてみると、
- DNSのA/AAAAレコードのIPアドレスが正しいか
- 「.well-known」へのアクセスが正常にできるか
それらを確認してほしいとのこと。

対応策

WordPressとLet's Encryptの組み合わせによる対応方法は、いろいろな情報があるが、以下がわかりやすかった。

WordPressのサイトをHTTPS化して学ぶLet’s Encryptの使い方
- OXYNOTES

自分の場合は、上記の設定は対応されていたが、以下のドキュメントルートまでのパスを指定していなかった。

# certbot-auto certonly --webroot -w [ドキュメントルートのパス]

対応策後のコマンドと結果

certonlyにして、webrootオプションを指定して実行。

# certbot-auto certonly --webroot -w /home/kusanagi/blog/DocumentRoot -d blog.tanebox.com --post-hook "systemctl restart nginx"

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Cert is due for renewal, auto-renewing...
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for blog.tanebox.com
Using the webroot path /home/kusanagi/blog/DocumentRoot for all unmatched domains.
Waiting for verification...
Cleaning up challenges
Running post-hook command: systemctl restart nginx

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:

▽
   /etc/letsencrypt/live/blog.tanebox.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/blog.tanebox.com/privkey.pem
   Your cert will expire on 2019-03-01. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

更新が成功した。

事前に、「.well-known」以下にテスト用ファイルを設置して、ファイルがURLからアクセスできるか確認もした。WordPressで静的ファイルをそのままドキュメントルートに設置したら表示できるって知らなかった(Nginxなどで要設定)。

同様にもう1つのドメインも更新した。

再度更新を実行してみる

念のため、もう1回実行してみると、、、

# certbot-auto certonly --webroot -w /home/kusanagi/blog/DocumentRoot -d blog.tanebox.com --post-hook "systemctl restart nginx"
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Cert not yet due for renewal

You have an existing certificate that has exactly the same domains or certificate name you requested and isn't close to expiry.
(ref: /etc/letsencrypt/renewal/blog.tanebox.com.conf)

What would you like to do?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Keep the existing certificate for now
2: Renew & replace the cert (limit ~5 per 7 days)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 

SSL証明書の有効期限はきていないが、「1: 更新しない」「2: 強制的に更新」の2択を対話型で聞かれる。

cronの設定

ひとまず、成功したコマンドでcronに設定したが、有効期限が来ていないと対話型で聞かれてしまうため、ここはまた動かしてみて調査予定。

Let’s Encryptの新規登録はかんたんだが、更新が予想以上に難しい。
また、以下のようにアーキテクチャーのレイヤー(つまり環境)によって設定も異なるため、自分の中で整理しておかないと混乱する。

【ネットワーク】
--------------
DNS
--------------

【サーバー】
--------------
アプリケーション ← WordPress、Railsなど
--------------
ミドルウェア ← Apache、Apache+Nginxなど
--------------
OS ← CentOS、Amazon Linuxなど
--------------