This article mainly introduces the implementation process of PHP and related concepts, has a certain reference value, now share to everyone, the need for friends can refer to
Program Architecture
First look at what support is required to run a PHP program. The starting point is that it makes sense to start writing PHP from the programmer, so the application-level PHP script files (including Composer/include's various third-party PHP code) are required. The script file must be parsed and compiled before it can be executed, so the PHP virtual machine (usually the Zend engine) is also required. In addition, the PHP script uses several expanded functions and classes, so the expansion (including official, PECL, and user-written extensions) is almost essential. In addition to the PHP program to interact with the external (for example, from the command line to get parameters, from the Web server to obtain request information), this layer is responsible for SAPI, so SAPI is also necessary.
Summing up, the PHP program architecture from the top down to see four layers, respectively: The application layer, SAPI layer, the expansion layer and the Zend engine. See the architecture Relationship:
(Image source: Http://www.nowamagic.net/libr ... )
The SAPI layer may be relatively unfamiliar to some people. SAPI provides a unified set of interfaces that decouple upper-level applications from the actual operating environment. User-written PHP files can be executed on the command line or in Apache httpd or FPM. The support work behind it is provided by SAPI and the developer has no sense. The sapi,php script layer does not require much consideration of the execution of the specific environment, and PHP itself can let SAPI for their own characteristics to give a unique implementation.
Execution process
Aside from the differences in the implementation of each SAPI, the execution process of the PHP program can be simply summed up as follows:
Program start, Zend Engine and core component initialization;
Extended initialization (MINIT);
Receive request, expand Activation (Rinit);
Parsing and executing PHP scripts;
End of request, expand Deactivate (Rshutdown);
Offload extension (Mshutdown);
Program shutdown
In addition to 345, the remaining steps are executed only once throughout the SAPI life cycle. In CGI/CLI mode, 345 is executed only once.
Understanding the life cycle of PHP programs is an essential step in PHP, and can help developers quickly locate problems. For example, the script function does not exist, it is likely that some extension missing or loading error, in the cli/cgi mode, and then how pconnect futile, scripts run out of resources will be released, exit/die the termination is the execution of the script, does not necessarily mean the end of the process; script compiled resident memory, Rinit and Rshutdown are not repeated and are performance points for the CLI framework relative to other operating modes, and so on.
For more details on each stage of the SAPI life cycle, please refer to the "in-depth understanding of the PHP kernel" book.
CGI, FastCGI, PHP-FPM, etc.
Cgi/fastcgi/php-cgi and PHP-FPM are several concepts that make PHP developers confusing and confusing. The relationship between these concepts is as follows:
CGI/FASTCGI: The gateway protocol, which is not related to language, is not very relevant to PHP. The difference is that fastcgi can be independent of the Web server, and the program that runs the FASTCGI protocol becomes the content provider (upstream) of the Web server. In addition, after decoupling with the Web server, the process of interacting with the FASTCGI protocol has the advantages of good performance, security and stability, and distributed support; PHP-CGI: PHP parser that implements FASTCGI protocol, does not smooth restart and unloading; fpm:php The official fastcgi process Manager, The executable program is PHP-FPM, supports smooth restart, hot load, runs stably, and its management object is not php-cgi process, it doesn't matter.
Just a few concepts are easier to distinguish, in fact confusing developers with the following four groups of concepts:
Web server. Common Apache httpd and nginx;
SAPI. Common is Apache2handler, CLI, fpm-fcgi;
Agreement. The CGI and fastcgi mentioned in the text;
Program. namely php-cgi and PHP-FPM.
Because the Web server is more familiar to most people, let's talk about the relationship with other concepts: using Apache httpd, more than 90% of the situation in the module to execute PHP script, so with the Apache2handler in Sapi, Not related to other concepts (neither CGI nor fastcgi protocol); When using Nginx, 90% is forwarded to FPM through the FASTCGI protocol, so fpm-fcgi in Sapi, fastcgi in Protocol, There are three concepts related to PHP-FPM in the program, regardless of other concepts.
Summarize
This article briefly reviews the architecture and execution flow of PHP programs, and introduces several confusing concepts.
Thanks for reading, please correct me!
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!