まず、certbot-renew のステータスを確認します。
sudo systemctl status -l certbot-renew certbot[14804]: "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML certbot[14804]: 2.0//EN\">\n<html><head>\n<title>401 certbot[14804]: Unauthorized</title>\n</head><body>\n<h1>Unauthorized</" certbot[14804]: To fix these errors, please make sure that your domain name was certbot[14804]: entered correctly and the DNS A/AAAA record(s) for that domain certbot[14804]: contain(s) the right IP address. systemd[1]: certbot-renew.service: main process exited, code=exited, status=1/FAILURE systemd[1]: Failed to start This service automatically renews any certbot certificates found. systemd[1]: Unit certbot-renew.service entered failed state. systemd[1]: certbot-renew.service failed.
すると「401 Unauthorized」で、認証のエラーが出ていることがわかります。
Let's Encrypt の自動更新ではドキュメントルートに「.well-known/acme-challenge」という2階層のディレクトリを作成し、その下にファイルが作成されます。
しかし、Basic 認証がかかっていると、そのファイルにアクセスできず、自動更新に失敗してしまいます。
そこで、conf ファイルを編集し「.well-known」以下は Basic 認証の対象外に設定します。
# 設定ファイルを確認
ls -l /etc/httpd/conf.d
# 対象のファイルを修正
sudoedit /etc/httpd/conf.d/vhost-ssl.conf
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html/example.com
~ 省略 ~
<Directory "/var/www/html/example.com">
Options FollowSymLinks
AllowOverride All
</Directory>
# ↓↓↓ 以下の3行を追加 ↓↓↓
<Directory "/var/www/html/example.com/.well-known">
Require all granted
</Directory>
# ↑↑↑
~ 省略 ~
</VirtualHost>
設定後、apache を再起動します。
# 再起動 sudo apachectl graceful
試しに、正常に動作するか、ディレクトリとファイルを作ってみます。
# 再帰的にディレクトリ作成 mkdir -p /var/www/html/example.com/.well-known/acme-challenge # テスト用ファイル作成 vi /var/www/html/example.com/.well-known/acme-challenge/test.html
ブラウザで Basic認証なしでアクセスできるか確認します。
このとき、すでに Basic認証済みのブラウザでアクセスすると認証が発生せず意味がないので、シークレットモードなどを使います。
https://example.com/.well-known/acme-challenge/test.html
認証なしでアクセスに成功したら、Let's Encrypt の自動更新も成功するはずです。
動作確認後は、作成したディレクトリとファイルは削除しておいてください。