Schema of the PHP fastcgi process Manager php-fpm

Source: Internet
Author: User
Tags connection pooling epoll fpm php file upload php source code upload php server memory

A master process that supports multiple pool, each of which listens to a different port by the master process, with multiple worker processes in the pool.
Each worker process has a built-in PHP interpreter, and the process resides in the background, supporting the prefork dynamic increase.
Each worker process supports compiling scripts at run time and caching the generated opcode in memory to improve performance.
Each worker process supports a configuration response that automatically restarts after a specified number of requests, and the master process restarts the suspended worker process.
Each worker process maintains a persistent connection to Mysql/memcached/redis, implements a "connection pool", avoids duplication of connections, and is transparent to the program.
You should set a fixed number of worker processes when using a database persistent connection, and do not use the dynamic prefork mode.

After @syaokun219 and @IM xin Ye corrected, the following two sentences are wrong:
The master process uses the Epoll model to receive and distribute requests asynchronously, listen the listening port, epoll_wait waits for a connection.
Then distributed to the worker process in the pool, the worker process Accpet the Request poll processing the connection.
Should be:
The master process does not receive and distribute the request, but the worker process poll processing directly after the Accpet request.

after viewing the source code structure, PHP has supported the Epoll model, epoll,accept after listening poll
The master process constantly calls Epoll_wait and getsockopt to handle signal events and timer events asynchronously.
In this case, Nginx is similar, the master process does not process the request, but the worker process directly processing,
But the difference is that the Nginx worker process is epoll asynchronous processing request, and PHP-FPM is still poll.

If the worker process is not sufficient, the master process prefork more processes.
If the prefork reaches the Pm.max_children limit, the worker process is all busy,
The master process then suspends the request to the connection queue backlog (the default value is 511).

1 PHP-FPM worker processes can handle only 1 requests at a time.
MySQL Maximum number of connections max_connections default is 151.
As long as the number of PHP-FPM work processes is not more than 151, there will be no connection to the MySQL situation.
And under normal circumstances, there is no need to open so many php-fpm work processes,
For example, 4 PHP-FPM processes can run over 4 cores of CPUs,
So you're driving 40 php-fpm and it doesn't make any sense,
It only consumes more memory, resulting in more CPU context switching and worse performance.
To reduce the overhead of repeatedly establishing and releasing connections for each request, you can turn on persistent connections,
A php-fpm process keeps a long connection to MySQL, enabling transparent "connection pooling".

Nginx and PHP-FPM separate, in fact, is very good decoupling, PHP-FPM specifically responsible for processing PHP requests, a page corresponding to a PHP request,
The request of all static resources in the page is handled by Nginx, so the dynamic and static separation is realized, and Nginx is best at dealing with high concurrency.
PHP-FPM is a multi-process fastcgi service, similar to the Apache Prefork process model,
This model is very efficient and stable for handling only PHP requests.
Unlike Apache (, a page that handles multiple requests, including images, style sheets, JS scripts, PHP scripts, and so on.

PHP-FPM has entered the PHP source code skeleton since 5.3, and the previous version did not have PHP-FPM.
The spawn-fcgi at that time was a fastcgi process manager that needed to call php-cgi,
Another PHP manager like Apache mod_fcgid and IIS needs to call the php-cgi process.
But PHP-FPM does not rely on php-cgi at all, runs completely independently, and does not rely on the PHP (CLI) command-line interpreter.
Because PHP-FPM is a fastcgi service with a built-in PHP interpreter, it can read php.ini configuration and php-fpm.conf configuration on its own at startup.

Personally, the number of PHP-FPM work processes, set to twice times the CPU core number is sufficient.
After all, Nginx and MySQL and the system also consume CPU.
It is very unreasonable to set the number of PHP-FPM processes based on server memory.
allocating memory to the Mysql,memcached,redis,linux disk cache (Buffers/cache) These services are clearly more appropriate.
Excessive PHP-FPM processes increase the overhead of CPU context switching.
The PHP code should try to avoid curl or file_get_contents the code that could lead to longer network I/O time.
Note Set the Curlopt_connecttimeout_ms timeout to prevent the process from being blocked for long periods of time.
If you want to perform long-time tasks asynchronously, you can Pclose (Popen ('/path/to/task.php & ', ' R ')); Open a process to process,
Or with the help of Message Queuing, you should try to avoid blocking to the PHP-FPM worker process.
Set Request_slowlog_timeout to 1 seconds in php-fpm.conf to see if there is a code that takes more than 1 seconds in Slowlog.
Optimizing the code to offload all PHP-FPM work processes is the fundamental way to improve performance.

Operations that allow the CPU to run at full capacity can be considered CPU-intensive operations.
Uploads and downloads are typical I/O intensive operations, because time-consuming occurs primarily in network I/O and disk I/O.
Download operations that require PHP authentication can be delegated to the Nginx AIO thread pool:
Header ("X-accel-redirect: $file _path");
As for upload operations, such as the ability to create a pool of php-fpm processes called upload that listens on port 9001,
Specifically responsible for handling upload operations (through Nginx distribution), to avoid blocking the upload operation to listen to the 9000 port of the compute-intensive WWW process pool.
At this time upload process pool More open point process also does not matter.
Location =/upload.php {
Include Fastcgi_params;
Fastcgi_param script_filename $document _root$fastcgi_script_name;
Listen =
PM = static
Pm.max_children = 4
Listen =
PM = dynamic
Pm.max_children = 8
Pm.start_servers = 4
Pm.min_spare_servers = 4
Pm.max_spare_servers = 4
Where IO-intensive this process pool [IO] uses a dynamic prefork process, such as here is 8 busy, 4 idle.
The isolation of the pools provided by the PHP-FPM, separating compute dense and I/O intensive operations, can reduce the impact of blocking on the entire PHP application.

if (Isset ($_post[' submit ')) {
Header (' Content-type:text/plain; Charset=utf-8 ');
chmod 777 Uploads
Move_uploaded_file ($_files[' upload_file ' [' tmp_name '], ' uploads/'. $_files[' upload_file ' [' name ']);
Print_r ($_files[' upload_file ');
Exit ();
} else {
Header (' content-type:text/html; Charset=utf-8 ');
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title>php File Upload Test </title>
<!--enctype= "Multipart/form-data" transmits data in binary format post--
<form action= "<?php Echo pathinfo (__file__) [' basename '];?>" method= "POST" enctype= "Multipart/form-data" >
<div> file 1 <input type= "file" Name= "Upload_file"/></div>
<div><input type= "Submit" name= "Submit" value= "Submission"/></div>
Nginx and PHP-FPM's work process only open 1 each.
Upload images in 2KB per second:
Time Trickle-s-U 2 curl \
-F "action=info.php" \
-F "[Email protected];type=image/jpeg" \
-F "submit= commit" \
sudo netstat-antp|egrep "CURL|NGINX|FPM"
It is found that only nginx and curl are in the established state, and Nginx and FPM are not blocked.
Top-p 4075 visible nginx single thread.
sudo strace-p 4075 visible nginx call Recvfrom receive data and Pwrite save data.
sudo strace-p 13751 visible php-fpm is the data that is obtained when Nginx receives the data uploaded by the user.
That being the case, I envision another open PHP-FPM process pool handling upload operations is not too much of a use.
In the process of file upload php-fpm will not be blocked, because Nginx received the uploaded content only one-time to PHP-FPM.
Attached: Download images in 2KB per second
Time Trickle-s-D 2 \
wget Http://


Schema of the PHP fastcgi process Manager php-fpm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.