certbot(LetsEncrypt)でエラー/Invalid response from


エラー症状

LetsEncryptの証明書更新でエラーが発生。

エラー内容を確認すると認証用ページにリクエストとしても応答がないと。
動作しているWebアプリケーションには動的にページを追加することはできないので失敗して当然。

Domain: mydomain.com
Type:   unauthorized
Detail: Invalid response from
http://mydomain.com/.well-known/acme-challenge/rwH-dBrZhXrgii7uiGmccp8GFMOdv1RRRHrBSVfkWuU
[2606:2800:220:1:248:1893:25c8:1946]: "...

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.

LetsEncryptの当該ドメインの設定ファイル(/etc/letsencrypt/renewal/<ドメイン名>.conf)を確認したところ
認証方法がwebrootでディレクトリがvar/www/xxxになっていた。たぶん、Webアプリ導入前に証明書を取得したのだと思う。

環境

  • Amazon Linux 2
  • Certbot 1.9.0
  • Apache 2.4
  • 複数のバーチャルホスト
  • それぞれがリバースプロキシを利用
  • リバース先のWebアプリにはアプリ外から動的にページを配置することはできない

対策

--webrootでなく--apacheオプションを付与して証明書を再取得するだけ。これだけでOKだった。

# certbot certonly --apache -d mydomain.com -d mydomain2.com

備考/更新時の指定

更新(certbot renew)する際は--apache オプションの指定は不要。

なぜ省略できるかといえば設定ファイルにapacheを使うことが明示されているから。
この指定をすると更新パラメタ(renewalparams)で、認証処理・インストール処理がapacheに設定されている。

/etc/letsencrypt/renewal/.conf

証明書や鍵ファイルのパス

[renewalparams]
authenticator = apache
account = 68964f20a9151fxxxxxxxxxxxxxxxxxxxxx
server = https://acme-v02.api.letsencrypt.org/directory
installer = apache

備考/バーチャルホストが1つしかない場合はwebrootでいける模様

今回は複数のサーバで発生していたのですが、バーチャルホストを1つしか使っていない場合は、
Apache Webサーバ設定で、Aliasを使って/.well-known/acme-challengeをファイルシステムに実在するディレクトリを紐付ければいけるようです。
(このディレクトリはアプリでは使ってない)

httpd.conf
Alias /.well-known/acme-challenge /var/www/letsencrypt
<Directory /var/www/letsencrypt>
  Order allow,deny
  Allow from all
</Directory>

certbotコマンド

# certbot certonly --webroot -w /var/www/letsencrypt -d ドメイン