🏷️

Let's Encrypt のワイルドカード証明書を自動更新したい

に公開

Let's Encrypt でワイルドカード SSL 証明書を更新する場合、DNS の TXT レコードを利用した ACME チャレンジが必要になります。
自前で DNS サーバを運用している場合は自動化が簡単にできますが、この記事では外部の DNS サービスを利用している前提で、certbot-dns-standaloneの利用例を示します。
(大手の DNS サービスで専用ツール certbot-dns-route53certbot-dns-sakuracloud などがある場合は、それらを使用する方がシンプルだと思います。)

テスト条件

  • SSL 証明書を使うサーバ:Amazon Lightsail
  • DNS サーバ:Amazon Lightsail (Amazon Route 53 ではない)
  • OS:Debian 12.2.0 (Linux version 6.1.0-33-cloud-amd64)
  • 初回の証明書発行は済んでいる

手順

以降、ドメインは domain.tld、IPアドレスは x.x.x.x としています。

  1. Standalone DNS Authenticator plugin for Certbot をインストールします。
shell
# apt install python3-certbot-dns-standalone
  1. DNS を設定します。
type name route
CNAME _acme-challenge acme.domain.tld
NS acme.domain.tld ns.acme.domain.tld
A ns.acme.domain.tld x.x.x.x
  1. ファイアウォールのポート UDP 53 を開放します。

  2. 証明書更新のテスト (--dry-run) をします。

shell
# certbot --non-interactive --agree-tos certonly --authenticator dns-standalone --dns-standalone-address=x.x.x.x -d domain.tld -d '*.domain.tld' --dry-run
  1. 問題がなければ自動実行させます。
    --deplay-hook で証明書が更新された時に実行するコマンドを指定できます。
shell
# crontab -e
+ 0 4 * * * certbot --non-interactive --agree-tos certonly --authenticator dns-standalone --dns-standalone-address=x.x.x.x -d domain.tld -d '*.domain.tld' --deploy-hook "systemctl reload apache2 postfix dovecot"

証明書の有効期限30日前の朝には無事に証明書が自動更新されていることを確認できました。

Discussion