Home  >  Article  >  Backend Development  >  PHP execution process and related concepts

PHP execution process and related concepts

不言
不言Original
2018-07-06 13:48:291648browse

This article mainly introduces the execution process and related concepts of PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it

Program architecture

Let’s first look at what support is needed to run a PHP program. The starting point of everything is that it makes sense to start writing PHP from programmers, so application-level PHP script files (including various third-party PHP codes in Composer/include) are necessary. Script files must be parsed and compiled before they can be executed, so a PHP virtual machine (usually Zend engine) is also necessary. In addition, PHP scripts will use functions and classes in multiple extensions, so extensions (including official, PECL, and user-written extensions) are almost necessary. In addition, PHP programs need to interact with the outside (such as obtaining parameters from the command line and obtaining request information from the web server). This layer is responsible for SAPI, so SAPI is also necessary.

To summarize the above, the architecture of the PHP program has four layers from top to bottom, namely: application layer, SAPI layer, expansion layer and Zend engine. The architectural relationship is shown in the figure below:

PHP execution process and related concepts

(Picture source: http://www.nowamagic.net/libr...)

The SAPI layer is Some people may be relatively unknown. SAPI provides a unified set of interfaces to decouple upper-layer applications from the actual operating environment. PHP files written by users can be executed using the command line or in Apache httpd or FPM. The support work behind it is provided by SAPI, and developers are unaware of it. Through SAPI, the PHP script layer does not need to consider too much about the specific environment of execution, and PHP itself can allow SAPI to provide unique implementations based on its own characteristics.

Execution process

Putting aside the differences in various SAPI implementations, the execution process of the PHP program can be simply summarized as follows:

  1. Program starts, Zend engine and core component initialization;

  2. Extension initialization (MINIT);

  3. Request received, extension activation (RINIT);

  4. Parse and execute the PHP script;

  5. The request ends and the extension is deactivated (RSHUTDOWN);

  6. Uninstall the extension ( MSHUTDOWN);

  7. Program shutdown

Except 345, the remaining steps will only be executed once in the entire SAPI life cycle. In CGI/CLI mode, 345 is only executed once.

Understanding the life cycle of PHP programs is an essential process for PHP advancement, and can also help developers quickly locate problems. For example, if the script reports that the function does not exist, it is very likely that an extension is missing or has a loading error; in CLI/CGI mode, no matter how pconnect is, it is in vain, and the resources will be released as soon as the script is executed; exit/die Terminates the execution of the script, which does not necessarily mean the end of the process; after the script is compiled, it will reside in the memory and will not execute RINIT and RSHUTDOWN repeatedly, which is a performance improvement point of the CLI framework compared to other operating modes; etc.

For more details on each stage in the SAPI life cycle, please refer to the book "In-depth Understanding of the PHP Core".

CGI, FastCGI, PHP-FPM, etc.

CGI/FastCGI/php-cgi and PHP-FPM are several concepts that can easily confuse and confuse PHP developers. The relationship between these concepts is as follows:

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

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

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

Just a few concepts are relatively easy to distinguish. In fact, what confuses developers is the combination of the following four groups of concepts:

  1. webserver. Common Apache httpd and Nginx;

  2. SAPI. Common ones are apache2handler, cli, fpm-fcgi;

  3. protocol. CGI and FastCGI mentioned in the article;

  4. programs. Namely php-cgi and php-fpm.

Since the web server is more familiar to most people, let’s talk about its relationship with other concepts: When using Apache httpd, more than 90% of the time, PHP scripts are executed in a module manner, so It is related to apache2handler in SAPI and has nothing to do with other concepts (neither CGI nor FastCGI protocol); when using Nginx, 90% of the time the request is forwarded to FPM through FastCGI protocol, so it is related to fpm-fcgi in SAPI and the protocol FastCGI and php-fpm in the program are related to the three concepts and have nothing to do with other concepts.

Summary

This article briefly reviews the architecture and execution process of PHP programs, and introduces several easily confused concepts.

Thanks for reading, corrections welcome!

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Analysis of PHP container Pimple running process

Advantages of using Laravel service container

The above is the detailed content of PHP execution process and related concepts. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn