[Laravel7.x]Supervisorを導入してみる!

はじめに

前回の記事で嫌になったのだけど、再度やってみる。

参考)https://blog.e2info.co.jp/2021/04/11/amazon-linux-2_supervisor/

参考)https://zuntan02.hateblo.jp/entry/2019/06/27/205849
#というよりほぼ丸パクリ…

インストール

Python3とSupervisor4をインストール

インストール!

$ sudo yum install python3
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                   | 3.7 kB  00:00:00
246 packages excluded due to repository priority protections
パッケージ python3-3.7.9-1.amzn2.0.2.x86_64 はインストール済みか最新バージョンです
何もしません

$ sudo pip3 install supervisor
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting supervisor
  Downloading https://files.pythonhosted.org/packages/de/c9/50b1575b46ad9802e1b8d444c1ba0db842a1006ff7c9bea9d5e0ea700f2d/supervisor-4.2.2-py2.py3-none-any.whl (743kB)
    100% |████████████████████████████████| 747kB 1.6MB/s
Installing collected packages: supervisor
Successfully installed supervisor-4.2.2

警告でとるけど、root権限でええねん。

設定

Supervisorの設定

$ sudo mkdir /etc/supervisor
$ echo_supervisord_conf > supervisor.conf
$ sudo mv supervisor.conf /etc/supervisor/
$ sudo mkdir /etc/supervisor/conf.d
$ sudo vim /etc/supervisor/supervisor.conf

- logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
+ logfile=/var/log/supervisor/supervisor.log ; main log file; default $CWD/supervisord.log

- pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
+ pidfile=/var/run/supervisor.pid ; supervisord pidfile; default supervisord.pid

- ;[include]
- ;files = relative/directory/*.ini
+ [include]
+ files = /etc/supervisor/conf.d/*.conf↲

ログローテーションの設定

$ sudo mkdir /var/log/supervisor
$ sudo vim /etc/logrotate.d/supervisord
/var/log/supervisor/*.log {
    missingok
    weekly
    notifempty
    nocompress
}

Supervisorのサービス登録

$ sudo vim /etc/systemd/system/supervisord.service
[Unit]
Documentation=http://supervisord.org
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/supervisord -c /etc/supervisor/supervisor.conf
ExecReload=/usr/local/bin/supervisorctl reload
ExecStop=/usr/local/bin/supervisorctl shutdown
User=root

[Install]
WantedBy=multi-user.target

Supervisorサービス確認

$ sudo systemctl start supervisord
$ sudo systemctl status supervisord
● supervisord.service
   Loaded: loaded (/etc/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: active (running) since 木 2021-05-20 05:58:18 UTC; 8s ago
     Docs: http://supervisord.org
  Process: 15369 ExecStart=/usr/local/bin/supervisord -c /etc/supervisor/supervisor.conf (code=exited, status=0/SUCCESS)
 Main PID: 15372 (supervisord)
   CGroup: /system.slice/supervisord.service
           └─15372 /usr/bin/python3 /usr/local/bin/supervisord -c /etc/supervisor/supervisor.conf...

 5月 20 05:58:18 xxxxxx systemd[1]: Starting superviso...
 5月 20 05:58:18 xxxxxx systemd[1]: Started supervisor...
Hint: Some lines were ellipsized, use -l to show in full.
$ sudo systemctl stop supervisord
$ cat /var/log/supervisor/supervisor.log
2021-05-20 05:58:18,503 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
2021-05-20 05:58:18,503 WARN No file matches via include "/etc/supervisor/conf.d/*.conf"
2021-05-20 05:58:18,506 INFO RPC interface 'supervisor' initialized
2021-05-20 05:58:18,506 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2021-05-20 05:58:18,512 INFO daemonizing the supervisord process
2021-05-20 05:58:18,512 INFO supervisord started with pid 15372
2021-05-20 05:58:37,534 WARN received SIGTERM indicating exit request

なんとなく動いたし、ログも出力されとる。

Supervisorサービスの自動起動設定

$ sudo systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /etc/systemd/system/supervisord.service.

サーバを再起動してログを確認して自動起動しているかを確認する。

Laravel-worker

LaravelワーカーをSuperviserに監視してもらう設定ファイルを作成する。

$ sudo vim /etc/supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /path/to/artisan queue:work
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=nginx
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/supervisor/worker.log
stopwaitsecs=3600

設置後再起動して、SupervisorのログやLaravelのログを見て動作確認する。問題なさそう。

まとめ

なかなか面倒なインストール手順…

コメント

タイトルとURLをコピーしました