PHP-FPM optimization by copying a large amount ofpm.max_childrenThe pool must balance concurrency, each worker's memory, CPU, request time, and database capacity. The worker creates low-levels; the worker consumes too much RAM, the CPU cache is busy and dependencies are saturated.
Quick answer:First measure the dynamic request rate, PHP time, queue, and actual worker size. Then select the right process manager and set the worker ceiling within the RAM/CPU budget. Link the slow request to the URL and Query; validate the configuration and implement the change step by step with rollback and monitoring.
What does PHP-FPM manage?
PHP-FPM is a set of processes that execute a FastCGI request. A web server sends a PHP request to a socket or TCP pool. The pool setting controls the simultaneous execution capacity, but does not encode, bad query, or speed up the external API.The effect of PHP-FPM on the speed of the WordPressArchitecture explains the application.
What data is needed before change?
- Allowable CPU and RAM after database and system allocation
- Real RSS/PSS worker size at normal and peak workload
- Rate and concurrency of PHP requests
- The following information shall be provided:
- Long queue and reach the child ceiling
- 502/504 rate and restart processes
- Slow routes, cron and long jobs.
Average memory may hide heavy workloads. Samples should be taken after the application is warm and in admin, API and Checkout scenarios. Do not publish production data without cookies, sensitive query or customer information.
Three steps to process manager.
static
The behavior of the capacity is predictable but memory is also reserved during unemployment. It can be suitable for stable workload and precise capacity, provided the RAM budget is clear.
dynamic
The difference between minimum and maximum, depending on the spare processes, is child-creating or decreasing. It is common for variable traffic, but the start/min/max spare values should be consistent with the burst pattern.
ondemand
It creates processes when requested and closes idles. It releases memory for low traffic sites, but cold start and fast burst should be measured.
pm.max_childrenHow is it determined?
First, specify the RAM that remains after the kernel, web server, database, cache, agent, and headroom. Then measure it by the actual distribution of worker memory, not an Internet number. The resulting ceiling should also be limited by the number of cores, latency, and database capacity.
If each child executes a CPU-bound request, hundreds of children on several cores will only touch the context switch and queue down. If requests are more waiting for I/O, higher concurrency may be useful, but timeout and dependency should be controlled.
Spare servers and burst.
In dynamic mode, ready processes should respond to a typical burst without a storm fork, but retain a large amount of idle memory. Measure the request input rate and child build time. Small changes and queue viewing are better than large shifts in a few parameters.
pm.max_requestsWhat's the use?
Periodic worker recovery can limit the effect of library memory growth or extension, but leak treatment is not. Very low overhead increases process and cache warming; very high amounts keep leakage longer.
Enable the Page Status Secure
The status pool can display active/idle process, queue, and capacity ceiling. The endpoint should not be public; make it available only from network or authentication monitoring and limit the proxy rule. Collect status data with latency and request rate on a timeline.
Slow Log for long requests
request_slowlog_timeoutAnd theslowlogThey can record a long request stack. The threshold should not be low enough to create a log flood and not high enough to see only the final timeout.
Timeout and end of request.
The PHP execution ceiling, timeout FPM, Nginx, and client layers are different. Increasing them all takes up more of the worker for long import. Move the long operations as far as possible to queue/CLI.
Separate pool for different workloads
Sites or routes with different levels of trust and resource patterns can have separate pools with user, socket, and independent limit. This reduces the separation of the blast radius, but the total ceiling of pools should be located at host capacity. Building multiple pools without a shared budget hides the overcommit.
Socket, Permission and Nginx.
listenPool should be exactly the same asfastcgi_passThe web server must be compatible. The ownership and mode socket must provide the minimum access required;777After you upgrade PHP, check the socket path and the active unit.502 Nginx error is not correct.It often appears from this border.
OPcache and FPM
OPcache prevents duplication of bytecode compilation and its configuration depends on the size of the codebase, deployment, and memory. Lack of cache space can create churn; overallocation also takes up RAM from other services.
Do not misinterpret the PHP Memory Limit
memory_limitEach PHP implementation is implemented and does not have a full pool ceiling. Raising it may save a request but increase the risk of simultaneous consumption. Memory errors must be connected to the plugin, dataset or code path; then the required ceiling is aligned with the pool capacity.
Don't forget the database and downstream.
Increasing child can send more connection and query to the database and worsen latency. Redis, the payment API, and the filesystem also have a ceiling.
The safe change method.
- Save the configuration and baseline metric.
- Choose a hypothesis and a group of related parameters.
- Validate the syntax with the same binary version of PHP.
- First, apply the change to the staging or part of the traffic.
- Reload is controlled and check the status of the children.
- Compare the array, latency, RAM, CPU and error.
- If the metric gets worse, roll back with the configuration version.
The name of the binary and unit are different between the distributions and PHP versions; do not run the boundary version command in production. Get the effective configuration path from the same active service.
How do we know Worker is short?
Frequent max active, growing queue and waiting latency with CPU/RAM have headroom clue. If the workers themselves are too slow, adding child may only transfer the backlog to the database.
How do we know Worker is a lot?
The swap, OOM, context switch, CPU saturation and latency drop in high concurrency indicate. If the total memory ceiling pools pass through RAM, the risk is hidden even in normal traffic.Server RAM consumptionIt's useful for separating cache and pressure.
Common Mistakes
- Copy of the number.
pm.max_childrenFrom another server. - Calculation based on total RAM without database input
- Increase child to Query
- Public status page
- Slow log permanent and without retention
- Changing several parameters at a time.
- Re-load without syntax test and rollback
- One knows the memory_limit with the full memory FPM
When do you need special assistance?
If the site moves between a long row, 502, and an OOM, the settings will move a problem number to another layer.Monthly management of the serverIt can set FPM, Nginx, database and memory budget with actual workload measurement and step.
Common Questions
What's the best value of pm.max_children?
It has no public numbers; actual worker memory, allowable RAM, CPU, latency and downstream capacity are determinants.
Is Dynamic better or Ondemand?
It depends on the traffic pattern and the sensitivity of the cold start.
Does the increase of the worker speed up the site?
Only if the row is due to lack of concurrency and has resources and downstream capacity, code or query will not be modified slowly.
Does the server need to be rebooted to change the FPM?
Usually, the same service management is enough, but the exact method depends on the distribution and version; check the syntax and reload behavior.