LMLPHP后院

php-fpm 顺滑的启动 重启 终止操作技术

maybe yes 发表于 2021-04-21 16:26

很多时候,我们被 service, /etc/init.d/ 洗脑了,觉得来个方便的操作应该是那样,想起当年老师讲 signal 的时候,觉得排不上用场,通信一般都是 socket 的啊,有谁会用信号来进行进程的通信呢?这不,signal 肯定是有优点的,我们习惯了 kill 掉一个命令,却总是忘记了使用信号来的更加优雅。

启动 php-fpm

/usr/local/php/sbin/php-fpm

php 5.3.3 以后的 php-fpm 不再支持 php-fpm 以前具有的 php-fpm (start|stop|reload) 等命令,所以不要再看这种老掉牙的命令了,需要使用信号控制。 

fpm master 进程可以理解以下信号:

  • INT, TERM 立刻终止
  • QUIT 平滑终止
  • USR1 重新打开日志文件
  • USR2 平滑重载所有 worker 进程并重新载入配置和二进制模块

很多大神要骂了,这就是宗教信仰啊,定义的这么奇怪

一个简单直接的重启方法,查看 php-fpm 的 master 进程号

# ps aux|grep php-fpm
root       437  0.0  0.4 216312  9136 ?        Ss   4月08   1:54 php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf)
www-data   598  0.0  0.8 217360 16148 ?        S    4月08   0:06 php-fpm: pool www
www-data   599  0.0  0.7 217100 15076 ?        S    4月08   0:06 php-fpm: pool www
pi       27083  0.0  0.0   6168   648 pts/3    S+   12:05   0:00 grep --color=auto php-fpm

重启 php-fpm:

kill -USR2 437

顺滑的重启好了。

上面方案一般是没有生成 php-fpm.pid 文件时使用,如果已生成 php-fpm.pid,使用下面这种方案。

上面 master 进程可以看到,matster 使用的是 /etc/php/7.3/fpm/php-fpm.conf 这个配置文件,cat /etc/php/7.3/fpm/php-fpm.conf 发现:

[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
;pid = run/php-fpm.pid

pid 文件路径应该位于 /var/run/php-fpm.pid,由于注释掉,所以没有生成,我们把注释去除,再 kill -USR2 437 重启 php-fpm,便会生成 pid 文件,下次就可以使用以下命令重启、关闭 php-fpm 了:

php-fpm 关闭:
kill -INT 'cat /var/run/php-fpm.pid'
php-fpm 重启:
kill -USR2 'cat /var/run/php-fpm.pid'

使用信号来控制,返璞归真,性能好啊,这才是应用开发的标准操作

附上 php-fpm 性能调优

pm = dynamic

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI. The below defaults are based on a server without much resources. Don't
; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
pm.max_children = 99

; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 40

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 20

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 60

; The number of seconds after which an idle process will be killed.
; Note: Used only when pm is set to 'ondemand'
; Default Value: 10s
;pm.process_idle_timeout = 10s;

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
;pm.max_requests = 500
2024-11-23 17:38:33 1732354713 0.008120