The article about PHP optimization is often to teach you how to write efficient code, this article intends to discuss the problem from another perspective, teach you how to configure an efficient environment, so the same can achieve the goal of optimization.
Pool
One frustrating message is that most PHP programmers ignore the value of the pool. The pool mentioned here is not a database connection pool, but a process pool, PHP allows to start multiple pools at the same time, each pool uses a different configuration, each pool respect each other's sovereign territorial integrity, non-interference in internal affairs.
-pool
What good is it? By default, PHP only has one pool enabled, and all requests are executed in this pool. Once some requests appear congestion and so on, it is likely to cause the whole pool to burn Chibi of the end; If you enable multiple pools, you can classify requests into different pools, and if some of the requests are congested, it will only affect the pool in which you are located, thus controlling the extent of the failure.
Listen
Although Nginx and PHP can be deployed on different servers, but in practical applications, most people are accustomed to deploying them on the same server, so there are two choices: one is TCP and the other is a Unix Socket.
-listen
Compared to TCP, Unix sockets omit some links such as the TCP three handshake, so it is relatively more efficient, but it is important to note that when using a UNIX socket, because there is no TCP corresponding reliability guarantee mechanism, it is best to put the backlog and Somaxcon n set larger, otherwise unstable when facing high concurrency.
Pm
Process management has dynamic and static points. Dynamic mode typically starts a small number of processes first, and then adjusts the number of processes in real time, depending on the number of requests. The advantages are obvious: saving resources; Of course, its drawbacks are obvious: once high concurrent requests occur, the system will have to rush to FORK the new process, which will inevitably affect performance. Corresponding, the static mode of the one-time FORK of the process, regardless of the amount of demand will remain unchanged. While static mode consumes more resources than dynamic mode, it does not need to perform expensive FORK in the face of high concurrent requests.
-pm
For large traffic sites, static mode is undoubtedly the best choice unless server resources are tight.
Pm.max_children
How many PHP processes are appropriate to start? Before you give your answer, take a look at the following article:
Some misunderstandings of PHP-FPM's Max_chindren
Should PHP Workers always Equal number of CPUs
A CPU can only process one request at a time. When the number of requests is greater than the number of CPUs, the CPU will divide the time slices, take turns to execute each request, since it involves the scheduling of multiple tasks, then the context switch will inevitably consume a portion of performance, in this sense, the number of processes should be equal to the number of CPUs, so that each process corresponds to a dedicated CPU, The efficiency of the context switching loss can be minimized. However, this conclusion only when the request is CPU-intensive is correct, and for the general Web request, mostly IO-intensive, at this point, this conclusion is debatable, because the database query, such as the presence of IO, will inevitably lead to CPU has a considerable portion of time in the WAIT state, that is wasted state. At this point, if the number of processes is more than the number of CPUs, then when IO occurs, the CPU has the opportunity to switch to another request to continue execution, although this will lead to a certain context switching overhead, but always better than the card in the WAIT state.
How much does that fit? To sort this out, we need to focus on the memory situation in addition to the CPU:
-php Memory
The memory-related columns in the results of the top command as shown above are VIRT,RES,SHR respectively. Where VIRT represents the theoretical value of memory consumption, usually do not care about it, RES represents the actual memory consumption, although Res looks very large, but contains shared memory, which is the SHR display value, so a single PHP process is actually independent of the size of the memory is equal to "res? shr", generally is 10M up and down. In this calculation, theoretically 1G memory can support about 100 PHP processes, 10G memory can support about 1000 PHP processes. Of course, it is not rude to think that the more the better, the best combination of PHP's status interface, by monitoring the number of active connections to adjust.