LoginSignup
2
0

More than 1 year has passed since last update.

Monit で Ubuntu 20.0.4 の nginx を監視・自動復旧

Posted at

はじめに

対象読者

  • Ubuntu で nginx サーバを運用している人
  • 気づいたら nginx がお亡くなりになっていて対応しないとならない、という事態に陥ったことのある人
  • あるいは上記のような事態に陥りたくない人

動作環境

  • Ubuntu 20.0.4

インストール

Monit

  • Monit
  • 以下の通りインストール
apt install monit

設定

/etc/monit/monitrc

  • 初期値は 120 秒ごとにチェックするようになっている。運用に応じて変更するべし
/etc/monit/monitrc
  set daemon 120            # check services at 2-minute intervals
  • ログは以下のところに出力されるようになっている。ここも含め、他の設定も特に変更する必要はないと思われる
/etc/monit/monitrc
  set log /var/log/monit.log

/etc/monit/conf-available/nginx

  • 今回は nginx を監視するので監視対象用のコンフィグファイルは /etc/monit/conf-available/nginx
  • 最近の monit はインストールした時点でコンフィグファイルが用意されている
  • 中身は以下のようになっているので、自身の環境で要変更箇所があれば変更すべし
/etc/monit/conf-available/nginx
 check process nginx with pidfile /var/run/nginx.pid
   group www
   group nginx
   start program = "/etc/init.d/nginx start"
   stop program = "/etc/init.d/nginx stop"
#  if failed port 80 protocol http request "/" then restart
   if 5 restarts with 5 cycles then timeout
   depend nginx_bin
   depend nginx_rc

 check file nginx_bin with path /usr/sbin/nginx
   group nginx
   include /etc/monit/templates/rootbin

 check file nginx_rc with path /etc/init.d/nginx
   group nginx
   include /etc/monit/templates/rootbin
  • 上記コンフィグを適用したいので、シンボリックリンクを作成
ln -s /etc/monit/conf-available/nginx /etc/monit/conf-enabled/
  • 設定を再読込
systemctl reload monit

動作確認

  • 試しに nginx -s stop を実行して nginx プロセスを止めてみると、自動復旧してくれるようになる
  • ログは以下のように出力される
/var/log/monit.log
[JST Jul  5 18:35:19] error    : 'nginx' process is not running
[JST Jul  5 18:35:19] info     : 'nginx' trying to restart
[JST Jul  5 18:35:19] info     : 'nginx' start: '/etc/init.d/nginx start'

おわりに

感想

  • 日本語で、最近の Ubuntu 環境での monit + nginx 記事が見当たらなかったので、まとめました
  • これで知らない間に nginx が沈黙していた……という状況のときに自動復活してくれるので精神衛生面で助かる

参考記事

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