Diagnosing 502/504 Gateway Errors
Understanding 502 and 504 Errors
A 502 Bad Gateway means OLS received an invalid response from the PHP backend (lsphp). A 504 Gateway Timeout means the PHP process took too long to respond. Both point to PHP-side issues.
Common Cause: PHP Timeout
WordPress operations like imports, plugin updates, or WooCommerce order exports can exceed the default timeout. Increase these values:
# PHP settings
max_execution_time = 300
max_input_time = 300
# OLS external app timeout (vhconf.conf)
extprocessor lsphp {
maxConns 20
env PHP_LSAPI_CHILDREN=20
initTimeout 300
retryTimeout 0
respBuffer 0
backlog 100
connTimeout 300
}
Check lsphp Processes
If all PHP workers are busy, new requests get 502 errors. Check the current worker count:
ps aux | grep lsphp | grep -v grep | wc -l
If the count equals your PHP_LSAPI_CHILDREN limit, either increase the limit or investigate which requests are consuming workers (check the access log for slow requests).
OLS LSAPI Connection Issues
If lsphp crashes or fails to start, OLS cannot forward requests. Check the OLS error log:
tail -50 /usr/local/lsws/logs/error.log
Look for messages like connection refused, lsphp is not available, or max children reached.
Restart PHP Workers
Sometimes stale lsphp processes need to be killed and restarted:
sudo killall -9 lsphp
sudo systemctl restart lsws
OLS will spawn fresh PHP workers on the next request.
Memory Exhaustion
If the server is running out of RAM, lsphp processes get killed by the OOM killer. Check:
dmesg | grep -i "out of memory"
free -h
Reduce PHP_LSAPI_CHILDREN or add swap space if memory is tight.
Quick Checklist
- Check
/usr/local/lsws/logs/error.logfor LSAPI errors - Check
/home/DOMAIN/logs/error.logfor PHP fatal errors - Verify
max_execution_timeis sufficient - Ensure enough free RAM for PHP workers
- Restart OLS if processes are stuck:
sudo systemctl restart lsws