CertbotでLet's encryptのワイルドカード証明書を取得

Let's encryptのワイルドカード証明書を取得したときの覚書。

環境: FreeBSD 14.2-RELEASE-p3, certbot 2.9.0, nginx 1.26.3, Gemini 2.5 Pro


certbotを2.9.0から3.1.0にアップグレード

certbotのバージョン確認
#  certbot --version

certbot 2.9.0

certbotのGitHubページで確認すると最新が4.1.1だった。
pkgで検索。
# pkg search certbot

py311-certbot-3.1.0,1          Let's Encrypt client
py311-certbot-apache-3.1.0     Apache plugin for Certbot
py311-certbot-dns-cloudflare-3.1.0 Cloudflare DNS plugin for Certbot
py311-certbot-dns-cpanel-0.4.0_1 CPanel DNS Authenticator plugin for Certbot
py311-certbot-dns-digitalocean-3.1.0 DigitalOcean DNS Authenticator plugin for Certbot
py311-certbot-dns-dnsimple-3.1.0 DNSimple DNS Authenticator plugin for Certbot
py311-certbot-dns-dnsmadeeasy-3.1.0 DNS Made Easy DNS Authenticator plugin for Certbot
py311-certbot-dns-gandi-1.5.0_1 Gandi LiveDNS plugin for Certbot
py311-certbot-dns-gehirn-3.1.0 Gehirn Infrastructure Service DNS Authenticator plugin for Certbot
py311-certbot-dns-google-3.1.0 Google Cloud DNS Authenticator plugin for Certbot
py311-certbot-dns-linode-3.1.0 Linode DNS Authenticator plugin for Certbot
py311-certbot-dns-luadns-3.1.0 LuaDNS Authenticator plugin for Certbot
py311-certbot-dns-nsone-3.1.0  NS1 DNS Authenticator plugin for Certbot
py311-certbot-dns-ovh-3.1.0    OVH DNS Authenticator plugin for Certbot
py311-certbot-dns-powerdns-0.2.1_2 PowerDNS DNS Authenticator plugin for Certbot
py311-certbot-dns-rfc2136-3.1.0 RFC 2136 DNS Authenticator plugin for Certbot
py311-certbot-dns-route53-3.1.0 Route53 DNS Authenticator plugin for Certbot
py311-certbot-dns-sakuracloud-3.1.0 Sakura Cloud DNS Authenticator plugin for Certbot
py311-certbot-dns-standalone-1.1_1 Standalone DNS Authenticator plugin for Certbot
py311-certbot-nginx-3.1.0      NGINX plugin for Certbot

インストールされているcertbotを確認。
# pkg list | grep certbot

pkg upgradeを実行
# pkg update
# pkg upgrade

certbotは更新されなかったので手動で更新する。
今のcertbotコマンドの位置を確認。
# which certbot
# ls -l /usr/local/bin/certbot

lrwxr-xr-x  1 root wheel 11 May 30  2024 /usr/local/bin/certbot -> certbot-3.9

シンボリックリンクになっていたので、「py311-certbot」と共存できそう。
py311-certbotをインストールする。
# pkg install py311-certbot

In order to automatically renew the certificates, add this line to
/etc/periodic.conf:

    weekly_certbot_enable="YES"

インストールの途中でpy39-certbotのアンインストールが出てきたので実行。
自動更新の設定を確認。
# less /etc/periodic.conf

weekly_certbot_enable="YES"

バージョン確認
# certbot --version

certbot 3.1.0

今までの設定が残っているか確認。
# certbot renew

問題なし。


Let's Encryptのワイルドカード証明書を取得する仕組みを理解

Let's Encryptのワイルドカード証明書を取得するにはDNS-01チャレンジを使った認証方法にする必要がある。
参考: チャレンジの種類 - Let's Encrypt

定期的にDNSのTXTレコードを更新する必要があり、APIを提供しているDNSプロバイダ(ドメインレジストラ)のみ対応可能。

wwwだけのためにワイルドカード証明書を取得する必要はないと思う。


ドメインのネームサーバーをCloudflareに変更

DNSだけを利用するなら無料のCloudflareへネームサーバーを変更する。
(ドメイン移管はしない)
参考: 無料プランの概要 | Cloudflare


ちなみにドメインレジストラはvalue domeinを使っている。
Cloudflare ネームサーバーに置き換えるのに最大24時間かかる。
参考: Webサイト運営者必見!Cloudflareの使い方【無料プランの登録・設定方法を詳しく解説】 - 初心者向けホームページ&メールアドレスの作成ガイド|バリューノート

nslookupでNSレコードを確認してから「確認依頼」する。
PS > nslookup
> set type=NS
> server 8.8.8.8
> hoge.jp


CloudflareでAPIトークンを取得してCertbotでワイルドカード証明書の取得

以下はGemini 2.5 Proで教えてもらった手順。

CloudflareでAPIトークンを取得する。

  1. Cloudflareの右上のプロフィールアイコンから「プロフィール」を選択
  2. 左メニューの「APIトークン」タブを開き、「トークンを作成する」ボタンをクリック
  3. カスタムトークン」の「始める」を選択
    トークン名:certbot-dns
    権限: ゾーン、DNS、編集
    ゾーンリソース: 含む、特定のゾーン、hoge.jp
  4. 概要へ進む」→「トークンを作成する」をクリックして、トークンをメモ。

本番サーバー(FreeBSD)で作業
# cd /usr/local/etc/letsencrypt/
# vim cloudflare.ini

# Cloudflare API token used by Certbot
dns_cloudflare_api_token = 3N...Jpk5KWl

パーミッションを変更
# chmod 600 cloudflare.ini

certbotのcloudflare用プラグインをインストール。
# pkg install py311-certbot-dns-cloudflare

証明書取得
# certbot certonly --dns-cloudflare --dns-cloudflare-credentials /usr/local/etc/letsencrypt/cloudflare.ini -d "*.demo.hoge.jp" -d "demo.hoge.jp" --email suganuma@hoge.co.jp

▼ 説明

  • certonly: 証明書を取得するだけで、Webサーバーの設定は自動で行いません。(手動設定が確実です)
  • --dns-cloudflare: CloudflareのDNSプラグインを使用することを指定します。
  • --dns-cloudflare-credentials: 認証情報ファイルのパスを指定します。
  • -d "*.demo.hoge.jp": 取得したいワイルドカードドメイン。
  • -d "demo.hoge.jp": 取得したいベースドメイン。
  • --email your-email@example.com: Let's Encryptからの通知(有効期限切れなど)を受け取るメールアドレス。

もしエラーになったら、AIに聞けば解決方法を教えてくれる。


Nginxの設定変更

nginxのconf設定変更。
# cd /usr/local/etc/nginx/conf.d
# less hoge.conf

server {
    include conf.d/include/ssl_listen.inc;
    server_name  project.demo.hoge.jp;
    root /home/httpd/hoge-project/wordpress;

    # WordPress Setting
    include conf.d/include/wordpress_dev.inc;
    #include conf.d/include/wordpress_live.inc;

    ssl_certificate         /usr/local/etc/letsencrypt/live/demo.hoge.jp/fullchain.pem;
    ssl_certificate_key     /usr/local/etc/letsencrypt/live/demo.hoge.jp/privkey.pem;
    ssl_trusted_certificate /usr/local/etc/letsencrypt/live/demo.hoge.jp/chain.pem;
    include conf.d/include/ssl.inc;
}

testしてnginxの再読み込み。
# nginx -t
# service nginx reload

ブラウザでアクセスして確認する。


ERR_SSL_VERSION_OR_CIPHER_MISMATCHとSSL_ERROR_NO_CYPHER_OVERLAP

Google Chromeブラウザで表示したら下記エラー。

ERR_SSL_VERSION_OR_CIPHER_MISMATCH

Fireboxで表示したら下記エラー。

安全な接続ができませんでした

project.demo.hoge.jp への接続中にエラーが発生しました。Cannot communicate securely with peer: no common encryption algorithm(s).

エラーコード: SSL_ERROR_NO_CYPHER_OVERLAP


CloudflareのDNSレコードで「プロキシステータス」がオンになっていると、ユーザーのブラウザと配信サーバーの間にCloudflareのサーバーが入る。

ワイルドカード証明書を取得するDNSレコードは「プロキシステータス」がオフにする。

Gemini先生に聞いたら、CloudflareのSSL/TLS暗号化モードは「フル (厳密) 」が推奨らしいので、設定変更した。


▼ 関連記事