Architecture of PHPFastCGI process manager PHP-FPM

Source: Internet
Author: User
: This article mainly introduces the PHPFastCGI process manager PHP-FPM architecture, for PHP tutorials interested in students can refer.

A master process supports multiple pools. each pool listens to different ports from the master process. The pool has multiple worker processes.
Each worker process has a built-in PHP interpreter, and the process is resident in the background, supporting dynamic addition of prefork.
Each worker process supports compiling scripts at runtime and caching the generated opcode in the memory to improve performance.
Each worker process supports automatic restart after a specified number of requests are configured to respond, and the master process restarts the crashed worker process.
Each worker process can maintain a persistent connection to MySQL, Memcached, and Redis to implement a "connection pool". This avoids repeated connections and is transparent to the program.
When using persistent database connection, you should set a fixed number of worker processes. do not use the dynamic prefork mode.
The master process uses the epoll model to asynchronously receive and distribute requests, the listen listening port, and epoll_wait waiting for connection,
Then it is distributed to the worker process in the corresponding pool. after the worker process accpet requests, poll processes the connection,
If the worker process is not enough, the master process will prefork more processes,
If the prefork reaches the pm. max_children limit, all worker processes are busy,
The master process suspends the request to the backlog of the connection queue (default value: 511 ).
One PHP-FPM worker can process only one request at a time.
The maximum number of connections of MySQL max_connections is 151 by default.
As long as the number of PHP-FPM worker processes does not exceed 151, there will be no connection to MySQL.
And under normal circumstances, do not need to open so many PHP-FPM work process,
For example, four PHP-FPM processes can run full 4 core CPU,
So it doesn't make sense for you to open 40 PHP-FPM processes,
It only occupies 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 enable persistent connections,
A PHP-FPM process maintains a persistent connection to MySQL for transparent "connection pool ".
Nginx and PHP-FPM separated, in fact, is a good decoupling, PHP-FPM is responsible for processing PHP requests, a page corresponds to a PHP request,
Requests for all static resources on the page are processed by Nginx, which achieves static/dynamic separation. Nginx is best at processing high concurrency.
PHP-FPM is a multi-process FastCGI service, similar to the Apache prefork process model,
For PHP requests only, this model is efficient and stable.
Unlike Apache (libphp. so), a page must process multiple requests, including images, style sheets, JS scripts, and PHP scripts.
Php-fpm enters the PHP source code trunk from 5.3. the previous version does not have php-fpm.
At that time, spawn-fcgi was a FastCGI Process Manager that needed to call php-cgi,
In addition, PHP Manager for Apache mod_fcgid and IIS also needs to call the php-cgi process,
However, php-fpm does not rely on php-cgi at all, and runs completely independently, nor does it rely on the php (cli) command line interpreter.
Because php-fpm is a FastCGI service with built-in php interpreter, you can read php. ini configuration and php-fpm.conf configuration at startup.
I think, the number of PHP-FPM worker process, set to 2 times the number of CPU core is enough.
After all, Nginx and MySQL and the system consume the same CPU.
Setting the number of PHP-FPM processes based on the server memory is unreasonable,
Allocating memory to MySQL, Memcached, Redis, and Linux disk cache services is obviously more suitable.
Too many PHP-FPM processes increase the overhead of CPU context switching.
In PHP code, try to avoid code that may generate long network I/O time using curl or file_get_contents.
Note: Set the CURLOPT_CONNECTTIMEOUT_MS timeout time to prevent the process from being blocked for a long time.
If you want to asynchronously execute a time-consuming task, you can use pclose (popen ('/path/to/task. php &', 'r') to open a process for processing,
Or with the help of message queue, in short is to try to avoid blocking to the PHP-FPM working process.
Set request_slowlog_timeout to 1 second in the php-fpm.conf and check whether there is code that takes more than 1 second in the slowlog.
Optimization code, can reduce the burden for all PHP-FPM workers, this is the fundamental way to improve performance.
Operations that allow the CPU to run at full capacity can be considered as CPU-intensive operations.
Upload and download are typical I/O-intensive operations, because the time consumed mainly occurs in network I/O and disk I/O.
Download operations that require PHP authentication can be delegated to the AIO thread pool of Nginx:
Header ("X-Accel-Redirect: $ file_path ");
For upload operations, for example, you can create a PHP-FPM process pool (pool) that listens to Port 9001 named upload ),
It is responsible for processing Upload operations (distribution through Nginx) to prevent upload operations from being congested to the computing-intensive www process pool listening to Port 9000.
At this time, the upload process pool does not matter if multiple processes are enabled:
[Www]
Listen = 127.0.0.1: 9000
Pm = static
Pm. max_children = 4
[Upload]
Listen = 127.0.0.1: 9001
Pm = dynamic
Pm. max_children = 8
Pm. start_servers = 4
Pm. min_spare_servers = 4
Pm. max_spare_servers = 4
With the isolation of the pool provided by the PHP-FPM, separating computing intensive and I/O intensive operations can reduce the impact of blocking on the entire PHP application.

The article was originally published by the open-source Chinese blog eechen.

The above introduces the PHP FastCGI Process Manager PHP-FPM architecture, including the content, hope to be interested in PHP Tutorial friends help.

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: info-contact@alibabacloud.com 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.