Mattermost の SSL 証明書の更新を Certbot タイマーで行う

急に Mattermost に接続できなくなった。

環境:WebARENA Indigo Linux 1GB(Ubuntu 22.04)、Freenom(ドメインレジストラ)

いろいろ調べた結果、以下のコマンドで調べてみると Nginx が Inactive となって止まっていると分かった。

$ sudo systemctl status nginx

上記コマンド結果を読んでみると、/etc/nginx/nginx.conf に余計な } が入っていることが原因のようだ。早速編集して } を削除した。

以下のコマンドで再起動。

$ sudo systemctl stop nginx
$ sudo systemctl start nginx

これで問題なく Mattermost が動くようになった。

***

なぜ何もしていないのに突然動かなくなったのか。どうやら SSL 証明書の自動更新スクリプトが原因らしい。

僕は以下のサイトの「SSL証明書の自動更新」の項目に従って SSL 証明書の自動実行をする設定にしていた。

【ライフハック】チャットツール『Mattermost』で家族コミュニケーション [準備編] | en.
https://en-log.net/lifehack_mattermost_00_preparation

ちょうどこの自動実行のタイミングで Mattermost に接続できなくなったので、それが原因に違いない。なお、Let’s Encrypt の更新自体はうまくいっていた。

参考にしたサイトでは、Nginx を止めないと SSL 証明書の更新ができないと書いてある。通常こういう場合は、—standalone オプションではなく、—webroot オプションを付けて更新を実行すればいい。しかし、そのサイトのやり方では、Nginx を一旦止めて更新するスクリプトを採用している。僕の環境ではこれがうまく行かなかったわけだ。

試しに Nginx が動いている状態で dry-run を実行してみる。

$ sudo certbot renew --dry-run

すると Congratulations, all simulated renewals succeeded: と表示されて更新がうまくいく。Nginx を止めなくていいようだ。

よく調べてみると、最近は cron など使わなくても Certbot に組み込まれたタイマーを使って簡単に SSL 証明書の更新ができるようになっているようだ。以下のコマンドでタイマーのステータスを確認する。

$ sudo systemctl status certbot.timer
certbot.timer - Run certbot twice daily
Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
Active: active (waiting) since Sat 2022-11-12 08:25:11 JST; 2h 34min ago
Trigger: Sat 2022-11-12 23:11:36 JST; 12h left
Triggers: certbot.service

日に2回実行されるようになっている。

SSL 証明書の自動更新が行われた場合、Nginx サーバーの再起動も行いたい。/lib/systemd/system/certbot.service を以下のように変更する。

【変更前】
[Unit]
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://certbot.eff.org/docs
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot -q renew
PrivateTmp=true
【変更後】
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://certbot.eff.org/docs
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot -q renew -post-hook "systemctl reload nginx.service"
PrivateTmp=true

systemctl の再読み込み。

$ sudo systemctl daemon-reload

次の自動更新時でうまくいっているか確かめてみよう。

参考:
How to Start, Stop, and Restart Nginx (systemctl & Nginx Commands)
https://phoenixnap.com/kb/nginx-start-stop-restart

【Ubuntu+Nginx】Let's EncryptでSSL証明書を発行してhttps通信を行う - Qiita
https://qiita.com/shin4488/items/8738e13b92143c88aab6

Ubuntu 20.04 Let’s Encryptを使用してNginxでhttps接続(Certbot使用) - Symfoware
https://symfoware.blog.fc2.com/blog-entry-2511.html

Ubuntu 20.04でLet’s Encryptを使用してNginxを保護する方法 | DigitalOcean
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04-ja

Let's Encrypt で webroot 利用時に更新(renew)がこけた時の対処方法 - Qiita
https://qiita.com/fallout/items/6676011cdfe42be6334a

Certbot証明書発行・更新コマンドとNginx設定(Let’s Encrypt) | Mulong.mehttps://www.mulong.me/tech/wordpress/certbot-letsencrypt/

certbot-autoの--pre-hookと--deploy-hookと--post-hook | 買い物、自重。なバイク乗りの日記
https://griffonworks.net/nikki/archive/4415.php

【簡単】Let’s Encryptの自動更新をcronなしで実現しよう | TACK-WEB
https://tackweb.net/index.php/2020/09/21/auto_renew_ssl/

コマンド解説 - Let's Encrypt 総合ポータル
https://free-ssl.jp/command/

Certbot - ArchWiki
https://wiki.archlinux.jp/index.php/Certbot

10.6.4. 既存のユニットファイルの変更 Red Hat Enterprise Linux 7 | Red Hat Customer Portal
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-unit_file_modify


この記事が気に入ったらサポートをしてみませんか?