在PHP生命周期的不同阶段,涉及服务器的各类操作均借助SAPI接口完成。
各服务器抽象层遵循统一的约定,这一规范被称为SAPI接口。
查阅PHP源码可知,获取服务器相关信息的调用全部经由SAPI接口中的对应方法实现。
php-fpm + nginx
php + terminal
...
PHP常见的四种运行模式
SAPI(Server Application Programming Interface)即服务器应用程序编程接口,是PHP与其他应用交互的桥梁。 每个SAPI的实现都对应一个_sapi_module_struct结构体变量。
PHP脚本的执行途径多样,既可通过Web服务器处理,也可在命令行环境下运行,甚至嵌入到其他程序中执行。
SAPI提供了与外部通信的标准接口,常见的实现包括cgi、fast-cgi、cli以及ISAPI(Apache模块的DLL)。
- ISAPI模式(如Apache的apache2handler模式)作为Web服务器的一个模块加载运行,即将PHP源码与Web Server代码一同编译。运行时两者处于同一进程,共享地址空间。例如在LAMP架构中,PHP作为Apache模块运行,Apache以多线程方式调用该模块(IIS同理)。
- CGI模式下,Web Server将动态请求转发给CGI程序(以PHP为例),相当于fork出一个子进程并exec(php process)。CGI程序负责解释请求内容,随后将子进程的output返回。此时Web Server与PHP进程地址空间独立,PHP作为独立程序运行。
- FastCGI模式是CGI的增强版。传统CGI采用单进程或多线程方式,执行完毕后销毁,每次均需重新加载配置和环境变量(创建-执行)。 而FastCGI不同,它是一种常驻(long-live)型CGI,激活后持续运行,无需每次请求都花费时间去fork一次。
- CLI command line interface
CLI
php_module_startup
php_request_startup
php_execute_script
php_request_shutdown
php_module_shutdown
PHP-FPM
自php 5.3.3版本起,php-fpm不再支持php-fpm (start|stop|reload)等命令,必须使用信号来控制。php-fpm master进程可识别以下信号:
- kill -USR1 "php-fpm master pid" 用于重新打开日志文件。执行后你会发现php-fpm master/worker进程id不发生变化。
- kill -USR2 "php-fpm master pid" 用于平滑重载所有php-fpm进程。执行后你会发现php-fpm master/worker进程id发生改变。
- kill -KILL/-9 php-fpm-master.pid 强制杀死master进程。该信号不允许中断或阻塞,此时master进程无法通知回收worker进程,因此worker进程仍监听端口,能继续正常处理http请求。
- kill -INT/-QUIT/-TERM master pid 用于停止php-fpm服务。该信号会被当前进程树接收,意味着不仅当前进程会收到信号,其子进程也会收到。
- kill master pid 向进程发送SIGTERM信号。此信号可能会被阻塞,但master可以借此机会回收worker进程。
example.
[sujianhui@dev529 ~]$>ps aux | grep php-fpm
root 17000 0.0 0.0 243220 7208 ? Ss 17:00 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
sujianh+ 17001 0.0 0.0 245304 7072 ? S 17:00 0:00 php-fpm: pool www
sujianh+ 17002 0.0 0.0 245304 7072 ? S 17:00 0:00 php-fpm: pool www
sujianh+ 17069 0.0 0.0 112816 976 pts/3 S+ 17:01 0:00 grep --color=auto php-fpm
[sujianhui@dev529 ~]$>sudo kill -USR1 17000
[sujianhui@dev529 ~]$>ps aux | grep php-fpm
root 17000 0.0 0.0 243220 7208 ? Ss 17:00 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
sujianh+ 17001 0.0 0.0 245304 7072 ? S 17:00 0:00 php-fpm: pool www
sujianh+ 17002 0.0 0.0 245304 7072 ? S 17:00 0:00 php-fpm: pool www
sujianh+ 17105 0.0 0.0 112816 972 pts/3 S+ 17:01 0:00 grep --color=auto php-fpm
[sujianhui@dev529 ~]$>sudo kill -USR2 17000
[sujianhui@dev529 ~]$>ps aux | grep php-fpm
root 17122 0.0 0.0 243220 7212 ? Ss 17:01 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
sujianh+ 17123 0.0 0.0 245304 7072 ? S 17:01 0:00 php-fpm: pool www
sujianh+ 17124 0.0 0.0 245304 7072 ? S 17:01 0:00 php-fpm: pool www
sujianh+ 17126 0.0 0.0 112816 976 pts/3 S+ 17:01 0:00 grep --color=auto php-fpm
[sujianhui@dev529 ~]$>pstree 17122 -a
php-fpm
├─php-fpm
└─php-fpm
[sujianhui@dev529 ~]$>sudo kill -INT 17122
[sujianhui@dev529 ~]$>ps aux | grep php-fpm
sujianh+ 17229 0.0 0.0 112816 976 pts/3 S+ 17:03 0:00 grep --color=auto php-fpm
因此,我们应该使用 sudo kill -INT master.pid 来停止php-fpm服务。
nginx的master-worker机制与fpm大体相同,但需注意一个问题:使用systemctl启动起来的master被kill以后,worker也会随之死亡。
正常启动nginx,然后kill掉master
[sujianhui@dev0529 sbin]$>which nginx
/usr/sbin/nginx
[sujianhui@dev0529 sbin]$>sudo nginx
[sujianhui@dev0529 sbin]$>ps aux | grep nginx
root 4562 0.0 0.0 46608 1084 ? Ss 21:46 0:00 nginx: master process nginx
sujianh+ 4563 0.0 0.0 49128 2088 ? S 21:46 0:00 nginx: worker process
sujianh+ 4578 0.0 0.0 112812 972 pts/0 S+ 21:46 0:00 grep --color=auto nginx
[sujianhui@dev0529 sbin]$>sudo kill -9 4562
[sujianhui@dev0529 sbin]$>ps aux | grep nginx
sujianh+ 4563 0.0 0.0 49128 2088 ? S 21:46 0:00 nginx: worker process
sujianh+ 4612 0.0 0.0 112812 972 pts/0 S+ 21:46 0:00 grep --color=auto nginx
[sujianhui@dev0529 sbin]$>kill -9 4563
[sujianhui@dev0529 sbin]$>ps aux | grep nginx
sujianh+ 4638 0.0 0.0 112812 972 pts/0 S+ 21:47 0:00 grep --color=auto nginx
使用systemctl启动的master被kill掉以后,worker也会杀掉
[sujianhui@dev0529 sbin]$>systemctl start nginx
[sujianhui@dev0529 sbin]$>ps aux | grep nginx
root 4678 0.0 0.0 46608 1072 ? Ss 21:47 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
sujianh+ 4679 0.0 0.0 49124 2080 ? S 21:47 0:00 nginx: worker process
sujianh+ 4702 0.0 0.0 112812 972 pts/0 S+ 21:47 0:00 grep --color=auto nginx
[sujianhui@dev0529 sbin]$>sudo kill -9 4678
[sujianhui@dev0529 sbin]$>ps aux | grep nginx
sujianh+ 4732 0.0 0.0 112812 972 pts/0 S+ 21:47 0:00 grep --color=auto nginx
rective run
[sujianhui@dev529 ~]$>kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
[sujianhui@dev529 ~]$>sudo nginx
[sudo] password for sujianhui:
[sujianhui@dev529 ~]$>ps aux | grep nginx
root 3628 0.0 0.0 46600 1052 ? Ss 09:49 0:00 nginx: master process nginx
sujianh+ 3629 0.0 0.0 49096 2056 ? S 09:49 0:00 nginx: worker process
sujianh+ 3637 0.0 0.0 112812 972 pts/0 S+ 09:49 0:00 grep --color=auto nginx
[sujianhui@dev529 ~]$>sudo kill -SIGTERM 3628
[sujianhui@dev529 ~]$>ps aux | grep nginx
sujianh+ 3744 0.0 0.0 112812 972 pts/0 S+ 09:50 0:00 grep --color=auto nginx
[sujianhui@dev529 ~]$>sudo nginx
[sujianhui@dev529 ~]$>ps aux | grep nginx
root 3766 0.0 0.0 46600 1052 ? Ss 09:51 0:00 nginx: master process nginx
sujianh+ 3767 0.0 0.0 49096 2056 ? S 09:51 0:00 nginx: worker process
sujianh+ 3775 0.0 0.0 112812 972 pts/0 S+ 09:51 0:00 grep --color=auto nginx
[sujianhui@dev529 ~]$>sudo kill -9 3766
[sujianhui@dev529 ~]$>ps aux | grep nginx
sujianh+ 3767 0.0 0.0 49096 2056 ? S 09:51 0:00 nginx: worker process
sujianh+ 3799 0.0 0.0 112812 972 pts/0 S+ 09:51 0:00 grep --color=auto nginx
apache prefork
总结
到此这篇关于php7中停止php-fpm服务的文章就介绍到这了,更多相关php7停止php-fpm服务内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!