らっちゃいブログ

日々の学びと気づきを発信するブログ

Let’s Encryptで発行したSSL証明書を自動更新するときのノウハウ

スポンサーリンク

racchai.hatenablog.com

前回の記事では証明書の自動更新についてはさらった触れただけでしたが、もっと情報がほしいという声を受けて少し補足しようと思います。

ドメイン毎に証明書を更新するにはどうしたらいいの?

前回のブログでは以下のコマンドで更新する方法を紹介しました。

$ ./letsencrypt-auto renew

ですが、このコマンドは常に全ドメインが対象となるため、別々に更新したいケースには対応できません。

個別に更新したいときはどうするかと言いますと、新規発行したときと同じオプションでコマンドを実行すればよいそうです。

つまり、racchai.com だけの証明書を更新したい場合は以下のコマンドをもう一度実行することになります。

$ ./letsencrypt-auto certonly --webroot -w /var/www/html/ -d racchai.com

しかし、上記を実行してみると確認のダイアログが出るので自動実行するには都合が悪いことがわかります。

これを避けるため、--non-interactive オプションを付与しましょう。

$ ./letsencrypt-auto certonly --webroot -w /var/www/html/ -d racchai.com --non-interactive

これで自動実行が可能になりました。

上記のコマンドであっても renew と同様、新しい証明書は更新されませんので、強制的に更新したい場合は --force-renew オプションをお忘れなく。

新規発行したときのオプションを忘れちゃったらどうするの?

あるあるですね。あとは前任者が退職してしまった等でわからなくなるということも有り得そうです。

本ブログでは必要最低限のオプションだけを指定していますが、企業によっては自社のポリシーに沿ったオプションを指定することが考えられます。

例えば、--rsa-key-size オプションで鍵長を変更するなどです。

letsencrypt では、renew 等で証明書を更新するために発行時の情報として/etc/letsencrypt/renewal/[ドメイン].conf というファイルを残すようになっています。

以下は racchai.com を発行したときの conf ファイルになります。

cert = /etc/letsencrypt/live/racchai.com/cert.pem
privkey = /etc/letsencrypt/live/racchai.com/privkey.pem
chain = /etc/letsencrypt/live/racchai.com/chain.pem
fullchain = /etc/letsencrypt/live/racchai.com/fullchain.pem

# Options used in the renewal process
[renewalparams]
authenticator = webroot
installer = None
account = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
webroot_path = /var/www/html
rsa_key_size = 4096
[[webroot_map]]
racchai.com = /var/www/html

これを見れば、鍵長が 4096 bit にカスタマイズされていることがわかりますね。

オプションの指定ミスを防ぎたい

誤って前回と違う値をオプションに指定してしまうことも考えられますね。人間ですから。

そういった方にはコマンドの引数に設定ファイル(cli.ini)を指定することをおすすめします。

さっそくさきほどの 鍵長を 4096 bit に変更する cli.ini ファイルを作成してみましょう。

# This is an example of the kind of things you can do in a configuration file.
# All flags used by the client can be configured here. Run Let's Encrypt with
# "--help" to learn more about the available options.

# Use a 4096 bit RSA key instead of 2048
rsa-key-size = 4096

# Uncomment and update to register with the specified e-mail address
# email = foo@example.com

# Uncomment and update to generate certificates for the specified
# domains.
domains = racchai.com

# Uncomment to use a text interface instead of ncurses
# text = True

# Uncomment to use the standalone authenticator on port 443
# authenticator = standalone
# standalone-supported-challenges = tls-sni-01

# Uncomment to use the webroot authenticator. Replace webroot-path with the
# path to the public_html / webroot folder being served by your web server.
authenticator = webroot
webroot-path = /var/www/html

あとはこのファイルを letsencrypt-auto コマンドに与えてやればOKです。

./letsencrypt-auto certonly --config ./cli.ini --non-interactive --force-renew

cli.ini を git 等でバージョン管理してやれば履歴や変更の経緯等もわかるようになりますので、これなら引き継ぎも楽ちんですね!

まとめ

今回は Let`s Encrypt で発行した証明書を自動更新するノウハウについて紹介してみました。

少しでも誰かのお役に立てば幸いです。