Let’s Encriypt の証明書の期間は3ヶ月。有効期限の更新を忘れるとWebサイトにアクセスができなくなるので更新を自動化して運用する方法。
snap版 certbot は自動更新
Let’s Encrypt の3ヶ月毎に更新が必要なのですが、snap版のcertbotをインストールすれば、最適なタイミングで自動で更新をしてくれます。
<snap版のインストールはこちら>
cron 等で設定をするというような説明もありますが、snap版では不要です。
systemctl list-timers コマンドで、「snap.certbot.renew.timer」が設定されていればOK。
> systemctl list-timers
NEXT LEFT LAST PASSED UNIT
2024-03-23 22:30 10h left 2024-03-23 07:46 48min ago snap.certbot.renew.timer
nginx を自動でリスタート
証明書の更新をしても nginx等のサービスをリスタートさせないと証明書が更新されないので、これも自動で実行する。
.confファイルに定義
nginx をリスタートさせるだけのような単純な処理であれば、confファイルに追記すれば簡単に実現できる。
<設定ファイルの確認>
sudo certbot certificates を実行し、Certificate Name: を確認する。
Certificate Name の名前 + .conf が設定ファイル名になる。
以下のサンプルの場合は、sample.com.conf が設定ファイルになる。
> sudo certbot certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: sample.com
Serial Number: xxxxxxxxxx
Key Type: RSA
Domains: sample.com www.sample.com
Expiry Date: 2024-xx-xx 12:33:51+00:00 (VALID: 1 days)
Certificate Path: /etc/fullchain.pem
Private Key Path: /etc/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/etc/letsencrypt/renewal/sample.com.conf を編集して、
deploy_hook = systemctl restart nginx
を追加する。
これで、証明書の更新が成功した後に、nginx がリスタートされて新証明書が有効になる。
# renew_before_expiry = 30 days
version = 2.x.x
archive_dir = /etc/sample.com
cert = /etc/cert.pem
privkey = /etc/privkey.pem
chain = /etc/chain.pem
fullchain = /etc/fullchain.pem
deploy_hook = systemctl restart nginx
# Options used in the renewal process
[renewalparams]
authenticator = nginx
account = xxxxxxxxxx
server = https://xxxxxxxxxx/directory
key_type = rsa
hook用ディレクトリに .sh を置く
別の方法として、
/etc/letsencrypt/renewal-hooks
に シェルスクリプト を記載したファイルを置くことでも実現できる。
コメント