Apache returns 503 or 504 because PHP-FPM did not answer in time
The timeout specified has expiredShort answer
Apache sent the request to PHP-FPM and gave up waiting. Either the script runs longer than Apache's proxy timeout, every PHP-FPM worker is busy so the request just waits, or PHP-FPM kills the request itself. The Apache error log and the PHP-FPM slow log tell these apart.
Fix it fast
1Find out which cause you have
Find the failing requests in the Apache error log and note whether they are always the same URL (a slow script) or random ones under traffic (a busy pool).
sudo grep -E 'proxy_fcgi|timeout specified' /var/log/apache2/error.log | tail -n 202Match what you see, then jump to its fix
- 1
Always the same URL fails, after about the same number of secondsThe script takes longer than Apache waitsFix - 2
Random URLs time out when traffic is high, while a single request on a quiet server worksAll PHP-FPM workers are busyFix - 3
Failures happen at a round number of seconds that matches a PHP-FPM settingPHP-FPM ends the request before Apache doesFix
- 1
What does the error look like?
The timeout specified has expiredError dispatching requestproxy_fcgi:error
Cause 1 of 3
The script takes longer than Apache waits
1Check
Apache waits for data from the backend for ProxyTimeout seconds, which defaults to the value of Timeout. A report or import that runs longer than that is cut off on the Apache side. Time the request.
grep -R -n -E "ProxyTimeout|^Timeout|timeout=" /etc/apache2/
time curl -s -o /dev/null https://example.com/slow-report.php2Fix
First make the script faster or move it to a queue. If it must stay long, raise the timeout for that backend only, with the timeout parameter on the proxy worker or ProxyTimeout.
# in the vhost, next to the PHP handler
ProxyTimeout 1203Verify
Test the config, reload Apache and repeat the slow request.
sudo apachectl configtest && sudo systemctl reload apache2
time curl -s -o /dev/null https://example.com/slow-report.phpConfirmed by httpd.apache.org
Cause 2 of 3
All PHP-FPM workers are busy
1Check
Each PHP-FPM worker serves one request at a time. When pm.max_children workers are all busy, new requests queue until Apache gives up. The Apache docs also warn that idle persistent connections can occupy every worker. See the pool settings and whether connection reuse is on.
sudo grep -R -n -E "^pm\.|^pm =" /etc/php/8.3/fpm/pool.d/
grep -R -n "enablereuse" /etc/apache2/2Fix
Raise pm.max_children to what your memory allows, and turn off enablereuse on the FPM worker if it is on and the pool is small.
; /etc/php/8.3/fpm/pool.d/www.conf
pm.max_children = 203Verify
Restart PHP-FPM and repeat a load test; the timeouts should stop appearing.
sudo systemctl restart php8.3-fpm
sudo grep -c "timeout specified" /var/log/apache2/error.logConfirmed by httpd.apache.org
Cause 3 of 3
PHP-FPM ends the request before Apache does
1Check
request_terminate_timeout kills a worker after that many seconds. It is 0 (off) by default, so only a value someone set explains this. request_slowlog_timeout, when set, writes a backtrace of slow requests so you can see where the time goes.
sudo grep -R -n -E "request_terminate_timeout|request_slowlog_timeout|^slowlog" /etc/php/8.3/fpm/2Fix
Raise or remove request_terminate_timeout for the pool, and keep max_execution_time in php.ini consistent with it.
; pool config
request_terminate_timeout = 120s
request_slowlog_timeout = 10s3Verify
Restart PHP-FPM and run the long request again.
sudo systemctl restart php8.3-fpmConfirmed by php.net
How do I stop it coming back?
- Move work that takes more than a few seconds into a queue and return at once.
- Set request_slowlog_timeout so slow requests leave a backtrace before they become timeouts.
- Size pm.max_children from memory per worker, not by guess.
- Keep Apache's ProxyTimeout and PHP-FPM's request_terminate_timeout in step.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
CodeIgniter 4 cannot write its cache files
CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/
CodeIgniter 4's file cache needs a cache directory that the web server can really write to.
2 causes, each with a fixBack end
CodeIgniter 3 loads the model but the property is undefined
The model name you are loading is the name of a resource that is already being used: user
CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.
2 causes, each with a fixBack end
CodeIgniter 3 stops with "Your system folder path does not appear to be set correctly"
503 Service Unavailable
CodeIgniter 3's front controller resolves $system_path and then checks that the result is a directory.
3 causes, each with a fixBack end
Composer cannot reach repo.packagist.org on port 443
Operation timed out after 300000 milliseconds
When Composer cannot reach repo.packagist.org, run composer diagnose first; it tests the connection and the certificate store.
4 causes, each with a fix