在PHP生命周期的各个阶段,一些与服务相关的操作都是通过SAPI接口实现。

各个服务器抽象层之间遵守着相同的约定,这里我们称之为SAPI接口。

在PHP的源码中,当需要调用服务器相关信息时,全部通过SAPI接口中对应的方法调用实现
php-fpm + nginx
php + terminal

PHP常见的四种运行模式
SAPI(Server Application Programming Interface)服务器应用程序编程接口,即PHP与其他应用交互的接口.
每个SAPI实现都是一个_sapi_module_struct结构体变量。

PHP脚本要执行有很多方式,通过Web服务器,或者直接在命令行下,也可以嵌入在其他程序中。

SAPI提供了一个和外部通信的接口,常见的SAPI有:cgi、fast-cgi、cli、isapi apache模块的DLL

ISAPI模式 (eg Apache : apache2handler mode ) 以web服务器的一个模块加载运行,其实就是将PHP的源码与webServer的代码一起编译,运行时是同一个进程,共享同一个地址空间. 例如 LAMP中,PHP就是作为Apache的一个模块运行的.Apache是多线程调用php模块的.(same as IIS)
CGI模式 fork-and-execute webServer将动态请求转发到CGI程序(以php为例子),就相当于fork一个子进程,然后exec(php process),用CGI程序来解释请求内容,最后将子进程的output返回.此时webServer与php进程的地址空间是独立的.此时的php是作为一个独立的程序运行.
FastCGI模式 这种形式是CGI的加强版本,CGI是单进程,多线程的运行方式,程序执行完成之后就会销毁,所以每次都需要加载配置和环境变量(创建-执行)。
而FastCGI则不同,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 not change
kill -USR2 “php-fpm master pid” 平滑重载所有php-fpm进程,执行完毕后你会发现php-fpm master/worker进程id have changed.
kill -KILL/-9 php-fpm-master.pid , 强制杀死master进程,该信号不允许中断/阻塞,此时master进程无法通知回收worker进程,所以此时worker进程仍然监听port,仍然可以正常处理http请求.
kill -INT/-QUIT/-TERM master pid , stop php-fpm service 信号被当前进程树接收到.也就是说,不仅当前进程会收到信号,它的子进程也会收到.
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

  1. [sujianhui@dev529 ~]$>sudo kill -USR1 17000
  2. [sujianhui@dev529 ~]$>ps aux | grep php-fpm
  3. root 17000 0.0 0.0 243220 7208 ? Ss 17:00 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
  4. sujianh+ 17001 0.0 0.0 245304 7072 ? S 17:00 0:00 php-fpm: pool www
  5. sujianh+ 17002 0.0 0.0 245304 7072 ? S 17:00 0:00 php-fpm: pool www
  6. sujianh+ 17105 0.0 0.0 112816 972 pts/3 S+ 17:01 0:00 grep --color=auto php-fpm
  7. [sujianhui@dev529 ~]$>sudo kill -USR2 17000
  8. [sujianhui@dev529 ~]$>ps aux | grep php-fpm
  9. root 17122 0.0 0.0 243220 7212 ? Ss 17:01 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
  10. sujianh+ 17123 0.0 0.0 245304 7072 ? S 17:01 0:00 php-fpm: pool www
  11. sujianh+ 17124 0.0 0.0 245304 7072 ? S 17:01 0:00 php-fpm: pool www
  12. sujianh+ 17126 0.0 0.0 112816 976 pts/3 S+ 17:01 0:00 grep --color=auto php-fpm
  13. [sujianhui@dev529 ~]$>pstree 17122 -a
  14. php-fpm
  15. ├─php-fpm
  16. └─php-fpm
  17. [sujianhui@dev529 ~]$>sudo kill -INT 17122
  18. [sujianhui@dev529 ~]$>ps aux | grep php-fpm
  19. sujianh+ 17229 0.0 0.0 112816 976 pts/3 S+ 17:03 0:00 grep --color=auto php-fpm

so we should use sudo kill -INT master.pid to kill php-fpm service.

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

  1. [sujianhui@dev0529 sbin]$>sudo kill -9 4562
  2. [sujianhui@dev0529 sbin]$>ps aux | grep nginx
  3. sujianh+ 4563 0.0 0.0 49128 2088 ? S 21:46 0:00 nginx: worker process
  4. sujianh+ 4612 0.0 0.0 112812 972 pts/0 S+ 21:46 0:00 grep --color=auto nginx
  5. [sujianhui@dev0529 sbin]$>kill -9 4563
  6. [sujianhui@dev0529 sbin]$>ps aux | grep nginx
  7. 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

  1. [sujianhui@dev529 ~]$>sudo nginx
  2. [sudo] password for sujianhui:
  3. [sujianhui@dev529 ~]$>ps aux | grep nginx
  4. root 3628 0.0 0.0 46600 1052 ? Ss 09:49 0:00 nginx: master process nginx
  5. sujianh+ 3629 0.0 0.0 49096 2056 ? S 09:49 0:00 nginx: worker process
  6. sujianh+ 3637 0.0 0.0 112812 972 pts/0 S+ 09:49 0:00 grep --color=auto nginx
  7. [sujianhui@dev529 ~]$>sudo kill -SIGTERM 3628
  8. [sujianhui@dev529 ~]$>ps aux | grep nginx
  9. sujianh+ 3744 0.0 0.0 112812 972 pts/0 S+ 09:50 0:00 grep --color=auto nginx
  10. [sujianhui@dev529 ~]$>sudo nginx
  11. [sujianhui@dev529 ~]$>ps aux | grep nginx
  12. root 3766 0.0 0.0 46600 1052 ? Ss 09:51 0:00 nginx: master process nginx
  13. sujianh+ 3767 0.0 0.0 49096 2056 ? S 09:51 0:00 nginx: worker process
  14. sujianh+ 3775 0.0 0.0 112812 972 pts/0 S+ 09:51 0:00 grep --color=auto nginx
  15. [sujianhui@dev529 ~]$>sudo kill -9 3766
  16. [sujianhui@dev529 ~]$>ps aux | grep nginx
  17. sujianh+ 3767 0.0 0.0 49096 2056 ? S 09:51 0:00 nginx: worker process
  18. sujianh+ 3799 0.0 0.0 112812 972 pts/0 S+ 09:51 0:00 grep --color=auto nginx

更多相关文章

  1. Ashmem(Android共享内存)使用方法和原理
  2. Dalvik虚拟机是如何执行程序的
  3. Android内存管理
  4. Android, property 添加写权限。
  5. Android菜鸟的成长笔记(17)—— 再看Android中的Unbounded Service
  6. Android(安卓)Activity属性介绍
  7. 浅谈并发处理PHP进程间通信之System V IPC
  8. Android(安卓)LCD(二):LCD常用接口原理篇
  9. Jmeter通过OS进程取样器调用Python脚本实现参数互传

随机推荐

  1. Android RelativeLayout 实现顶部左中右
  2. android framework
  3. android EditText 去除边框
  4. Basic4android (Basic for Android) - Ra
  5. Android探索之旅(第二十一篇)Android中事件
  6. 小米2及其他Android手机无法连接mac解决
  7. android TextView显示文字和图片
  8. Android中dip, dp, px, sp之间的区别
  9. Android源码下载
  10. 2013.05.16——— android 关于listview