PHP执行流程及相关概念

转载请注明文章出处:https://tlanyan.me/php-review-work-flow-related

PHP回顾系列目录

本文总结PHP的执行流程以及相关概念。

程序架构

先看看运行一个PHP程序需要哪些支撑。一切的起点,要从程序员开始写PHP才有意义,所以应用层面的PHP脚本文件(包括Composer/include的各种第三方PHP代码)是必须的。脚本文件要解析编译后才能执行,所以PHP虚拟机(通常是Zend引擎)也是必备的。此外PHP脚本中会使用多个拓展中的函数和类,所以拓展(包括官方、PECL、以及用户自行写的拓展)几乎也是必备的。另外PHP程序要与外部交互(例如从命令行获取参数、从web服务器获取请求信息),这一层由SAPI负责,所以SAPI也是必须的。

总结以上,PHP程序的架构从上往下看有四层,分别是:应用层、SAPI层、拓展层和Zend引擎。架构关系见下图:

php程序架构php程序架构

(图片来源:http://www.nowamagic.net/librarys/veda/detail/102

SAPI层对某些人可能相对陌生。SAPI提供一套统一的接口,让上层应用程序与实际运行环境解耦。用户写的PHP文件,可以用命令行执行,也可以在Apache httpd或FPM中执行。背后的支持工作由SAPI提供,开发人员无感知。通过SAPI,PHP脚本层无需过多考虑执行的具体环境,而PHP本身则可以让SAPI针对自己的特点给出特有实现。

执行流程

抛开各个SAPI实现上的差异,PHP程序的执行流程可以简单归结如下:

  1. 程序启动,Zend引擎和核心组件初始化;
  2. 拓展初始化(MINIT);
  3. 收到请求,拓展激活(RINIT);
  4. 解析、执行PHP脚本;
  5. 请求结束,拓展停用(RSHUTDOWN);
  6. 卸载拓展(MSHUTDOWN);
  7. 程序关闭

除345,其余几步在整个SAPI生命周期中只会执行一次。CGI/CLI模式下,345也只执行一次。

理解PHP程序的生命周期,是PHP进阶的必备过程,也能帮助开发人员快速定位问题。例如脚本报函数不存在,很有可能是某个拓展缺失或加载出错;在CLI/CGI模式下,再怎么pconnect也是徒劳的,脚本一执行完资源就释放掉;exit/die终止的是脚本的执行,不一定意味着进程的结束;脚本编译后常驻内存,不会反复执行RINIT和RSHUTDOWN,是CLI框架相对于其他运行模式的性能提升点;等等。

SAPI生命周期中各个阶段的更多细节,请参考《深入理解PHP内核》一书。

CGI、FastCGI、PHP-FPM等

CGI/FastCGI/php-cgi和PHP-FPM是几个容易让PHP开发人员困惑和混淆的概念。这几个概念的关系如下:

CGI/FastCGI:网关协议,与语言无关,所以与PHP关系也不大。两者的区别是FastCGI可以独立于web服务器,运行FastCGI协议的程序变成web服务器的内容提供方(上游)。另外与web服务器解耦后,用FastCGI协议交互的进程具有性能好、安全稳定、支持分布式等优点;

php-cgi:实现FastCGI协议的PHP解析器,不能平滑重启和热加载;

FPM:PHP官方的FastCGI进程管理器,可执行程序为php-fpm;支持平滑重启、热加载,运行稳定;其管理对象不是php-cgi进程,两者没什么关系。

仅是几个概念比较容易容易区分,实际上混淆开发人员的是以下四组概念的综合:

  1. web服务器。常见的Apache httpd和Nginx;
  2. SAPI。常见的是apache2handler、cli、fpm-fcgi;
  3. 协议。文中提到的CGI和FastCGI;
  4. 程序。即php-cgi和php-fpm。

由于web服务器对大多数人更熟悉,拿之说一下与其他概念的关系:使用Apache httpd时,90%以上的情况以模块方式执行PHP脚本,所以与SAPI中的apache2handler有关,与其他概念无关(既不是CGI也不是FastCGI协议);使用Nginx时,90%的情况是通过FastCGI协议将请求转发到FPM,所以与SAPI中的fpm-fcgi、协议中的FastCGI、程序中的php-fpm三个概念有关,与其他概念无关。

总结

本文简要回顾了PHP程序的架构和执行流程,并对几个容易混淆概念做了介绍。

感谢阅读,欢迎指正!

参考

  1. http://www.php-internals.com/book/?p=chapt02/02-00-overview
  2. http://www.nowamagic.net/librarys/veda/detail/102
  3. http://cuishan.win/2017/02/05/cgi-fastcgi-php-cgi-php-fpm/
  4. http://www.mike.org.cn/articles/what-is-cgi-fastcgi-php-fpm-spawn-fcgi/
  5. https://www.kancloud.cn/nickbai/php7/363265
  6. http://php.net/manual/en/install.fpm.php
留言评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select * from vps_autoblog_queue where is_running >0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=6 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=6 and source_url_key='85f8e27ce55bd78fd33f47d2b1409198'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(6,'https://www.hijk.pw/post-sitemap.xml','85f8e27ce55bd78fd33f47d2b1409198',1750368303,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=4 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=4 and source_url_key='54b1dccf98c13c81f546faf82132bc77'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(4,'https://ssr.tools/sitemap-posttype-post.2020.xml','54b1dccf98c13c81f546faf82132bc77',1750368303,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=2 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=2 and source_url_key='91372ddcd9e4809b115feaa5b6f00815'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(2,'https://tlanyan.me/post-sitemap.xml','91372ddcd9e4809b115feaa5b6f00815',1750368303,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=7 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=7 and source_url_key='569e17863f7693f1afa9e9256af9dcff'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(7,'https://aisoa.cn/sitemap.xml','569e17863f7693f1afa9e9256af9dcff',1750368303,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=9 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=9 and source_url_key='cc9416f8df40d8c3a48e66856b1cd6fd'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(9,'https://www.v2rayssr.com/sitemap.xml','cc9416f8df40d8c3a48e66856b1cd6fd',1750368303,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=10 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=10 and source_url_key='74714f67855820f0c701ce905b231f27'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(10,'https://doubibackup.com/sitemap.xml','74714f67855820f0c701ce905b231f27',1750368303,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,is_running,last_post_time,post_interval,not_check_stoped from vps_autoblog_queue WHERE is_running>0 and fetched=0 order BY id ASC

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,sn_id from vps_autoblog_queue WHERE is_running=0 and fetched=0 order BY id ASC LIMIT 1