php-fpm is a master (main)/worker (child) multi-process architecture, which is somewhat similar to the nginx design style. The master process is mainly responsible for CGI and PHP environment initialization, event monitoring, sub-process status, etc., and the worker process is responsible for processing PHP requests.
Before introducing the operating principle, let us first understand its several operating modes.
Running mode
php-fpm supports three running modes, namely static, ondemand, and dynamic. The default is dynamic.
static: Static mode, a fixed worker process is allocated at startup.
ondemand: Allocate on demand, fork the worker process when a user request is received.
dynamic: Dynamic mode, a fixed process is allocated at startup. As the number of requests increases, the worker process is adjusted within the set floating range.
These three modes each have their own merits, and you can adjust the corresponding configuration according to different environments.
The following is the topic of this article, focusing on the operating principle of php-fpm.
Related recommendations: "PHP Getting Started Tutorial"
Operation Principle
php-fpm is designed with master/worker architecture. The functions of the master and worker process modules are briefly described above. The operating principles of these two modules will be explained in detail below.
master process
The master process workflow is divided into 4 stages, as shown below:
1. cgi initialization phase: call the fcgi_init() and sapi_startup() functions respectively, register process signals and initialize the sapi_globals global variable.
2. PHP environment initialization phase: triggered by cgi_sapi_module.startup. The php_cgi_startup function is actually called, and php_cgi_startup internally calls php_module_startup for execution.
Main functions of php_module_startup:
a). Load and parse php configuration;
b). Load php module and record it in the function symbol table (function_table);
c). Load zend extension;
d). Set disabled functions and class library configuration;
e). Register memory recovery method;
3. php -fpm initialization phase: execute the fpm_init() function. Responsible for parsing the php-fpm.conf file configuration, obtaining process-related parameters (the maximum number of files allowed to be opened by the process, etc.), initializing the process pool and event model and other operations.
4. php-fpm running phase: execute the fpm_run() function, and the main process will be blocked after running. This phase is divided into two parts: fork child process and loop event.
The fork child process part is handled by the fpm_children_create_initial function (Note: ondemand mode is created in the fpm_pctl_on_socket_accept function).
The loop event part is processed through the fpm_event_loop function, which is an infinite loop inside and is responsible for the collection of events.
Worker process
The worker process is divided into three stages: receiving client requests, processing requests, and completing requests.
#1. Receive client request: Execute the fcgi_accept_request function, which internally obtains the client request by calling the accept function.
//请求锁 FCGI_LOCK(req->listen_socket); req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len); //释放锁 FCGI_UNLOCK(req->listen_socket);
From the above code, you can notice that there is a request lock operation before accept. This design is to avoid the "shock" phenomenon in the request. Of course, this is an optional option and the feature can be disabled.
2. Processing request phase: First, call fpm_request_info and php_request_startup respectively to obtain the request content and register global variables ($_GET, $_POST, $_SERVER, $_ENV, $_FILES); then call php_fopen_primary_script to access according to the request information Script file; finally handed over to php_execute_script for execution. php_execute_script internally calls the zend_execute_scripts method to hand the script over to the zend engine for processing.
3. Request end phase: execute the php_request_shutdown function. At this time, call back the registered function and __destruct() method of register_shutdown_function, send response content, release memory and other operations.
Summary
php-fpm adopts the master/worker architecture design. The master process is responsible for the initialization and event monitoring operations of CGI and PHP public environments. The worker process is responsible for request processing functions. When the worker process processes the request, there is no need to initialize the PHP running environment again, which is one of the reasons for the excellent performance of php-fpm.
The above is the detailed content of How does php-fpm work?. For more information, please follow other related articles on the PHP Chinese website!