Server Management

PHP-FPM Explained: What It Is and How to Configure It

PHP-FPM is the process manager that runs your PHP code on Nginx servers. Misconfigured PHP-FPM is the most common cause of 502 errors and server slowdowns. Here is how it works and how to tune it.

April 1, 2025 11 min read NextCode Solutions

If your Nginx server is returning 502 Bad Gateway errors, or your site slows to a crawl under traffic, PHP-FPM configuration is almost always the culprit. Understanding how PHP-FPM works — and how to tune it — is one of the most valuable server management skills you can have.

What Is PHP-FPM?

PHP-FPM (FastCGI Process Manager) is the component that actually executes your PHP code. When a visitor requests a PHP page, Nginx passes that request to PHP-FPM, which runs the code and returns HTML back to Nginx to serve to the visitor.

PHP-FPM manages a pool of worker processes — pre-forked PHP processes waiting to handle requests. The number of workers available determines how many simultaneous PHP requests your server can process. If all workers are busy when a new request arrives, it queues. If the queue fills, visitors get 502 errors.

Key PHP-FPM Configuration Parameters

The PHP-FPM pool config lives at: /etc/php/8.x/fpm/pool.d/www.conf (Ubuntu) or in aaPanel at /www/server/php/81/etc/php-fpm.conf

ParameterWhat It ControlsDefaultRecommended Starting Point
pmProcess management modedynamicdynamic (for most sites)
pm.max_childrenMaximum simultaneous PHP processes5(RAM in MB ÷ 40) — see below
pm.start_serversProcesses started on boot2pm.max_children ÷ 4
pm.min_spare_serversMinimum idle processes1pm.max_children ÷ 4
pm.max_spare_serversMaximum idle processes3pm.max_children ÷ 2
pm.max_requestsRequests before worker restart0 (unlimited)500 (prevents memory leaks)
request_terminate_timeoutMax time for a single request060s

Calculating pm.max_children

The most important value. Setting it too low causes 502 errors. Setting it too high causes OOM (out of memory) kills that crash everything.

Formula: (Available RAM in MB) ÷ (Average PHP process size in MB) = max_children

Check your average PHP process size:

ps --no-headers -o rss,cmd -C php-fpm8.1 | awk "{ sum+=\$1 } END { printf \"Avg: %dMB\", sum/NR/1024 }"

Example: 2GB VPS with 1.5GB available for PHP, average process size 45MB → max_children = 33

Process Management Modes

Multiple Pools — One Per Site

On a server with multiple websites, each site should have its own PHP-FPM pool. This improves security (each site runs under its own user) and performance (a misbehaving site cannot exhaust workers for other sites).

In aaPanel, each site automatically gets its own pool. Check them at /www/server/php/81/etc/php-fpm.conf.d/

After Changing Configuration

# Test config for errors first
php-fpm8.1 -t

# Reload gracefully (no downtime)
systemctl reload php8.1-fpm

Quick diagnosis: If you see "connect() to unix:/run/php/php8.x-fpm.sock failed (11: Resource temporarily unavailable)" in your Nginx error log — your pm.max_children is too low. Increase it by 5-10 and reload.

Related Reading

Is PHP-FPM Causing Your 502 Errors?

NextCode Solutions diagnoses and tunes PHP-FPM configurations for WordPress and PHP sites on Nginx VPS servers. Usually resolved same day.

Get Server Help