PHP-FPM settings calculator

PHP-FPM settings calculator

Revised
Published

~ 5 min read


Use measured worker memory to estimate pm.max_children, then check that limit under load. This calculator gives a memory-based worker ceiling. CPU capacity, database connections and response times may require fewer workers.

Calculate a memory budget

Reserve memory for the OS and other services, then allow for the FPM master process and shared memory such as OPcache. Keep some safety headroom for variation in worker size and other memory use.

remaining memory = total memory - OS and services - FPM overhead and shared memory
worker budget = remaining memory × (1 - headroom percentage / 100)
pm.max_children = floor(worker budget / memory per busy worker)

The example inputs use 4 GiB total, 1.5 GiB for the OS and other services, 256 MiB for FPM overhead and shared memory, 10% headroom and 75 MiB per worker. That leaves 2073.6 MiB for workers and gives a ceiling of 27. These are example allowances, so replace them with measurements from your server. One GiB is 1024 MiB throughout the calculator.

Use the memory limit available to PHP. In a container, that may be much smaller than the host’s RAM. For multiple pools, allocate each a budget based on its own worker size and workload. The combined budgets must fit within the available memory, counting shared allocations once. Reserve the other pools’ planned maximum use when calculating one pool at a time.

PHP-FPM settings calculator

For a container, use its memory limit rather than the host's RAM.

Include the web server, database and any other PHP pools sharing this budget.

Allow for the master, OPcache and other shared memory once. Exclude anything already counted elsewhere.

Use private memory measured under load. See the article for RSS and PSS alternatives.

Held back after the fixed reservations above. The example 10% allowance needs checking against your workload.

Advanced: startup and idle workers

The example starts 4 workers and keeps 2 to 8 idle, capped by the memory ceiling. Adjust these counts for normal traffic and bursts.

Dynamic pool worker counts

The output contains settings to apply within your existing pool configuration. It is not a complete FPM configuration. The startup and idle-worker defaults are examples, capped by the memory ceiling. Open the advanced options to change them.

Measure busy workers on Linux

Warm up the application and OPcache, then exercise representative traffic, including expensive requests. Sample worker memory repeatedly during sustained load. Choose a conservative busy-worker value that covers the requests you need to support, rather than an average from a quiet server.

This command lists RSS for workers in the www pool, excluding the master. Replace www with your pool name and check that the process titles match your installation.

ps -eo pid=,rss=,args= | awk '$3 == "php-fpm:" && $4 == "pool" && $5 == "www" { printf "PID %s: %.1f MiB RSS\n", $1, $2 / 1024 }'

RSS includes resident shared pages, so multiplying it by the number of workers counts some memory repeatedly. It can provide a conservative estimate, but avoid reserving those same shared pages again in the overhead field. Keep an allowance for the master and anything else outside the worker measurements.

For a more useful breakdown, replace 1234 with one of the worker PIDs above and read its Linux smaps_rollup file. Access to another user’s process may require sudo.

sudo awk '
    /^(Private_Clean|Private_Dirty):/ { private_kib += $2 }
    /^Pss:/ { pss_kib += $2 }
    END {
        printf "Private: %.1f MiB\nPSS: %.1f MiB\n", private_kib / 1024, pss_kib / 1024
    }
' /proc/1234/smaps_rollup

Use private worker memory with a separate allowance for shared memory and FPM overhead. PSS apportions shared pages between the processes using them, so it helps assess the current pool’s total footprint. Its per-worker value changes as the number of sharing processes changes. Recheck the total at your proposed concurrency before treating it as a sizing input. The Linux memory accounting documentation explains these fields.

Account for OPcache once

OPcache stores compiled bytecode in shared memory. When using private worker measurements, include that shared allocation in the overhead reservation once per OPcache instance. Include the JIT buffer too if enabled. Do not add the full cache size to every worker, or reserve it again if your chosen measurement already accounts for it.

Measure again after changing OPcache, extensions or application code. memory_limit and FPM’s per-request PHP memory figures do not describe the whole worker’s resident memory.

Choose startup and idle-worker counts

With pm = dynamic, the calculator starts with 4 startup workers, a minimum of 2 idle workers and a maximum of 8 idle workers. Smaller memory ceilings reduce those values to fit. RAM alone cannot tell you how many idle workers your traffic needs, so these defaults stay modest even on a large server.

  • pm.start_servers controls how many workers start with the pool.
  • pm.min_spare_servers is the desired minimum number of idle workers ready for requests.
  • pm.max_spare_servers is the desired maximum number of idle workers retained after demand falls.

The advanced options enforce positive whole numbers and the relationship min_spare_servers ≤ start_servers ≤ max_spare_servers ≤ max_children. See the PHP-FPM configuration reference for these directives and the alternative static and ondemand process managers.

Validate under load

Apply the settings to the intended pool and test its configuration before reloading FPM. Increase traffic gradually while watching response times, CPU utilisation, database connections and memory pressure. Sustained swapping or out-of-memory kills mean the budget or worker measurement needs revisiting.

Use the FPM status page, restricted to trusted access, to inspect listen queue, active processes, idle processes and max children reached. Queued requests with spare CPU, memory and database capacity may justify more workers. If CPU or the database is already saturated, adding workers can make response times worse.

Adjust startup and spare-worker counts against normal traffic and bursts. Recheck the measurements after changing the worker limit, and repeat the test after application or infrastructure changes.

all posts →