Table of Contents
A 502 Bad Gateway error means your web server (Nginx or Apache) received an invalid response from PHP or an upstream service. In WordPress, this is usually caused by PHP-FPM crashes, plugin issues, memory limits, or server misconfiguration.
Step 1: Identify Your Web Server and PHP Handler
First, confirm what stack you’re using.
ps aux | grep php
Check web server:
nginx -v
apache2 -v
Most 502 issues happen with Nginx + PHP-FPM.
Step 2: Restart Web and PHP Services
This fixes temporary crashes.
For Nginx + PHP-FPM:
sudo systemctl restart php-fpm
sudo systemctl restart nginx
For Apache:
sudo systemctl restart apache2
If 502 disappears, the issue was a PHP worker crash.
Step 3: Check Server Error Logs
Logs tell the truth. Guessing doesn’t.
Nginx:
sudo tail -n 50 /var/log/nginx/error.log
Apache:
sudo tail -n 50 /var/log/apache2/error.log
PHP-FPM:
sudo tail -n 50 /var/log/php*/fpm.log
Look for:
-
out of memory -
child exited -
timeout -
connection refused
Step 4: Increase PHP Memory Limit
WordPress often hits memory limits.
Edit PHP config:
sudo nano /etc/php/*/fpm/php.ini
Set:
memory_limit = 512M
max_execution_time = 300
Restart PHP-FPM:
sudo systemctl restart php-fpm
Step 5: Disable Plugins Temporarily
A broken plugin can crash PHP.
Rename plugins directory:
cd /var/www/wordpress/wp-content
mv plugins plugins-disabled
Reload the site. If 502 is gone, the issue is a plugin. Re-enable plugins one by one.
Step 6: Switch Theme to Default
Bad themes also crash PHP.
mv themes themes-disabled
WordPress will fall back to a default theme.
Step 7: Check PHP-FPM Socket Configuration (Nginx Only)
Mismatch = instant 502.
Check PHP-FPM socket:
ls /run/php/
Example:
php8.2-fpm.sock
Edit Nginx config:
sudo nano /etc/nginx/sites-enabled/your-site.conf
Ensure:
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
Restart:
sudo systemctl restart php-fpm nginx
Step 8: Increase PHP-FPM Workers
Low workers = overload = 502.
Edit pool config:
sudo nano /etc/php/*/fpm/pool.d/www.conf
Adjust:
pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 10
Restart PHP-FPM:
sudo systemctl restart php-fpm
Step 9: Check Server Resources
If RAM or CPU is maxed out, nothing helps.
htop
free -h
If memory is exhausted, upgrade the server or optimize WordPress.
Step 10: Check Cloudflare or Proxy (If Used)
Cloudflare may show 502 while origin is broken.Temporarily disable proxy (orange cloud) and test directly. 502 errors are server problems, not WordPress “bugs”. If your hosting can’t handle PHP load, no plugin or theme will save you. Fix the server or expect the error to return.
