A 502 Bad Gateway on cPanel says the web server (Apache or LiteSpeed) tried to forward a request to an upstream worker (PHP-FPM, LSPHP, or a backend service) and got either no answer or a malformed one. Unlike a 500, the request never made it deep enough into PHP to log a traceback there. The fix path runs from the upstream backwards.
What’s happening
The chain is: client > Apache/LiteSpeed > upstream worker (PHP-FPM/LSPHP) > application. A 502 means the link between the web server and the upstream broke. Either the upstream is not listening on the expected socket, it timed out before responding, or it returned an unparseable response (usually because it crashed mid-request). The web server’s error log is where the upstream’s name and the failure mode appear.
How to verify
Find which upstream is failing:
# cPanel Apache log
tail -100 /usr/local/apache/logs/error_log | grep -i "bad gateway\|upstream"
# LiteSpeed log
tail -100 /usr/local/lsws/logs/error.log
# PHP-FPM pool status
ls /opt/cpanel/ea-php*/root/var/log/php-fpm/
systemctl status ea-php82-php-fpm
ps -eo pid,user,cmd | grep php-fpm
# socket listener
ss -lnp | grep php-fpm
If the upstream is alive but slow:
# response time on a known endpoint
curl -w "%{time_total}\n" -o /dev/null https://example.com/healthz
The fix
- PHP-FPM pool exhaustion — if
pm.max_childrenis reached, new requests queue and eventually time out as 502. Raisepm.max_childrenin the PHP-FPM pool config (/opt/cpanel/ea-php82/root/etc/php-fpm.d/<user>.conf). Restart the pool. - PHP-FPM crashed worker —
journalctl -u ea-php82-php-fpm --since "1 hour ago". Look for segfault entries. Common cause: a buggy extension (opcache,xdebug, or a third-party one). - LSPHP not running — LiteSpeed spawns LSPHP workers on demand. If a per-user
lsphpis killed, the 502 hits only that user. Restart LiteSpeed:/usr/local/lsws/bin/lswsctrl restart. - Backend service down — if the application proxies to a separate service (Node, Python, Ruby) via
ProxyPass, that service is the upstream. Checksystemctl status <service>. - Timeout misalignment — if Apache’s
ProxyTimeoutis 60 seconds but the application takes 90, the worker is fine but Apache gave up. RaiseProxyTimeoutto match the application’smax_execution_time.
A flaky 502 that hits one request in twenty under load is usually PHP-FPM pm.max_children set too low. Stack Harbor tunes these worker pools under cPanel/WHM management — start a conversation when 502s are intermittent.