CH-J Toolsserver management over SSH
Menu
All guides and articles →

PHP-FPM: worker, RAM and performance settings

Design the number of PHP workers based on memory, concurrency, and actual request times. Static, dynamic, and ondemand modes, queues, slowlog, and validation without one-size-fits-all recipes.

The number of workers is a capacity decision

PHP-FPM handles FastCGI requests using worker processes. A single worker performs one requested job at a time; other requests can wait. Therefore, a higher pm.max_children can increase concurrency, but also memory consumption, number of database connections, and CPU contention. It is not a universal switch for speeding up a website.

First, isolate the problem: waiting for a free worker, the request itself being too slow, or a lack of overall capacity. A website may have many busy workers and low CPU because processes are waiting for remote APIs. Adding more in such a situation can only increase the pressure on the same dependency. The fix should correspond to where the time is actually being generated.

The following procedure works with general FPM principles. Paths, service names, and option availability vary by distribution and PHP version. Do not put the examples into production without comparing them to real pools and other services. On a single machine, multiple pools can share the same memory budget.

Measure the default state and actual configurations

Record the current response, requests per second, error rate, queue and number of active processes during normal and peak traffic. A single idle snapshot will not explain the memory consumption of an import or large pages. Compare changes under a similar scenario, otherwise the improvement may just coincide with a drop in traffic.

systemctl status php8.3-fpm.service --no-pager
sudo journalctl -u php8.3-fpm.service --since "-30 min" --no-pager
ps -eo pid,ppid,comm,rss,pcpu --sort=-rss | head -25
free -h
swapon --show

Replace the version number with the FPM you have installed. The PHP CLI may not use the same php.ini, extensions, or OPcache as the pool serving the web. Therefore, the output of php -iz in the terminal is not automatically proof that the web process is set up effectively. Verify correct configuration in a manner appropriate to the FPM and do not expose public phpinfo with sensitive paths and values.

If a secure FPM status page is in place, monitor in particular active and idle processes, listen queue, and reaching maximum children. Consider resetting counters after reboot. Detailed status may include paths and request information, so should remain accessible only for authorized diagnostics.

Calculate the memory budget for the entire machine

From the usable RAM, first allocate capacity for the operating system, web server, database, other applications and operational reserve. Only then allocate the rest to FPM. If you have multiple pools, the sum of their possible peaks must correspond to this reserve. Do not derive the maximum consumption from one freshly created idle process.

Illustrative example: 1024 MiB is left for all workers, and from measurements of similar work you expect approximately 64 MiB of private memory per worker. A simple ratio comes out to 16. It is not an automatic recommendation to set 16: you still need to factor in additional shared demands, process fluctuations, concurrent heavy pages, and CPU and database capacity. The point of the example is to show a budget, not to set a universal PHP process size.

RSS totals can count shared pages multiple times. PSS and private memory provide other perspectives that can help refine the budget. Overall cgroup accounting is also important for protecting a specific service. Compare definitions and don't present one metric as a universal physical cost of every other process.

PHP memory_limit is also not an exact cap for all RSS workers. Extensions, shared areas, and other requirements may have different billing. Multiplying memory_limit by the number of workers can be a rough guide, but it is not a substitute for measuring the actual application. The request protection limit and pool capacity budget address different issues.

Static, dynamic and ondemand have different operational behaviors

  • Static holds a fixed number of worker processes. It offers predictable readiness, but also a constant memory cost.
  • Dynamic adjusts the number according to set limits and maintains a reserve of idle processes. Suitable for variable operations with the need for quick response.
  • Ondemand creates processes as needed and terminates them after inactivity. It can reduce idle consumption, but process creation and cold start come at a cost.

No mode is generally the fastest for every application. For many lightly used pools, saving idle memory can be important. For a constantly busy site, ready concurrency and stable response are more important. Before changing modes, measure both the start after idle and normal warm-up operation.

; Pouze ilustrační platná kombinace pro jeden dynamic pool:
pm = dynamic
pm.max_children = 12
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500

The values given are not recommendations for your server. The number of starting and standby processes must make sense in relation to max_children and the expected rate of load growth. In addition, ondemand considers process_idle_timeout; its value that is too short can increase the number of starts. Static, on the other hand, does not use the same standby management as dynamic.

Distinguish the queue from the application's own work time

If the status reports max_children reached and the listen queue is growing, requests are waiting. Find out what the workers are holding. They could be actually computing, waiting on the database, or calling a slow service. More concurrency only helps if the entire chain has the necessary headroom. Otherwise, you'll just move the queue to the database or memory.

The relationship between incoming intensity, work time, and average concurrency can be used as a guide. For example, 50 requests per second with an average work time of 0.2 seconds means approximately ten requests being served simultaneously under appropriate stable conditions. However, the actual capacity must handle dispersion, expensive paths, and short bursts; the ratio of averages is no guarantee of acceptable percentiles.

listen.backlog increases the possibility of waiting for acceptance, not the computing speed of workers. A large queue can extend the waiting time for users until a proxy timeout occurs. Therefore, in addition to throughput, consider latency and the number of rejected or canceled requests. The goal is not just to hide the message about the number of children reached.

Slowlog, OPcache and process recycling

FPM slowlog can help determine where in PHP a slow request is spending time. It is set up specifically with a suitable threshold and a protected output path. Collection has overhead and can reveal the internal structure of the application, so enable it according to the specific question and verify the availability of the necessary permissions in the given environment.

OPcache reduces the repetitive work of compiling PHP code. Its benefits and file updates must be evaluated in the context of the deployment method. Do not turn off change verification just for the sake of speed without a design for how the website will safely retrieve new code after publication. Similarly, the absence of OPcache in the CLI does not automatically indicate its status in FPM.

Pm.max_requests can recycle a worker after a certain number of requests. This can reduce the long-term accumulation of some claims, but it does not fix the cause of the actual leak, and too frequent recovery is worth it. For a growth problem, observe the behavior over time and the specific path. Permanently restarting the entire pool is not a substitute for such a diagnosis.

Limits and time budgets must not work against each other

Pool, PHP, NGINX and external dependencies can have different timeouts. Decide how long the user should wait and which layer should terminate in a controlled manner in case of a problem. Expiry of a proxy wait does not necessarily mean immediate termination of the backend operation. Therefore, the application must safely handle retry or retrieval of the result for change requests.

The cgroup memory limit protects the environment, but too tight can cause the pool to terminate during peak hours. Too loose together with too many workers can compromise the entire machine. The setting is based on budget and measurement. Consider large imports or exports in particular: a separate work queue may be more appropriate than the same synchronous web processes for all operations.

Separate pools will allow for different permissions and limits, but they are not in themselves complete security isolation of all traffic. Consider file ownership, socket access, database, and shared resources. Splitting the same overloaded machine into multiple pool names does not create new physical capacity.

Small change, syntax check and comparable test

Change a limited number of related values and save the original state. Verify the syntax of the actual FPM binary and use a documented reload or restart depending on the service and change. Reload can temporarily overlap the old processes with the new ones, so if you are on a tight budget, measure this transition as well. Leave a return path.

sudo /usr/sbin/php-fpm8.3 -t
# Pouze po úspěšném testu a podle skutečné jednotky:
sudo systemctl reload php8.3-fpm.service
  • The memory peak remains under the documented budget even at peak times.
  • Queue and responses improved with comparable inbound traffic.
  • Databases and external services did not gain a new unbearable concurrency.
  • There are no OOMs, new 502/504s, or excessive process starts.
  • The result includes both common sites and truly challenging routes.

In CH-J Tools, you can combine CPU and memory overview with terminal pool diagnostics. Support your conclusion with a record of specific measurements and settings. The sentence "more workers is faster" is too general without a budget and response; it is correct to describe what concurrency the server safely handles and what still constitutes its limit.

Frequently asked questions

How many workers should a server with 4 GB of RAM have?

This cannot be determined by total RAM alone. It depends on other services, actual request memory, CPU, and dependency capacity.

Is ondemand always more economical and better?

It may save idle processes, but it comes at the cost of startups and other crash behavior. Verify the choice on your operation.

Is memory_limit an accurate limit for RSS worker?

No. It is not identical to all process and shared memory requirements. Pool capacity needs to be measured separately.

Will pm.max_requests solve the memory leak?

It can limit its long-term impact by recycling the process, but it does not eliminate the specific application error and has its own operational cost.

Detailed documentation

PHP: FPM configuration →

PHP: FPM status page →

PHP: OPcache →

systemd: accounting and memory limits →

Biometric unlock and security →

All tutorials by category →