If you run PHP behind Nginx, there is a good chance PHP-FPM is doing most of the work behind the scenes. Nginx handles the web request, passes PHP scripts to PHP-FPM, and waits for PHP-FPM to return the response.
When that relationship breaks, you can end up with slow websites, 502 Bad Gateway errors, timeouts, failed deployments or PHP processes consuming most of the server's memory.
Understanding a few PHP-FPM concepts makes these problems much easier to diagnose. This guide explains how requests flow through PHP-FPM, how pools and workers operate, what settings such as pm.max_children actually control, and how to troubleshoot the most common failures.
PHP-FPM stands for PHP FastCGI Process Manager.
Instead of starting a new PHP process for every request, PHP-FPM keeps a pool of PHP worker processes ready to execute scripts.
A typical Nginx and PHP request looks like this:
Browser
|
v
Nginx
|
| FastCGI
v
PHP-FPM
|
v
PHP application
|
+---- MySQL
+---- Redis
+---- External APIs
Nginx handles things such as:
When the request needs PHP, Nginx passes it to PHP-FPM using FastCGI.
PHP-FPM selects an available worker, executes the PHP application and returns the response to Nginx.
PHP describes FPM as its primary FastCGI implementation and includes features such as separate process pools, different users and groups, configurable process management, slow-request logging and status information.
This distinction is useful when troubleshooting.
A normal Nginx PHP configuration contains something similar to:
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
The important line is:
fastcgi_pass unix:/run/php/php-fpm.sock;
That tells Nginx where PHP-FPM is listening.
Depending on the server, PHP-FPM may listen on a Unix socket:
/run/php/php8.4-fpm.sock
or a TCP address:
127.0.0.1:9000
If Nginx cannot connect to that socket or port, the PHP application cannot run.
That is one of the most common reasons for an Nginx 502 Bad Gateway response.
PHP-FPM can run multiple independent pools of worker processes.
A server hosting three websites could use:
PHP-FPM
├── example.com pool
├── shop.example.com pool
└── api.example.com pool
Each pool can have its own:
PHP specifically supports pools running under different users and groups and listening on different sockets or ports.
This is useful when hosting unrelated websites on the same server.
For example:
[example]
user = example
group = example
listen = /run/php/example.sock
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 4
A second website could use:
[shop]
user = shop
group = shop
listen = /run/php/shop.sock
pm = dynamic
pm.max_children = 20
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
The two sites now have separate PHP workers and Unix users.
That helps with both resource management and isolation. A PHP process for one website does not need to run as the same Linux user as every other site on the server.
The path depends on the operating system and how PHP was installed.
On Ubuntu and Debian, you will commonly see:
/etc/php/8.4/fpm/php-fpm.conf
/etc/php/8.4/fpm/pool.d/
with a default pool such as:
/etc/php/8.4/fpm/pool.d/www.conf
Different installed PHP versions have separate directories:
/etc/php/8.2/fpm/
/etc/php/8.3/fpm/
/etc/php/8.4/fpm/
On Rocky Linux, AlmaLinux and other RHEL-family systems, a common layout is:
/etc/php-fpm.conf
/etc/php-fpm.d/
Do not assume the path from an online guide matches your server.
Find the running service:
systemctl list-units --type=service \
| grep -i fpm
You can also inspect the PHP-FPM process:
ps aux | grep '[p]hp-fpm'
The PHP version returned by:
php -v
is the command-line PHP version.
It does not necessarily tell you which PHP version a website uses.
A server could have:
CLI: PHP 8.4
Website A: PHP 8.3
Website B: PHP 8.4
Website C: PHP 8.2
Check the site's Nginx configuration:
sudo nginx -T 2>/dev/null \
| grep fastcgi_pass
You might find:
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
That site is using PHP 8.3 regardless of what php -v reports.
This is particularly important after changing PHP versions.
A server may have PHP 8.4 installed while Nginx is still trying to connect to:
/run/php/php8.3-fpm.sock
If PHP 8.3 has been removed, Nginx will return a 502.
ServerAuth supports assigning different PHP versions to individual websites, so when troubleshooting a managed site it is worth checking that the PHP version selected for the website matches the version you expect.
Each PHP-FPM worker can process one request at a time.
If a pool has:
pm.max_children = 10
then that pool can process at most ten PHP requests simultaneously.
The eleventh request must wait until a worker becomes available.
This does not mean the website can only handle ten users.
Many requests may:
The important number is the number of PHP requests being processed at the same moment.
PHP's documentation describes pm.max_children as the limit on the number of simultaneous requests a pool can serve.
Every pool has a pm setting.
PHP-FPM supports:
static
dynamic
ondemand
Each manages workers differently.
staticExample:
pm = static
pm.max_children = 10
PHP-FPM creates exactly ten workers and keeps them running.
This gives predictable capacity and avoids creating new workers during traffic spikes.
The trade-off is memory.
Even a quiet site keeps all ten workers alive.
static can work well for predictable, busy applications where enough memory has been reserved specifically for PHP.
dynamicExample:
pm = dynamic
pm.max_children = 20
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
PHP-FPM starts with a number of workers and adjusts the pool according to demand.
It tries to keep enough idle workers ready while avoiding unnecessary processes when traffic is lower.
PHP documents the relevant settings as:
| Setting | Purpose |
|---|---|
pm.max_children |
Maximum worker count |
pm.start_servers |
Workers created when the pool starts |
pm.min_spare_servers |
Minimum desired idle workers |
pm.max_spare_servers |
Maximum desired idle workers |
dynamic is a sensible choice for many general-purpose web servers.
ondemandExample:
pm = ondemand
pm.max_children = 10
pm.process_idle_timeout = 10s
Workers are created when requests arrive.
Idle workers are removed after the configured timeout.
This can reduce memory usage on servers hosting many low-traffic websites because inactive sites do not need to keep a collection of PHP workers alive.
The downside is that a request may occasionally have to wait for a new PHP worker to start.
ondemand can therefore be useful for low-traffic sites where conserving memory matters more than avoiding that small startup delay.
PHP documents pm.process_idle_timeout specifically for ondemand pools.
There is no universal best option.
A rough guide is:
| Mode | Good fit |
|---|---|
static |
Consistently busy applications with predictable resources |
dynamic |
General web applications with varying traffic |
ondemand |
Many low-traffic sites sharing a server |
For most developers running a typical Laravel, Symfony, Craft CMS or WordPress site, dynamic is usually the easiest starting point.
The more important setting is often pm.max_children.
Getting that badly wrong can either waste memory or leave requests waiting for workers.
pm.max_children actually control?Consider:
pm.max_children = 10
PHP-FPM can run up to ten PHP workers for that pool.
If all ten are busy:
Request 1 -> Worker 1
Request 2 -> Worker 2
Request 3 -> Worker 3
...
Request 10 -> Worker 10
Request 11 -> waits
Request 12 -> waits
If requests finish quickly, this may never be noticeable.
If each request spends several seconds waiting on a slow database query or external API, the available workers can disappear very quickly.
Eventually users may experience:
504 Gateway Timeoutpm.max_childrenThis is why simply increasing Nginx timeouts is often the wrong response to a slow PHP application.
The real problem may be that PHP-FPM has run out of workers.
There is no fixed amount.
Worker memory depends on:
Inspect PHP-FPM processes:
ps -ylC php-fpm \
--sort:rss
Depending on the executable name, you may need:
ps aux \
| grep '[p]hp-fpm'
To inspect resident memory in a more useful format:
ps -eo pid,user,rss,cmd \
| grep '[p]hp-fpm'
RSS is shown in kilobytes.
For example:
PID USER RSS CMD
1211 example 48320 php-fpm: pool example
1212 example 52184 php-fpm: pool example
1213 example 49760 php-fpm: pool example
These workers are using roughly 47 to 51 MB of resident memory each.
Do not measure a completely idle pool once and assume every production request will use the same amount.
Check memory while the application is experiencing representative traffic.
pm.max_childrenSuppose a server has 4 GB of RAM.
Do not give all 4 GB to PHP.
The server also needs memory for:
Imagine that after allowing for everything else you decide that PHP-FPM can safely use around 1.5 GB.
If an average busy worker uses around 60 MB:
1536 MB / 60 MB = approximately 25 workers
You might begin with:
pm.max_children = 20
rather than immediately using the theoretical maximum.
That leaves some headroom for requests that use more memory than average.
This is an estimate, not a formula you should apply blindly.
Monitor the server after making the change.
If you configure:
pm.max_children = 100
and each worker can use 80 MB, PHP alone could potentially demand several gigabytes of memory.
A higher limit is not automatically faster.
On a memory-constrained server it can make an outage worse.
pm.max_childrenPHP-FPM can log when the worker limit has been reached.
Search the service logs:
sudo journalctl \
-u php8.4-fpm \
| grep -i "max_children"
Depending on your logging setup, you may instead need to search a PHP-FPM log file.
A warning may look similar to:
server reached pm.max_children setting
This tells you the pool has had more simultaneous work than its configured worker limit allows.
It does not automatically mean you should increase the value.
First ask why the workers are busy.
Possible reasons include:
Increasing capacity can be appropriate, but only if the server has enough memory to support it.
PHP-FPM has an optional status endpoint.
In a pool configuration:
pm.status_path = /fpm-status
The status output can show information such as:
pm.max_children has been reachedPHP documents these values as part of the FPM status page.
The most useful fields during an incident are often:
listen queue
active processes
idle processes
max active processes
max children reached
slow requests
If:
idle processes: 0
max children reached: 42
listen queue: 18
the pool is clearly struggling to keep up with incoming PHP work.
The status endpoint contains operational information about your PHP processes.
Restrict it to localhost, a monitoring service or trusted IPs.
For Nginx, a local-only location might look like:
location = /fpm-status {
allow 127.0.0.1;
deny all;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/example.sock;
}
PHP-FPM itself should also never be exposed directly to an untrusted network. PHP's documentation specifically warns that an untrusted client able to connect directly to FPM can manipulate FastCGI parameters in dangerous ways.
If you use TCP rather than a Unix socket, bind FPM to an appropriate local or private address rather than:
0.0.0.0:9000
unless you have a very specific and securely controlled reason to do so.
When workers are constantly busy, you need to know what they are doing.
PHP-FPM includes a slow-request log.
A pool can contain:
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/example-slow.log
Requests that run beyond the configured threshold can then generate PHP backtraces in the slow log.
PHP lists slow-request logging as one of FPM's built-in features.
This is useful when users report that a website occasionally hangs.
Instead of simply knowing that PHP took a long time, you may be able to see that requests were repeatedly inside:
A five-second threshold is only an example.
Choose one that makes sense for the application.
A site where normal PHP responses complete in 100 ms may warrant a much lower threshold than an application where expensive reports legitimately take several seconds.
request_terminate_timeout carefullyPHP-FPM can terminate individual requests that run for too long:
request_terminate_timeout = 60s
This can stop a PHP worker from remaining occupied indefinitely.
However, terminating long requests can have consequences.
A request might be:
Do not introduce aggressive request termination without understanding what the application does.
Long jobs are often better moved away from the HTTP request entirely and into background workers.
pm.max_requestsPHP-FPM can restart a worker after it has processed a certain number of requests:
pm.max_requests = 500
PHP documents this as a way to mitigate memory leaks in third-party libraries. A value of 0 allows a worker to continue indefinitely.
If a PHP process gradually grows in memory after thousands of requests, recycling workers can be useful.
It should not be used to hide a severe application memory leak indefinitely.
If workers continually balloon in memory, investigate the application and extensions as well.
PHP-FPM commonly communicates with Nginx using either:
Unix socket:
/run/php/example.sock
or:
TCP:
127.0.0.1:9000
For services running on the same machine, Unix sockets are common and avoid opening a TCP listener.
They also introduce filesystem permissions.
The PHP-FPM pool might contain:
listen = /run/php/example.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
PHP-FPM supports socket ownership and mode settings directly in its pool configuration.
If Nginx cannot access the socket, its log may contain:
connect() to unix:/run/php/example.sock failed (13: Permission denied)
Check:
ls -la /run/php/
and identify the Nginx worker user:
ps -eo user,group,pid,cmd \
| grep '[n]ginx'
Fix the pool's socket permissions rather than applying:
chmod 777 /run/php/example.sock
The socket is normally recreated whenever PHP-FPM starts, so manually changing its permissions is also likely to be temporary.
Configure the correct values in the PHP-FPM pool instead.
502 Bad GatewayA PHP-related Nginx 502 often means Nginx failed to communicate correctly with PHP-FPM.
Start with:
sudo systemctl status php8.4-fpm
Then:
sudo nginx -t
Check the PHP socket:
ls -la /run/php/
Check which socket Nginx expects:
sudo nginx -T 2>/dev/null \
| grep fastcgi_pass
Check Nginx errors:
sudo tail -n 100 \
/var/log/nginx/error.log
Common clues include:
No such file or directory
Usually indicates Nginx points to a socket that does not exist.
Permission denied
Usually points towards socket or filesystem permissions.
Connection refused
PHP-FPM may not be listening on the configured address.
Connection reset by peer
The PHP process may have crashed or terminated the connection.
We cover this error in more detail in the dedicated 502 Bad Gateway troubleshooting guide.
Check its status:
sudo systemctl status php8.4-fpm
Then inspect its logs:
sudo journalctl \
-u php8.4-fpm \
-n 100 \
--no-pager
Configuration errors are a common cause.
Test the installed PHP-FPM configuration before restarting it.
Depending on the distribution and PHP version, the command may look like:
sudo php-fpm8.4 -t
or:
sudo php-fpm -t
Possible failures include:
If you recently edited a pool file, begin there.
Suppose Nginx expects:
/run/php/php8.4-fpm.sock
but:
ls /run/php/
shows nothing matching it.
Check whether PHP-FPM is running:
systemctl status php8.4-fpm
Then check what the pool actually listens on:
grep -RIn \
'^[[:space:]]*listen[[:space:]]*=' \
/etc/php/8.4/fpm/pool.d/
You may discover the pool uses:
/run/php/example.sock
while Nginx expects:
/run/php/php8.4-fpm.sock
One of the configurations needs correcting.
Sockets under /run are runtime files and are normally recreated when services start. Do not create an empty socket file manually.
An error such as:
connect() to unix:/run/php/example.sock failed (13: Permission denied)
means the socket exists, but Nginx cannot access it.
Check:
ls -l /run/php/example.sock
Then inspect the PHP-FPM pool:
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
Confirm which user Nginx workers run as:
ps -eo user,group,pid,cmd \
| grep '[n]ginx'
Correct the PHP-FPM pool configuration and reload or restart PHP-FPM as appropriate.
Avoid making the socket world-writable.
"Running" only tells you the service exists.
It does not tell you whether the worker pool is healthy.
Check:
pm.max_childrenLook at the PHP workers:
ps -eo pid,user,%cpu,%mem,rss,cmd \
--sort=-%cpu \
| grep '[p]hp-fpm'
If every available worker is busy, the application may have more concurrent work than the pool can handle.
Do not immediately double the worker count.
First determine whether those requests are genuinely busy or simply slow.
Find the PHP processes using the most memory:
ps -eo pid,user,%mem,rss,cmd \
--sort=-rss \
| grep '[p]hp-fpm'
Check:
pm.max_childrenpm modepm.max_requestsA server hosting ten pools with:
pm = static
pm.max_children = 10
could keep up to 100 PHP workers running even when the sites are quiet.
That may be unnecessary.
For several small websites, dynamic or ondemand may make better use of memory.
Also remember that PHP is not the only service consuming RAM.
Check the complete server:
free -h
and:
ps aux \
--sort=-%mem \
| head -20
ServerAuth monitors server memory and load over time, so historical graphs can be useful when PHP issues appear during traffic spikes rather than while you are actively investigating the server.
Check the kernel logs for out-of-memory events:
sudo journalctl -k \
| grep -Ei \
'out of memory|oom|killed process'
If you see PHP-FPM workers being killed, the server may be exhausting its memory.
Do not just restart PHP.
Investigate:
pm.max_childrenAn oversized PHP-FPM pool can contribute directly to an out-of-memory event.
This is an important clue.
If several sites use the same server and only one fails, the overall PHP service may be fine.
Check that site's specific:
For example:
sudo nginx -T 2>/dev/null \
| grep -A20 \
'server_name example.com'
Then check the associated fastcgi_pass.
If each site has its own pool, inspect that pool rather than restarting every PHP-FPM process on the server.
Changing from one PHP version to another can break several things at once.
Check:
Old:
/run/php/php8.3-fpm.sock
New:
/run/php/php8.4-fpm.sock
The old PHP version may have had:
php8.3-mysql
php8.3-redis
php8.3-curl
php8.3-mbstring
while the new version does not yet have their equivalents installed.
Check loaded modules:
php8.4 -m
Each PHP version may have separate configuration.
For example:
/etc/php/8.3/fpm/php.ini
/etc/php/8.4/fpm/php.ini
Changing PHP versions may therefore change:
The application or its dependencies may not support the new PHP version.
Always test a major PHP upgrade before applying it to an important production application.
ServerAuth allows different websites on the same server to use different PHP versions, which is useful when one application can move to a newer version before another.
Another common source of confusion is:
php -i
showing one setting while the website behaves differently.
CLI PHP and PHP-FPM can use separate php.ini files.
On Ubuntu, for example:
/etc/php/8.4/cli/php.ini
/etc/php/8.4/fpm/php.ini
Check CLI configuration:
php --ini
For PHP-FPM, inspect the FPM configuration directory directly or use application diagnostics carefully.
If you change:
/etc/php/8.4/cli/php.ini
and expect the website to use the new value, nothing may happen.
Similarly, restarting PHP-FPM will not change the configuration used by an already-running command-line script.
You will often see instructions such as:
sudo systemctl restart php8.4-fpm
A restart stops and starts the service.
Depending on the platform and service definition, PHP-FPM may also support a reload:
sudo systemctl reload php8.4-fpm
A reload is generally preferable where you want PHP-FPM to re-read configuration gracefully without unnecessarily interrupting work.
Check what the service supports:
systemctl status php8.4-fpm
and:
systemctl cat php8.4-fpm
Do not restart services repeatedly as a troubleshooting technique.
If restarting PHP-FPM fixes the site, find out why.
Possible causes include:
A restart is useful. An unexplained recurring restart is not a solution.
Before restarting PHP-FPM after editing its configuration, validate it.
Depending on the installation:
sudo php-fpm8.4 -t
or:
sudo php-fpm -t
Also test Nginx when changing its PHP configuration:
sudo nginx -t
Only then reload the services.
This small habit can prevent a configuration typo from turning a healthy website into an outage.
When PHP-FPM misbehaves, start with evidence.
Useful places include:
sudo journalctl \
-u php8.4-fpm \
-n 100 \
--no-pager
Nginx:
sudo tail -n 100 \
/var/log/nginx/error.log
Application logs:
/var/www/example.com/storage/logs/
or wherever your application writes them.
PHP-FPM also has its own configurable error log.
ServerAuth's Log Viewer includes access to common PHP, Nginx and other server logs from the web interface, which can be handy when diagnosing a site without manually locating each log file first.
When a PHP website stops working, this is a useful sequence:
| Step | Check | Example |
|---|---|---|
| 1 | Nginx response | curl -I https://example.com |
| 2 | PHP-FPM service | systemctl status php8.4-fpm |
| 3 | PHP-FPM logs | journalctl -u php8.4-fpm |
| 4 | Nginx configuration | nginx -t |
| 5 | Expected PHP socket | nginx -T | grep fastcgi_pass |
| 6 | Existing sockets | ls -la /run/php/ |
| 7 | Socket permissions | ls -l /run/php/example.sock |
| 8 | Worker processes | ps aux | grep '[p]hp-fpm' |
| 9 | Worker limit | Check pm.max_children |
| 10 | Server memory | free -h |
| 11 | OOM events | journalctl -k | grep -Ei 'oom|killed process' |
| 12 | Application logs | Check framework logs |
| 13 | Slow requests | Enable or inspect FPM slow log |
| 14 | Database/API delays | Investigate dependencies |
You will not need every step for every incident.
The important thing is to prove each part works before changing unrelated settings.
pm.max_children far too highMore workers only help if the server has enough memory and the underlying application can make use of them.
One hundred slow workers can put more pressure on MySQL than twenty slow workers.
A busy application with only a handful of workers may build a queue even when the server has plenty of spare memory.
Use the status page and logs to determine whether requests are waiting.
Running unrelated websites under the same PHP-FPM user weakens isolation.
Separate pools and users make it easier to control which files each application can access.
chmod 777Configure listen.owner, listen.group and listen.mode properly in the pool.
The socket is normally recreated anyway.
php -v tells you the site's versionIt only tells you which PHP executable your shell is using.
Check the site's FastCGI configuration.
The restart may clear the symptom while hiding the reason it happened.
Read the logs first whenever possible.
If PHP is waiting 90 seconds for a database query, raising an Nginx timeout from 60 to 120 seconds does not make the application healthy.
Find the slow operation.
For a typical PHP application, a good starting point is:
dynamic or ondemand unless you have a reason to use staticpm.max_children based on available memory and measured worker usageFor servers hosting multiple websites, those practices make it much easier to identify which application is consuming resources or causing problems.
Most PHP-FPM problems come down to a relatively small set of issues:
Start with the service and logs, confirm that Nginx and PHP-FPM agree on the socket, then look at workers, memory and slow requests.
ServerAuth can handle much of the routine website configuration around PHP versions while also giving you access to server logs and monitoring from the same interface. It does not remove the value of understanding PHP-FPM, but it can make the information you need easier to reach when a production site is having problems.
ServerAuth provides a whole host of management tools, from controlling who can access your server, to managing your website deployments. And with an ever-growing suite of tools you'll always be one step ahead!
Start for free