如何用supervisor守护php-fpm主进程以实现php-fpm的自动重启

如何用supervisor守护php-fpm主进程以实现php-fpm的自动重启,第1张

1. 安装supervisor

supervisor本身是python实现的,而且是调研阶段,故先创建一个新的virtualenv环境,然后用pip安装好supervisor包。

至此,基本的调研环境搭建完毕。当然敬枯枣,php-fpm和PHP环境以及前端的Nginx是早就ready的。

2. 分析php-fpm.sh脚本

通常编译安装PHP后,php-fpm这个2进制的C程序也会被编译并安装好,典型路径在php_install_path/sbin/目录下。该

目录下还有个名为php-fpm.sh的脚本用于控制php-fpm进程的start/stop/restart/reload等动作。

./sbin/php-fpm.sh脚本中,”start” *** 作启动了php-fpm主进程,其余的 *** 作都是通过向php-fpm master进程发signal实现的。

<code class="hljs bash">## code segment in php-fpm.sh

case "$1" in

start)

echo -n "Starting php-fpm "

## 下面这行是关键命令

$php_fpm_BIN --daemonize $php_opts

if [ "$?" != 0 ] then

echo " failed"

exit 1

fi

wait_for_pid created $php_fpm_PID

if [ -n "$try" ] then

echo " failed"

exit 1

else

echo " done"

fi

</code>

从上面是终端输入”./sbin/php-fpm.sh

start”时,实际执行的代码,可以看到,php-fpm进程的启动参数是–daemonize

$php_opts,而$php_opts的值为”–fpm-config $php_fpm_CONF –pid $php_fpm_PID”。

注意: php-fpm.sh启动败烂php-fpm master进程时,传入了daemonize参数,表明php-fpm master process以守护(daemon)方式启动,而根据supervisor文档的说明,当用supervisor监护进程时,被监护进程不能是守护进程,这是由于守护进程通常会在fork完子进程后就让父进程”结束生命”,也即由supervisor创建的父进程退出,此时,supervisor无法再监护已退出进程创建出来的子进程。关亮拆于daemon process的行为,可以参考Linux Daemon Writing HOWTO一文来理解。

根据上面的分析,我们知道,只要supervisor启动php-fpm进程时,不传入daemonize参数即可。

3. 实现php-fpm主进程守护功能的supervisor配置文件

上面的分析已经告诉我们应该怎么解决问题了,下面直接上验证可用的配置文件。文件位于php-fpm.conf同级目录下(典型路径为php_install_path/etc/)。

<code class="hljs bash"><code class="hljs vhdl">

[inet_http_server] inet (TCP) server disabled by default

port=127.0.0.1:9015(ip_address:port specifier, *:port for all iface)

[supervisord]

logfile=./var/log/supervisord.log (main log filedefault $CWD/supervisord.log)

logfile_maxbytes=50MB(max main logfile bytes b4 rotationdefault 50MB)

logfile_backups=2 (num of main logfile rotation backupsdefault 10)

loglevel=info(log leveldefault infoothers: debug,warn,trace)

pidfile=./var/run/supervisord.pid (supervisord pidfiledefault supervisord.pid)

nodaemon=false (start in foreground if truedefault false)

minfds=1024 (min. avail startup file descriptorsdefault 1024)

minprocs=200 (min. avail process descriptorsdefault 200)

identifier=sup.php-fpm (supervisord identifier, default is 'supervisor')

[rpcinterface:supervisor]

supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]

serverurl=http://127.0.0.1:9015 use an http:// url to specify an inet socket

[program:php-fpm]

command=bash -c "sleep 1 &&/home/slvher/tools/php/5.6.11/sbin/php-fpm --fpm-config /home/slvher/tools/php/5.6.11/etc/php-fpm.conf --pid /home/slvher/tools/php/5.6.11/var/run/php-fpm.pid" the program (relative uses PATH, can take args)

process_name=%(program_name)s process_name expr (default %(program_name)s)

autostart=truestart at supervisord start (default: true)

autorestart=truewhether/when to restart (default: unexpected)

startretries=5max # of serial start failures (default 3)

exitcodes=0,2,70 'expected' exit codes for process (default 0,2)

stopsignal=QUIT signal used to kill process (default TERM)

stopwaitsecs=2 max num secs to wait b4 SIGKILL (default 10)

</code></code>

配置文件结构通过查看supervisor文档很容易就能掌握,有两个配置项需要特别注意:

1) command

它指定了supervisor要监控的进程的启动命令,可以看到,这里我们没有给php-fpm传入daemonize参数,其余参数只是展开了php-fpm.sh中的shell变量而已。

大家已经注意到,command也不是直接调起php-fpm,而是通过bash -c执行了两个命令,而第一个命令是sleep 1。这是由于php-fpm在stop后,其占用的端口通常不能立即释放,此时,supervisor以极快的速度试图重新拉起进程时,可能会由于报如下错误而导致几次retry均失败:

<code class="hljs bash"><code class="hljs vhdl"><code class="hljs vbscript">## var/log/php-fpm.error.log

[18-Jul-2015 21:35:28] ERROR: unable to bind listening socket for address '127.0.0.1:9002': Address already in use (98)

[18-Jul-2015 21:35:28] ERROR: FPM initialization failed</code></code></code>

而supervisor目前还不支持delay restart功能,因此,这里只能通过先sleep再启动的略显tricky的方法来解决问题,结果表明,疗效不错且无副作用。-_-

2) autorestart

其文档描述如下:

<code class="hljs bash"><code class="hljs vhdl"><code class="hljs vbscript"><code class="hljs livecodeserver">May be one of false, unexpected, or true. If false, the process will never be autorestarted. If unexpected, the process will be restart when the program exits with an exit code that is not one of the exit codes associated with this process’ configuration (see exitcodes). If true, the process will be unconditionally restarted when it exits, without regard to its exit code.</code></code></code></code>

其默认值是unexpected,表示若被监护进程的exit code异常时,supervisor才会重新拉起进程。这里设置为true,表明任何时候进程退出均会被再次拉起。

这样配置好后,在本文第1步搭建好的virtualenv环境中,运行如下命令即可完成supervisor对php-fpm master进程的监护:

<code class="hljs bash"><code class="hljs vhdl"><code class="hljs vbscript"><code class="hljs livecodeserver"><code class="hljs avrasm">shell>supervisord -c etc/sup.php-fpm.conf</code></code></code></code></code>

然后,通过ps x | fgrep fpm可以看到,php-fpm主进程已经被拉起了。

然后,kill掉php-fpm主进程,再次ps x | fgrep fpm可以看到,一个新的php-fpm主进程会被supervisor创建出来。

至此,用supervisor守护php-fpm主进程以实现php-fpm的自动重启的需求已经解决了。

Supervisor 简单来说就是在你需要常陵差驻运行的程序挂掉的时候及时拉起。这对于现在的场景是非常合适啊。

编辑 /etc/supervisord.d/php-fpm.ini,配置如下:

进入 Supervisor 控制台:

请记住这里最后的结果是 ERROR,然而查看进程 php-fpm 确实在跑了,kill 掉进程也能尺茄拉起来,我不太清楚为什么会这样。但是跑了一天后,php-fpm 又挂了。对,是又挂了而且没拉起来。

继续锁定 ERROR,先查看 php-fpm 日志。

一直在报这个错误,看来必陵汪察须要解决才行呢,看样子是端口被占用了?但是是基于 supervisor 启动的,怎么会有这种错误呢?

当然,配置是有问题的。

php-fpm 进程默认是以 daemon 方式启动的,而 Supervisor 文档 的说明是, 使用 supervisor 监护进程时,被监护的进程不能是守护进程。

我们需要关闭 php-fpm 的进程守护,编辑 /usr/local/php/etc/php-fpm.conf ,查找 daemonize 修改为 no 。

然后 killall php-fpm 的所有进程,现在查看 php-fpm 日志。

查看 supervisor 状态:

program:hhvm]

command=/usr/bin/hhvm –mode server –config /空裤槐etc/hhvm/config.hdf –config /etc/纯禅hhvm/php.ini -vServer.Type=fastcgi -vServer.Port=9000 the program (relative uses PATH, can take args)

process_name=%(program_name)s process_name expr (default %(program_name)s)

numprocs=1 number of processes copies to start (def 1)

directory=/tmp directory to cwd to before exec (def no cwd)

autostart=true start at supervisord start (default: true)

autorestart=unexpected whether/when to restart (default: unexpected)

stopwaitsecs=10 max num secs to wait b4 SIGKILL (default 10)

user=nginx 启HHVM用户名

hhvm能工作daemon模式

[program:hhvm]

command=/usr/bin/hhvm –mode server –config /etc/hhvm/config.hdf –config /etc/hhvm/php.ini -vServer.Type=fastcgi -vServer.Port=9000 the program (relative uses PATH, can take args)

process_name=%(program_name)s process_name expr (default %(program_name)s)

numprocs=1 number of processes copies to start (def 1)

directory=/tmp directory to cwd to before exec (def no cwd)

autostart=true start at supervisord start (default: true)

autorestart=unexpected whether/when to restart (default: unexpected)

stopwaitsecs=10 max num secs to wait b4 SIGKILL (default 10)

user=nginx 启HHVM用户名

hhvm能工作daemon模斗友式


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12504726.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存