LoginSignup
0
1

certbot renew時にnginxを再起動して、mattermostへ通知する

Posted at

毎度、ググっても出てこない小ネタを取り扱っております。
本記事は個人的な見解であり、筆者の所属するいかなる団体にも関係ございません。

0. はじめに

certbotを使って自動的に証明書を発行し、更新も自動化されているかたも多いかと思います。
しかし、certbot renewしていてもnginxを再起動してくれるわけでもないので、証明書が更新されているのにもかかわらず、ブラウザーで見ると「証明書が古いです」とか言われて、慌ててnginxを再起動するなんて事もありますね。

定期的に再起動するような運用をしていたら問題ありませんが、そうでない場合このトラップに引っかかるなんて事もあります。

そこで、certbotのhookを使って自動的にnginxを再起動して、さらに証明書が更新されたというのが通知されると安心便利なので通知もしちゃおうよ、という話です。

1. nginx リロードスクリプト

以下のgistを元にnginx-reload.shを保存します。
Let's Encrypt Certbot post hook command for Nginx which checks the updated configuration files and reloads the server if everything validates.
https://gist.github.com/justinhartman/79c527076ce7cc740423eb92b5600d52

cd /etc/letsencrypt/renewal-hooks/deploy/
sudo wget https://gist.githubusercontent.com/justinhartman/79c527076ce7cc740423eb92b5600d52/raw/5709ad0ff74d713b3ab26c6b7c8999f98838ff0a/01_nginx-reload-post-hook.sh

実行権限を付けてテストしておきます。

sudo chmod a+x /etc/letsencrypt/renewal-hooks/post/nginx-reload.sh
sudo nginx -t && sudo systemctl reload nginx

2. mattermostへ通知します

通知スクリプト

02_send-renewal_notify-hook.sh
#!/bin/bash

# Hostname
HOST='hoge server'

# XXXX team
MATTERMOST_WEBHOOK_URL='https://<MatttermostURL>/hooks/hogehogehoge'

function post_mattermost ()
{
  eval postmsg="$1"
  curl -i -s -o /dev/null -X POST -H 'Content-Type: application/json' -d "${postmsg}" $MATTERMOST_WEBHOOK_URL
}

msg="{\"text\": \"$HOSTの証明書を更新しました。確認をお願いします。\"}"
curl -i -s -o /dev/null -X POST -H 'Content-Type: application/json' -d "$msg" $MATTERMOST_WEBHOOK_URL

実行権を付けてテストしておきます。

sudo chmod a+x ./02_send-renewal_notify-hook.sh
bash ./02_send-renewal_notify-hook.sh

参考資料

Let's Encrypt Certbot post hook command for Nginx which checks the updated configuration files and reloads the server if everything validates.
https://gist.github.com/justinhartman/79c527076ce7cc740423eb92b5600d52

Let's EncryptのSSL証明書更新時にサービスを再起動する #Let’sEncrypt - Qiita
https://qiita.com/kazuhidet/items/9d58a104f93d9ff7302d

Let's Encryptの証明書自動更新のSlack通知を賢く行う - Ryoto's Blog
https://www.ryotosaito.com/blog/?p=485

0
1
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
0
1