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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use