24/7 Magento, server & cloud support · USA · UK · Europe · Middle East · Asia hello@scripulse.com
PHP9 min read

PHP-FPM Performance Tuning: Sizing Workers, OPcache and Slow Logs

How to size pm.max_children, choose a process manager, tune OPcache and read PHP-FPM status and slow logs for Magento and Laravel.

PHP-FPM settings decide how many requests your server can handle at once and how it behaves under load. Too few workers and requests queue; too many and the server runs out of memory and starts swapping. Here is how to size them from measurements rather than guesses.

Choose a process manager

  • static: a fixed number of workers. Predictable and fast; best for dedicated application servers.
  • dynamic: workers scale between minimum and maximum spare values. A good general default.
  • ondemand: workers start only when needed. Saves memory on low-traffic or multi-site servers, with a small latency cost.

Calculate pm.max_children

First measure the average memory used by a worker under real traffic:

ps --no-headers -o rss -C php-fpm8.3 | awk '{s+=$1; n++} END {printf "%.0f MB avg over %d procs\n", s/n/1024, n}'

(Use the process name for your PHP version.) Then work out how much memory is available for PHP after the operating system, database, Redis, OpenSearch and other services:

pm.max_children = memory available for PHP / average worker memory

For example, with 6 GB available and workers averaging 120 MB, start around 50 and leave headroom. Magento workers are often much larger than typical PHP applications, so always measure.

A starting pool configuration

pm = dynamic
pm.max_children = 50
pm.start_servers = 12
pm.min_spare_servers = 8
pm.max_spare_servers = 16
pm.max_requests = 500          ; recycle workers to contain memory leaks
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/www-slow.log
pm.status_path = /fpm-status   ; restrict access to internal IPs only

Read the signals

  • “server reached pm.max_children setting” in the FPM log means requests are waiting for a worker. Either increase workers (if memory allows) or make requests faster.
  • The status page shows active processes, listen queue and max children reached. A growing listen queue means saturation.
  • The slow log records stack traces of requests exceeding the timeout, which points directly at expensive code.

Tune OPcache

OPcache stores compiled PHP in shared memory. Large frameworks such as Magento need more than the defaults:

opcache.enable=1
opcache.memory_consumption=512
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=100000
opcache.validate_timestamps=0   ; production only: reload PHP-FPM on every deploy

Check opcache_get_status() for a full cache or a high restart count. With validate_timestamps=0, your deployment process must reload PHP-FPM, or old code keeps running.

Other settings worth checking

  • realpath_cache_size and realpath_cache_ttl: larger values help applications with many files.
  • memory_limit: set per your application’s documented requirements; Magento documents higher limits than most applications.
  • Unix socket vs TCP: sockets avoid TCP overhead when Nginx or Apache run on the same host.

Validate with a load test

Change one thing at a time and load test with realistic scenarios using k6 or JMeter. Watch response times, the listen queue, memory and swap. The right configuration is the one that holds latency steady at your expected peak with memory to spare.

Want an expert to tune your stack?

We optimise PHP-FPM, OPcache, web servers and databases for Magento and Laravel. Discuss PHP performance →

S
Scripulse Engineering Team
Written and reviewed by our Magento, Linux, SRE, cloud and security engineers. About our team.
Let’s talk

Need a reliable engineering partner for your platform?

Tell us what you need. A senior engineer will reply within one business day with a clear next step.

Book a free audit →