【转】daemon、supervisor 与php

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

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

[program:php-fpm]
command=bash -c "sleep 1 && /usr/local/php7/sbin/php-fpm --fpm-config /usr/local/php7/etc/php-fpm.conf --pid /usr/local/php7/var/run/php-fpm.pid"
process_name=%(program_name)s
autostart=true
autorestart=true
startretries=5
exitcodes=0,2,70
stopsignal=QUIT
stopwaitsecs=2
stdout_logfile=/data/logs/supervisord/php-fpm.log

进入 Supervisor 控制台:

$ sudo supervisorctl
nginx                            RUNNING   pid 26046, uptime 0:00:01
php-fpm                          STARTING
supervisor> reread
php-fpm: changed
supervisor> start php-fpm
php-fpm: ERROR (abnormal termination)

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

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

[01-Sep-2018 11:20:21] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[01-Sep-2018 11:20:21] ERROR: FPM initialization failed
[01-Sep-2018 11:20:23] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[01-Sep-2018 11:20:23] ERROR: FPM initialization failed
[01-Sep-2018 11:20:24] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[01-Sep-2018 11:20:24] ERROR: FPM initialization failed
[01-Sep-2018 11:20:25] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[01-Sep-2018 11:20:25] ERROR: FPM initialization failed

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

当然,配置是有问题的。

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

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

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

$ tail -f /usr/local/php/var/log/php-fpm.log
[01-Sep-2018 11:28:25] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[01-Sep-2018 11:28:25] ERROR: FPM initialization failed
[01-Sep-2018 11:28:26] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[01-Sep-2018 11:28:26] ERROR: FPM initialization failed
[01-Sep-2018 11:28:27] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[01-Sep-2018 11:28:27] ERROR: FPM initialization failed
[01-Sep-2018 11:28:28] NOTICE: Terminating ...
[01-Sep-2018 11:28:28] NOTICE: exiting, bye-bye!
[01-Sep-2018 11:28:29] NOTICE: fpm is running, pid 26011
[01-Sep-2018 11:28:29] NOTICE: ready to handle connections

查看 supervisor 状态:

$ sudo supervisorctl
nginx                            RUNNING   pid 26046, uptime 0:00:01
php-fpm                          RUNNING   pid 26009, uptime 0:00:45

你可能感兴趣的:(【转】daemon、supervisor 与php)