使用supervisor管理你的nginx、php-fpm进程

supervisor

前言:出处

零、supervisor是什么

Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。

一、安装


apt -y install supervisor

yum -y install supervisor

二、配置


[unix_http_server]

file=/var/run/supervisor.sock

chmod=0700

[inet_http_server]

port=0.0.0.0:7020

username=root

password=xxxxxx

[supervisord]

logfile=/var/log/supervisor/supervisord.log

pidfile=/var/run/supervisord.pid

childlogdir=/var/log/supervisor

[rpcinterface:supervisor]

supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]

serverurl=unix:///var/run/supervisor.sock

[include]

files = /etc/supervisor/conf.d/*.conf

三、管理服务

  • 需要前台运行交于supervisor控制,原理是fork一个守护进程进行监控

  • program 标识,比如php-fpm,可以在后台supervisorctl中进行start/stop/restart操作

  • command 命令,需要加上sleep 1,防止进程还没退出supervisor就去检测,还挺好用

1、管理php-fpm


[program:php-fpm]

command=bash -c "sleep 1 && sudo /usr/local/php7.4/sbin/php-fpm"

process_name=%(program_name)s

autostart=true

autorestart=true

startretries=5

exitcodes=0,2,70

stopsignal=QUIT

stopwaitsecs=2

stdout_logfile=/var/log/supervisor/php-fpm.log

ps -ef|grep php-fpm

root 23034 22175 0 23:37 ? 00:00:00 sudo /usr/local/php7.4/sbin/php-fpm

root 23043 23034 0 23:37 ? 00:00:00 php-fpm: master process (/usr/local/php7.4/etc/php-fpm.conf)

www-data 23044 23043 0 23:37 ? 00:00:00 php-fpm: pool www

www-data 23045 23043 0 23:37 ? 00:00:00 php-fpm: pool www

2、管理nginx


[program:nginx]

command=bash -c "sleep 1 && sudo /usr/local/nginx/sbin/nginx -g 'daemon off;'"

process_name=%(program_name)s

autostart=true

autorestart=true

startretries=5

exitcodes=0,2,70

stopsignal=INT

stopwaitsecs=2

stdout_logfile=/var/log/supervisor/nginx.log

ps -ef|grep nginx

root 22613 22175 0 23:35 ? 00:00:00 sudo /usr/local/nginx/sbin/nginx -g daemon off;

root 22616 22613 0 23:35 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off;

www-data 22617 22616 0 23:35 ? 00:00:00 nginx: worker process

www-data 22618 22616 0 23:35 ? 00:00:00 nginx: worker process

www-data 22619 22616 0 23:35 ? 00:00:00 nginx: worker process

www-data 22620 22616 0 23:35 ? 00:00:00 nginx: worker process

3、管理Laravel job

[program:queue]
command=bash -c "cd /var/www/laravel && sudo /usr/bin/php artisan queue:work --timeout=60 --tries=3"
process_name=%(program_name)s
autostart=true
autorestart=true
startretries=5
exitcodes=0,2,70
stopsignal=INT
stopwaitsecs=2
stdout_logfile=/var/log/supervisor/queue.log
ps -ef|grep queue

root     30755 22175  0 Sep30 ?        00:00:00 sudo /usr/bin/php artisan queue:work --timeout=60 --tries=3
root     30756 30755  0 Sep30 ?        00:01:08 /usr/bin/php artisan queue:work --timeout=60 --tries=3

四、supervisorctl

supervisorctl


supervisor> status

nginx RUNNING pid 22613, uptime 0:03:40

php-fpm RUNNING pid 23034, uptime 0:01:43

supervisor> ?

default commands (type help <topic>):

=====================================

add exit open reload restart start tail

avail fg pid remove shutdown status update

clear maintail quit reread signal stop version

五、参考资料

Supervisor使用详解

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3

管理 nginx、php-fpm 很少见额,用来管理 php cli daemon 进程倒是很合适

3年前 评论
AloneUtopia

nginx php-fpm进程 用得着管理么 :flushed:

3年前 评论
OMGZui

@Hachiko @AloneUtopia 上面只是例子哈,具体管理什么看自己需求

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!