LoginSignup
16
20

More than 5 years have passed since last update.

certbot+certbot-dns-cloudflare でワイルドカード証明書を取得&自動更新する

Last updated at Posted at 2018-03-31

1. certbot をインストール

CentOS 7 なら epel にワイルドカード証明書対応版 (v0.22) があるので、それをインストールする。

yum install --enablerepo=epel certbot

2. certbot-dns-cloudflare プラグインをインストール

ワイルドカード証明書の発行、更新には DNS の TXT レコードを編集する必要がある。
DNS 操作を自動化するため、 certbot-dns-cloudflare プラグインを導入する。

yum install --enablerepo=epel python2-certbot-dns-cloudflare

Global API Keyhttps://www.cloudflare.com/a/profile から取得し、それを記した credentials ファイルを作成する。
ここでは /root/.cloudflare/credentials に作成するものとする。

mkdir /root/.cloudflare
chmod 700 /root/.cloudflare
echo "dns_cloudflare_email = {ログインメールアドレス}" > /root/.cloudflare/credentials
echo "dns_cloudflare_api_key = {取得した API キー}" >> /root/.cloudflare/credentials
chmod 600 /root/.cloudflare/credentials

3. 証明書を取得する

example.com*.example.com の証明書を取得する場合、以下のようなコマンドになる。

certbot certonly \
  --dns-cloudflare \
  --dns-cloudflare-credentials /root/.cloudflare/credentials \
  --dns-cloudflare-propagation-seconds 60 \
  --server https://acme-v02.api.letsencrypt.org/directory \
  -d example.com \
  -d *.example.com

DNS の変更が反映されるのを待つため、 60 秒 (--dns-cloudflare-propagation-seconds) ほど掛かる。

取得が完了すると、 /etc/letsencrypt/live/example.com 以下に秘密鍵と証明書が出力されている。

4. 証明書を更新する

certbot renew

/etc/letsencrypt/renewal/{ドメイン名}.conf に取得時の設定が残されているため、更新時にオプションを指定する必要はない。

付記. cron 設定例

echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew" > /etc/cron.d/certbot
16
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
20