Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
OrganizationEventAdvent CalendarQiitadon (β)
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.


【CentOS7】Rails5 + Nginx + Unicorn+SES(AWS)の環境にMonitを導入

【CentOS7】Rails5 + Nginx + Unicorn+SES(AWS)の環境にMonitを導入する記事です。

- Monitのインストール
- Nginx / Unicornのプロセスが異常終了してしまった場合に、復旧させる設定を行う
- 監視プロセスの異常終了・起動を検知した際に、SES経由でメール通知を行う

1. Monitのインストール

$ sudo yum install monit

2. Monitの起動・自動起動と確認

$ sudo systemctl start monit
$ sudo systemctl enable monit.service
$ sudo systemctl is-enabled monit

3. Nginxの設定ファイルを作成

check process nginx with pidfile /run/nginx.pid
  group nginx
  start program = "/bin/systemctl start nginx"
  stop  program = "/bin/systemctl stop nginx"
  if 5 restarts within 5 cycles then unmonitor

pidfile /run/nginx.pidのパスは /etc/nginx/nginx.confのpid と合わせる必要がある。

group nginx 任意のGroupを指定できる。これを指定するとGroup単位での監視・停止・起動が可能になる。

if 5 restarts within 5 cycles then unmonitor 5回起動を試みて失敗した場合、監視の対象から外れる

4. UnicornをServiceから起動させる

下記のURLを参考に/etc/init.d/unicorn start|stopで起動・停止できるように設定する
参考URL: https://engineer.blog.lancers.jp/2017/12/unicorn_restart/

5. Unicornの設定ファイルを作成

check process unicorn with pidfile /var/run/unicorn/unicorn.pid
  group unicorn
  start program = "/bin/sh -l -c '/etc/init.d/unicorn start'"
  stop program = "/bin/sh -l -c '/etc/init.d/unicorn stop'"
  if 5 restarts within 5 cycles then unmonitor

pidfile /var/run/unicorn/unicorn.pidのパスはunicorn.rbのpidと合わせる。

6. SES Mail通知の設定

set mailserver email-smtp.us-west.amazonaws.com port *** username "********" password "******************" using tlsv1 with timeout 30 seconds

set mail-format {
  from: **********@***.com
  reply-to: **********@***.com
  subject: $HOST: $SERVICE $EVENT at $DATE
           $SERVICE $EVENT at $DAT
           Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.

           Yours sincerely,

set alert **********@***.com                   # receive all alerts


7. syntax check

$ sudo monit -t
Control file syntax OK

8. 設定したファイルを読み込ませる

$ sudo monit reload

9. テスト


$ sudo pkill -kill -f nginx
$sudo monit status

Process 'nginx'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  pid                          15470
  parent pid                   1
  uid                          0
  effective uid                0
  gid                          0
  uptime                       2h 24m
  threads                      1
  children                     3
  cpu                          0.0%
  cpu total                    0.0%
  memory                       0.1% [1.2 MB]
  memory total                 0.5% [8.8 MB]
  security attribute           (null)
  data collected               Wed, 31 Oct 2018 17:32:54

10. コマンドまとめ

$ sudo monit start unicorn # 監視の開始
$ sudo monit status # ステータス
$ sudo monit -t # 文法チェック
$ sudo monit reload # 再読み込み

$ sudo systemctl start monit # monit起動
$ sudo systemctl enable monit.service # monit自動起動
$ sudo systemctl is-enabled monit # monit自動起動確認

11. 参考記事


Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Help us understand the problem. What are the problem?