今回は Cloudflare ( クラウドフレア ) で DNS-01 方式 ワイルドカード証明書を取得・自動更新 ( CRON JOB ) する方法を紹介します。クラウドフレアへアカウント登録および DNS ネームサーバの変更とdns-cloudflare プラグインをインストールが必要です。OS は CentOS7 です。
この記事は 2019 年当時の記録です(2026 年 7 月 追記)
本文は CentOS 7 / certbot 0.34.2 の環境で検証した当時の手順をそのまま残しています。基本的な考え方(DNS-01 チャレンジによるワイルドカード証明書の取得)は現在も変わりませんが、環境と推奨手順は大きく変わっています。現在これから構築する方は、以下の 4 点をご確認ください。
1. 認証情報は API トークンを使ってください(重要)
本文では
dns_cloudflare_emailとdns_cloudflare_api_key(グローバル API キー)を使っていますが、このキーはアカウント内の全ドメインに対する Cloudflare API 全体にアクセスできるため、漏洩時の被害が大きくなります。現在は、対象ゾーンと操作を限定できる API トークンが推奨方式です。
必要な権限は対象ゾーンのZone:DNS:Editのみです。認証情報ファイルは次の 1 行だけになります。# Cloudflare API token used by Certbot dns_cloudflare_api_token = 取得したトークントークンは Cloudflare ダッシュボードの API トークン画面から発行できます。詳細はプラグイン公式ドキュメントをご確認ください。
2. インストール方法が変わりました
本文の
yum install python2-certbot-dns-cloudflareは使えません。Python 2 のサポート終了に伴い、現在の Certbot は Python 3 専用です。公式は snap 経由でのインストールを案内しています。OS 別の正確な手順はCertbot 公式インストール手順でご自身の環境を選び、Wildcard タブをご確認ください。3. cron の設定は基本的に不要です
snap でインストールした場合、自動更新用の systemd タイマー(
snap.certbot.renew.timer)が同時に設定されるため、本文末尾の cron 登録は不要です。設定されているかは次のコマンドで確認できます。systemctl list-timers | grep certbotなお本文の cron 例にある
python -cは Python 2 を指すため、そのままでは動きません。cron で運用する場合はpython3に読み替えてください。4. CentOS 7 はサポートが終了しています
CentOS Linux 7 は 2024 年 6 月 30 日にサポートが終了しました(CentOS Linux 8 は 2021 年 12 月 31 日に終了)。現在サポートされている CentOS Linux はありません。これから新規に構築する場合は、AlmaLinux・Rocky Linux・RHEL・Ubuntu LTS など、サポート中のディストリビューションをご検討ください。詳細はRed Hat による解説をご参照ください。
※ 上記は公式ドキュメントに基づく変更点の整理であり、新しい環境での再検証は行っていません。実際に構築される際は必ず公式手順をご確認ください。
事前準備
- 事前準備1Cloudflare ( クラウドフレア ) 側
遷移されたページをそのまま開いておいてください。
- 事前準備2ドメイン登録機関側
仮に、example.com のドメイン登録は Freenom で行ったと前提します。Freenom にアクセスして先ほどメモった DNS ネームサーバに書き換えます。変更方法は こちら をご参考ください。
# 例: norm.ns.cloudflare.com (173.245.59.134) ulla.ns.cloudflare.com (173.245.58.233) # ping コマンドでIPアドレスを取得 [root@centos7 named]# ping norm.ns.cloudflare.com PING norm.ns.cloudflare.com (173.245.59.134) 56(84) bytes of data. 64 bytes from norm.ns.cloudflare.com (173.245.59.134): icmp_seq=1 ttl=58 time=7.42 ms [root@centos7 named]# ping ulla.ns.cloudflare.com PING ulla.ns.cloudflare.com (173.245.58.233) 56(84) bytes of data. 64 bytes from ulla.ns.cloudflare.com (173.245.58.233): icmp_seq=1 ttl=58 time=7.27 ms
- 事前準備3Cloudflare ( クラウドフレア ) 側
終わったら先ほどのクラウドフレア側の画面に戻って、Re-check now をクリックして連動を完了します。
ログイン後初期画面には、追加したサイトリストがあります。そこに example.com に Active 表示が出ていたら OK です。
サーバ環境 ( CentOS 7.6 / Apache 2.4.39 / BIND 9.9.4)
[root@centos7 ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@centos7 ~]# /usr/local/apache2/bin/httpd -v Server version: Apache/2.4.39 (Unix) Server built: May 26 2019 16:03:35 [root@centos7 ~]# named -v BIND 9.9.4-RedHat-9.9.4-74.el7_6.1 (Extended Support Version) [root@centos7 named]# certbot --version certbot 0.34.2 [root@centos7 named]#
Certbot と dns-cloudflare プラグインをインストール
(選択) 既に設置されたパッケージがあれば /etc/letsencrypt をバックアップし、全て削除してからインストールするのが楽です。
# 削除 [root@centos7 named]# mv /etc/letsencrypt /etc/letsencrypt-bak [root@centos7 named]# yum remove -y `yum list installed | cut -d " " -f 1 | grep certbot` # インストール [root@ centos7〜]#yum install epel-release [root@ centos7〜]#yum install certbot python2-certbot-dns-cloudflare # 確認 [root@centos7 named]# yum list installed | grep certbot certbot.noarch 0.34.2-3.el7 @epel python2-certbot.noarch 0.34.2-3.el7 @epel python2-certbot-dns-cloudflare.noarch 0.34.2-1.el7 @epel [root@centos7 named]#
Certbot 用 Cloudflare API credentials を作成
クラウドフレアのアカウントページ から Global API Key を取得できます。
[root@centos7 ~]# mkdir -p ~/.secrets/certbot/ [root@centos7 ~]# chmod 600 ~/.secrets/certbot/cloudflare.ini [root@centos7 ~]# vi ~/.secrets/certbot/cloudflare.ini # Cloudflare API credentials used by Certbot dns_cloudflare_email = admin@example.com dns_cloudflare_api_key = 8345692e953e123456de118bfe478054b8b8a [root@centos7 ~]#
ワイルド証明書の発行テスト
ここからは、
tail -f /var/log/message 又は、journalctl -fu named でエラーがないことを確認しながら進みましょう。–dry-run と –debug オプションを追加して実行します。
certbot certonly \ --dry-run \ --debug \ --dns-cloudflare \ --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \ --dns-cloudflare-propagation-seconds 5 \ -d example.com \ -d "*.example.com"
実行すると、The dry run was successful. と、表示されれば OK です。
[root@centos7 named]# certbot certonly --dry-run --debug --dns-cloudflare --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini --dns-cloudflare-propagation-seconds 5 -d example.com -d "*.example.com" --server https://acme-v02.api.letsencrypt.org/directory Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator dns-cloudflare, Installer None Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org Obtaining a new certificate Performing the following challenges: dns-01 challenge for example.com dns-01 challenge for example.com Starting new HTTPS connection (1): api.cloudflare.com Starting new HTTPS connection (1): api.cloudflare.com Waiting 5 seconds for DNS changes to propagate Waiting for verification... Cleaning up challenges Starting new HTTPS connection (1): api.cloudflare.com Starting new HTTPS connection (1): api.cloudflare.com IMPORTANT NOTES: - The dry run was successful. [root@centos7 named]#
発行テストが通れたら Certbot アカウント を登録して 実際のワイルドカード証明書を発行 します。
Certbot アカウントを登録 ( 選択 )
登録済みであれば、スキップしてください。
# メールアドレスをシェアしないで登録する certbot register --email admin@example.org --agree-tos --no-eff-email
ワイルドカード証明書を発行
–dns-cloudflare-propagation-seconds オプションはデフォルトで 60秒、長いと思ったら調整します。
certbot certonly \
–dns-cloudflare \
–dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \
–dns-cloudflare-propagation-seconds 5 \
-d example.com \
-d “*.example.com”
[root@centos7 named]# certbot certonly --dns-cloudflare --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini --dns-cloudflare-propagation-seconds 5 -d example.com -d "*.example.com" Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator dns-cloudflare, Installer None Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org Obtaining a new certificate Performing the following challenges: dns-01 challenge for example.com dns-01 challenge for example.com Waiting 5 seconds for DNS changes to propagate Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your cert will expire on 2019-09-15. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - 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 [root@centos7 named]#
/etc/letsencrypt/live/ドメイン名 以下に保存されます。
[root@centos7 named]# ls /etc/letsencrypt/live/example.com/ cert.pem chain.pem fullchain.pem privkey.pem README [root@centos7 named]#
証明書の更新テスト
Congratulations, all renewals succeeded. と、表示されたら OK です。
[root@centos7 named]# certbot renew --dry-run Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/example.com.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Cert not due for renewal, but simulating renewal for dry run Plugins selected: Authenticator dns-cloudflare, Installer None Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org Renewing an existing certificate - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - new certificate deployed without reload, fullchain is /etc/letsencrypt/live/example.com/fullchain.pem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates below have not been saved.) Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/example.com/fullchain.pem (success) ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates above have not been saved.) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - [root@centos7 named]#
自動更新設定 ( CRON JOBへ登録 )
自動更新をテストしてエラーがなければ CRON JOB へ登録します。
毎日2回(正午、真夜中)更新するように設定
[root@centos7 named]# crontab -e #下記の内容を追加 0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 60)' && certbot renew [root@centos7 named]#
これから https の設定をするなら、こちら をご参考ください。
デバッグ
All authorizations were not finalized by the CA.
LISTENポートを確認する
[root@centos7 named]# netstat -pant | grep -E ':80|:443' tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11501/httpd tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 8998/java tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 11501/httpd tcp 0 0 123.123.123.10:443 192.0.102.40:44695 TIME_WAIT -
参考文献
Certbot: Apache on CentOS/RHEL 7
Certbot-dns-cloudflare’s documentation
How does Cloudflare work?


コメント