The site slows down, then nginx starts returning 502 or 504. On PHP-based sites (WordPress, WooCommerce, Laravel), most of this picture comes down to two points: PHP-FPM process management and the database. This guide explains which data to look at to diagnose the problem, how to calculate the basic settings and how to verify changes. Settings are determined by server resources and workload; “ideal values” copied from the internet often make the problem worse.
1. Measure first
A setting changed without measuring is a guess. The picture we see most often in the field is this: when the site slows down, pm.max_children is doubled, the server starts swapping and the site becomes even slower than before. Collect the following data before making changes.
- PHP-FPM status page (
pm.status_path): active processes, requests waiting in the queue and themax children reachedcount. - PHP-FPM slow log (
request_slowlog_timeout): stack traces of requests over a set duration; shows which plugin or function is slow. - Web server logs: a log format with response time and upstream time fields added.
- MariaDB slow query log: queries above the
long_query_timethreshold. - System resources: memory (swap usage), CPU wait (iowait), disk latency.
When $request_time and $upstream_response_time are added to the log format in nginx, the difference between the two values says a lot. If they’re close to each other, time is being spent in PHP and the database. If the upstream time is short and the total time long, look for the problem on the client side, in the network or in sending the response to a slow client. If listen queue is greater than zero on the status page, requests can’t find an idle process. This single line is the first place to look before changing settings.
2. Calculate the number of PHP-FPM processes
pm.max_children is the maximum number of PHP processes that can run at the same time. If it’s too low, requests wait in the queue (504); if it’s too high, memory runs out and the server starts swapping. A simple calculation:
max_children ≈ (memory available for PHP) ÷ (average memory use of one PHP process)
For example, on a server with 8 GB of RAM, if 4 GB is set aside for the operating system, MariaDB and other services and a WordPress process uses 80 MB on average, about 4096 ÷ 80 ≈ 50 processes can be considered the upper limit. Average process memory should be measured under real traffic. On multi-site servers, each pool needs its own limit, and the total must not exceed memory.
When measuring process memory, the RSS value seen with a command such as ps -eo rss,cmd | grep php-fpm counts OPcache’s shared memory again in every process; real usage is somewhat lower. So a calculation with RSS stays on the safe side. Do the measurement not when idle but while the site’s heaviest pages (WooCommerce checkout, the admin panel, reports) are under load. An idle process looks small. A process under load tells the truth.
| Setting | Function | Note |
|---|---|---|
| pm | static / dynamic / ondemand | Based on the traffic profile |
| pm.max_children | Upper process limit | By memory calculation |
| pm.max_requests | How many requests before a process is recycled | Against memory leaks |
| request_terminate_timeout | Terminating long requests | In line with the web server timeout |
Keep timeouts consistent across layers. If nginx’s fastcgi_read_timeout is shorter than PHP-FPM’s request_terminate_timeout and PHP’s max_execution_time, nginx returns a 504 but the PHP process keeps running in the background and occupies the pool. The user refreshes the page and another process is tied up. The pool fills up in a short time.
3. OPcache
OPcache keeps compiled PHP code in shared memory. If the memory (opcache.memory_consumption) or file count limit (opcache.max_accelerated_files) is too small, the cache fills up and code is constantly recompiled. The hit rate and “cache full” status should be checked in the OPcache status information. In production, the opcache.validate_timestamps setting is decided according to the deployment process: if it’s disabled, OPcache must be reset on every deployment.
On servers where many WordPress sites use the same PHP version, the file count limit fills up quickly; each site brings thousands of PHP files with core, themes and plugins. If the number of cached scripts in the status information is close to the limit, raise the limit. A check done from the CLI doesn’t show the web pool’s OPcache; get the status information over the web, with a script running in the relevant pool.
4. MariaDB: memory and queries
- innodb_buffer_pool_size: The area where frequently used data and indexes are kept in memory. If the buffer pool hit rate is low and disk reads are high, it needs to be increased.
- innodb_log_file_size: In write-heavy workloads, a log file that’s too small causes frequent checkpoints.
- max_connections: Should be consistent with the number of PHP processes; raising it without limit increases memory risk.
- Slow queries: Queries doing full table scans are found with
EXPLAIN, and an appropriate index is added or the query is rewritten.
To see the buffer pool hit rate, run SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_read%';: Innodb_buffer_pool_reads gives reads from disk and Innodb_buffer_pool_read_requests total logical reads. If the disk read ratio keeps rising, the data can’t be kept in memory. The Max_used_connections value also shows how much of the connection limit is actually used; if it’s hitting max_connections, first look on the PHP side for requests that hold connections for a long time.
Before leaving the slow query log permanently enabled in production, check disk space and log rotation. During diagnosis, collecting a few hours’ sample with slow_query_log=1 and a reasonable long_query_time is usually enough.
5. Common WordPress-specific causes
- Large autoloaded data in the
wp_optionstable: it’s loaded into memory on every request. Total autoload size should be checked and unnecessary entries disabled. - Plugins that run unindexed
postmetaqueries (especially filtering and reporting). - Expired transients and session records.
- Plugins that make external API calls on every request; a common cause of 504s.
- WP-Cron being triggered by visitor requests; moving it to the system cron is recommended.
You can measure autoload size with SELECT SUM(LENGTH(option_value)) FROM wp_options WHERE autoload IN ('yes','on','auto-on','auto');; since WordPress 6.6 introduced new values other than yes in the autoload column, older queries may give incomplete results. In Multisite installations each subsite has its own table (such as wp_2_options); don’t look at a single table and say “no problem”. Large autoload entries left behind by a deleted plugin are a cause of slowness we come across often in the field.
To move WP-Cron to the system cron, define('DISABLE_WP_CRON', true); is added to wp-config.php and wp cron event run --due-now is run from the system cron at one-minute intervals. This keeps scheduled tasks from piggybacking on the duration of a visitor’s request.
6. Verify the change
Each setting change should be made one at a time and compared with the same measurements: average and 95th percentile response time, max children reached count, slow query count, memory and swap. A configuration backup should be taken before changes.
If you change three settings at once, you can’t tell which one worked. Or which one to roll back if things get worse. Write each change in a short log with the date, old value, new value and observation; when the same problem comes back a few weeks later, this log shortens diagnosis. For PHP-FPM settings, testing the syntax with php-fpm -t (or its versioned equivalent in your distribution) and reloading the service affects active requests less than a restart.
Frequently asked questions
pm = dynamic or ondemand?
On sites with constant traffic, dynamic or static; on hosting servers with low traffic and many sites, ondemand saves memory. The choice is made based on the number of sites and the traffic profile.
What should innodb_buffer_pool_size be?
On a server running only the database, most of the RAM can be allocated; in most corporate setups where web and database are on the same server, it’s set according to data and index size, leaving room for PHP-FPM and the operating system. The decision should be made by measurement, not a fixed percentage.
What’s the difference between a 502 and a 504 error?
A 502 usually means the web server couldn’t connect to PHP-FPM or the process crashed; a 504 means the response didn’t arrive within the timeout. The first is mostly caused by process limits or memory, the second by slow queries or external services.
For cache layers, see the Cloudflare, WP Rocket and Redis guide; for implementation, see our database performance optimization and Linux VPS and Virtualmin management services.