LNMP concurrency considerations, resource allocation (core configuration of the PHP-FPM process Manager)

Source: Internet
Author: User
Tags fpm php and php script server memory

Here's a summary of some of the most frequently asked questions in recent hires
Phper when asked about your program performance? How much can the concurrency of the program achieve? Where is the bottleneck of the program? How many servers should be purchased to meet business requirements? How many units does the PHP application server need in load balancing?
Perhaps these problems in the interview will be set up an application of the scene and some prerequisites, so that the interviewer to design, and put forward suggestions, can answer very good people or relatively few.
Today we'll talk about LNMP concurrency and resource allocation. First, figure out a few concepts.
N in Lnmp is Nginx acting as a web Server
The distributor of the content, who finds the appropriate file in the file system, returns it to the browser, such as Nginx. If it is a static file, it can be returned directly, but if it is the index.php script file that needs to be parsed and executed, the Web server is powerless, and the request is forwarded to the corresponding scripting language parser to interpret and execute, eventually returning the execution result of the program to the Web server. and return to the browser.
P in LNMP is the logical handler for PHP to act as a backend
So how is the regular collaboration between PHP and Nginx? We need to define a few concepts.
Cgi
A common Gateway interface, described in the HTTP protocol, that communicates between Web server and back-end handler processes
php-cgi
PHP implementation of the CGI protocol, so that Web server and PHP together to complete a Dynamic Web page request response
FastCGI
is to solve CGI performance problems, and another protocol to the specification, why to solve the CGI performance problem, because in the face of the business requirements of large and medium-sized Web sites, the CGI program has become more and more powerless, because the CGI program every time you receive a request to start a new process, and initialization of the environment, and then execute the program, The specific content of the agreement is not quoted here.
php-fpm
Implementation of the FASTCGI protocol, is a php-cgi process manager, to solve the high concurrent Web site performance problems.
There are several concepts that need to be clarified in the final answer to LNMP's concurrent consideration and resource allocation
Concurrent
Typically measured by the number of requests completed within the unit, such as the number of transactions per second (TPS), the number of HTTP requests per second (HPS), and the number of queries per second (QPS). Normally, we say that PHP concurrency is the number of dynamic requests that PHP completes in a second. If a site peak dynamic request concurrency of 5000 per second, this number is not too high, but also not low. The average number of active users in the 10 million-50 million web site applications to achieve this level.
Performance
Generally refers to the processing speed of the application, if the PHP application, open a page (execute a script program) usually need to complete in the 50-100ms, this performance requirements for the program is still relatively high. But this is only the process of processing, PHP processing is completed, but also to the Web Server,web server to return data to the browser, there will be a network delay, usually network normal situation, need about 100ms, The final request for a Dynamic Web page is approximately 200ms (ideally) accessible to the user's browser (just an HTML structure).
Resource allocation
1) PHP-FPM Process number
As described above, concurrency of 5000 per second, each request completed about 200MS (specific page to be specific analysis, here is only an ideal value), if only 5 PHP application server, then each machine on average concurrent 1000 per second, if the use of NGINX+PHP-FPM architecture, What about the configuration of the PHP-FPM php-cgi process Manager? The result of my calculation is (specific configuration item description in the following text):
Pm=static
pm.max_children=100
The above 100 is how to get, because the machine average concurrency of 1000 per second, each dynamic request processing time is 100ms, that is to say 1 php-fpm worker processing process in 1 seconds can handle 10 requests, 100 PHP-FPM worker processing process, 1000 requests can be processed.
Of course, the need to combine server hardware resources to configure, if improperly configured, it is easy to peak in the request or traffic surges caused server downtime.
2) network bandwidth
Network bandwidth will also be an important factor, if your service processing is very strong, but the user's request and response can not arrive in time is also white busy, this parameter how to calculate?
Concurrent 5000 per second, output of each request is 20K, then 5000x20k=100000k=100m
This requires your public network Load equalizer extranet bandwidth at least to reach 100M
3) Memory
In the above 100 php-fpm worker processing process, theoretically if the server only runs PHP-FPM, then we can allocate half of the server memory to PHP-FPM, usually we can think that a PHP-FPM worker process takes up memory 20M , then 100x20m=2g, which means that the server's memory is approximately 4G
4) CPU
Because PHP-FPM is a multi process model application, CPU process scheduling consumption is also very large, and PHP application problems will lead to high CPU occupancy rate, there is no quantitative indicators, the need for concrete analysis of the situation. But there is a small suggestion that you can deploy a crontab every minute to detect the CPU occupancy rate more than how to kill the corresponding PHP-FPM worker processing process.
5) Nginx and php-fpm use UNIX domain sockets instead of TCP Socke for communication
This configuration is very critical, pure echo AB test, using UNIX domain sockets per second to increase the number of requests 10%-20%
Configuration in Nginx:
Fastcgi_pass Unix:/data/server/var/php/php-fpm.sock;
Configuration in php-fpm.conf:
Listen =/data/server/var/php/php-fpm.sock
Finally encountered a lot of students on the PHP-FPM Process Manager's core configuration is not very familiar with, the following is my translation of the configuration instructions:
First, the PHP-FPM related configuration items are:
1) PM
Process Manager to control the number of child processes, the possible values are
Static a fixed value, specified by Pm.max_children
Dynamic (works in the same way as Apache's prefork mode), but keep at least one, by
Pm.max_children the maximum number of processes at the same time
Pm.start_servers the number of processes on which the wait request came on when PHP-FPM started
Pm.min_spare_servers the minimum number of processes to run in idle state, if less than this value, creates a new process
Pm.max_spare_servers the maximum number of processes running in idle state, if greater than this value, will kill part of the process
OnDemand does not create a process at startup and creates a subprocess process request when the request is reached
Pm.max_children the maximum number of processes at the same time
Pm.process_idle_timeout the process will be killed after a few seconds of idle
PM = dynamic
2) Pm.max_children
Maximum number of processes at the same time
Pm.max_children = 120
3) Pm.start_servers
PHP-FPM the number of processes on which waiting requests come on at startup, the default is: Min_spare_servers + (max_spare_servers-min_spare_servers)/2
Pm.start_servers = 80
4) Pm.min_spare_servers
The minimum number of processes running in idle state, if less than this value, creates a new process
Pm.min_spare_servers = 60
5) Pm.max_spare_servers
The maximum number of processes running in idle state, if greater than this value, will kill part of the process
Pm.max_spare_servers = 120
6) Pm.process_idle_timeout
The process is killed after how many seconds are idle, and the default is 10s
Pm.process_idle_timeout = 10s
7) pm.max_requests
Automatic termination of the number of requests processed by each process can effectively prevent a memory overflow, if 0 will not automatically terminate, the default is 0
Pm.max_requests = 5000
8) Pm.status_path
Registered URI to show statistics for the PHP-FPM status
Pm.status_path =/status
Where the Statistics page information is:
Pool Process Pools Name
Process Manager Processes manager name (static, dynamic or OnDemand)
Start Time php-fpm startup times
The total number of seconds that start since PHP-FPM started
Accepted conn The number of requests received by the current process pool
Number of requests for listen queue waiting queues
Max Listen Queue The maximum number of requests queued since startup
Listen queue Len waiting for connection socket queue Size
Idle processes the number of currently idle processes
Active processes the number of processes active
Total processes number of processes (idle+active)
Max Active processes the maximum number of processes active since startup
Max children reached maximum number of processes
9) Ping.path
Ping URL, which can be used to test whether PHP-FPM is alive and can respond
Ping.path =/ping
Ping.response)
The response body of the ping URL
Ping.response = Pong

Related Article

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.