Let's EncryptのSSL証明書をDNS認証で発行してみる

どうも、やまもとやまです。
とりあえずサイトをSSL化しておきたい、というときに便利なLet's EncryptのSSL証明書。
Certbot(やその他のACMEクライアント)を実行すればすぐに設定が可能です。
ところで、Let's Encryptでも利用しているACMEでは、利用されることの多いファイル認証(HTTP-01チャレンジ)以外にDNS認証(DNS-01チャレンジ)も可能です。
Webサーバーにアクセス制限がかかっており、HTTPでの認証ができない場合でもDNS認証なら対応できますね。
というわけで、今回はDNS認証でのSSL証明書発行を試してみましょう。

環境

AlmaLinux9を最小インストールした環境です。
SSL証明書を発行する対象ドメインはssltest.example.comとします。

設定準備

では早速準備します。
何はともあれApacheをインストールしてやりましょう。

# dnf install httpd mod_ssl
# systemctl enable httpd --now

これでWebアクセスができますね。
ただし、ファイアウォール設定ではTCP80/443ともに閉じておきます。

次にCertbotを導入。

# dnf install epel-release
# dnf install certbot

こちらも問題なく完了しました。
では、証明書を発行してみます。

# certbot certonly --manual -d ssltest.example.com --preferred-challenges dns
----------------------------------------------------------------------
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): ※メールアドレス入力※

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.5-February-24-2025.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: n
Account registered.
Requesting a certificate for ssltest.example.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:

_acme-challenge.ssltest.example.com.

with the following value:

※発行されたTXTレコード用の文字列が表示されます※

Before continuing, verify the TXT record has been deployed. Depending on the DNS
provider, this may take some time, from a few seconds to multiple minutes. You can
check if it has finished deploying with aid of online tools, such as the Google
Admin Toolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.ssltest.example.com.
Look for one or more bolded line(s) below the line ';ANSWER'. It should show the
value(s) you've just added.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
----------------------------------------------------------------------

メールアドレスには受信可能なアドレスを登録しましょう。
TXTレコードが払い出されますので、DNSサーバーへ登録してからエンターを押します。

----------------------------------------------------------------------
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/ssltest.example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/ssltest.example.com/privkey.pem
This certificate expires on 2025-12-28.
These files will be updated when the certificate renews.

NEXT STEPS:
- This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an authentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same certbot command before the certificate's expiry date.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
----------------------------------------------------------------------

さくっと発行されました。やっぱり簡単便利。
証明書用の各ファイルは以下に保存されています。
※実際はそれぞれ実体へのシンボリックリンクなので、更新時には自動でリンクが修正されます

/etc/letsencrypt/live/ssltest.example.com/cert.pem
/etc/letsencrypt/live/ssltest.example.com/chain.pem
/etc/letsencrypt/live/ssltest.example.com/fullchain.pem
/etc/letsencrypt/live/ssltest.example.com/privkey.pem

証明書が発行されたので、Apache設定も調整します。

# vi /etc/httpd/conf.d/ssl.conf
SSLCertificateFile    /etc/letsencrypt/live/ssltest.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/ssltest.example.com/privkey.pem

# systemctl reload httpd.service

ブラウザアクセスやopensslコマンドで、SSL証明書が設定されていることを確認できました!

結果

Let's Encryptで、DNS認証でのSSL証明書発行ができました。
ただし、これだと手動での実行が必要で、自動更新もできず不便ですよね。
今回の記事では詳細は省きますが、それを解消するために、DNSレコードを登録や削除するスクリプトと組み合わせて自動化することも可能です。

それではまた!