This article will introduce you to the life cycle of PHP7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Definition: Server Application Programming InterfaceServer Application Programming port. It is the interface for PHP to interact with other applications. PHP scripts can be executed in many ways, through the web server, directly on the command line, or embedded in other programs. SAPI provides an interface for external communication. Common SAPIs include: cgi, fast-cgi, cli, Apache module dll, etc.
CGI is the common gateway interface (common gateway interface). It is a program. In layman's terms, CGI is like a bridge. , connects the web page and the execution program in the WEB server, it passes the instructions received by the HTML to the server's execution program, and then returns the results of the server's execution program to the HTML page. CGI is extremely cross-platform and can be implemented on almost any operating system.
When the CGI method encounters a connection request (user request), it must first create a cgi sub-process, activate a CGI process, then process the request, and end the sub-process after processing. This is the fork-and-execute pattern. Therefore, a server using CGI will have as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance. When the number of user requests is very large, a large amount of system resources such as memory, CPU time, etc. will be occupied, resulting in low performance.
fast-cgi is an upgraded version of cgi, FastCGI is like a long-live type CGI can be executed all the time. As long as it is activated, it will not take time to fork every time. PHP uses PHP-FPM (FastCGI Process Manager), the full name of PHP FastCGI Process Manager, for management.
The FastCGI process manager (IIS ISAPI or Apache Module) is loaded when the Web Server starts. The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for connections from the Web Server.
When a client request reaches the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. The web server sends CGI environment variables and standard input to the FastCGI subprocess php-cgi.
After the FastCGI sub-process completes processing, it returns standard output and error information to the Web Server from the same connection. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits for and handles the next connection from the FastCGI process manager (running in the Web Server). In CGI mode, php-cgi exits at this point.
In the above case, you can imagine how slow CGI is usually. Every web request to PHP must reparse php.ini, reload all extensions, and reinitialize all data structures. With FastCGI, all of this happens only once, when the process starts. An added benefit is that persistent database connections work.
PHP as an Apache module, the Apache server pre-generates multiple process copies to reside in the memory after the system starts. When a request occurs, these idle sub-processes are immediately used for processing, so that there is no delay caused by generating sub-processes. These server copies do not exit immediately after processing an HTTP request, but stay in the computer waiting for the next request. The response to client browser requests is faster and the performance is higher.
cli is the command line running mode of php. You often use it, but you may not notice it (for example: we Under Linux, "php -m" is often used to find out which extensions PHP has installed, which is the PHP command line running mode.
Structure:
struct _sapi_module_struct { char *name; char *pretty_name; int (*startup)(struct _sapi_module_struct *sapi_module); int (*shutdown)(struct _sapi_module_struct *sapi_module); int (*activate)(void); int (*deactivate)(void); size_t (*ub_write)(const char *str, size_t str_length); void (*flush)(void *server_context); … }
Calling API :
Called when the module starts:
int (*startup)(struct _sapi_module_struct *sapi_module);
Called when the module ends:
int (*shutdown)(struct _sapi_module_struct *sapi_module);
Called when processing the request:
int (*activate)(void);
Called after processing the request:
int (*activate)(void);
structure corresponding to CGI mode:
##CLI life cycle:structure corresponding to FPM mode:
Life cycle of FPM mode:
# Recommended learning:The above is the detailed content of What is the life cycle of PHP7. For more information, please follow other related articles on the PHP Chinese website!