{"id":2552,"date":"2026-03-02T11:28:37","date_gmt":"2026-03-02T11:28:37","guid":{"rendered":"https:\/\/ssblog.eu.enginyr.ing\/?p=2552"},"modified":"2026-03-02T11:32:53","modified_gmt":"2026-03-02T11:32:53","slug":"wordpress-502-bad-gateway-in-2026-the-complete-fixing-and-debugging-guide-directadmin-vps","status":"publish","type":"post","link":"https:\/\/www.serverspan.com\/en\/blog\/wordpress-502-bad-gateway-in-2026-the-complete-fixing-and-debugging-guide-directadmin-vps\/","title":{"rendered":"WordPress 502 Bad Gateway in 2026: The Complete Fixing and Debugging Guide (DirectAdmin + VPS)"},"content":{"rendered":"\n\n<p class=\"wp-block-paragraph\">A WordPress 502 Bad Gateway error means NGINX received the request, forwarded it to PHP-FPM for processing, and got no valid response back. The fix is almost always one of seven things: PHP-FPM is stopped, the socket path is mismatched, the FPM worker pool is exhausted, NGINX&#8217;s FastCGI timeout is too short, the <a href=\"https:\/\/www.serverspan.com\/en\/blog\/linux-swap-vs-ram-the-definitive-guide-to-memory-management-on-vps\/\">Linux OOM killer<\/a> terminated the process, MySQL is hanging PHP workers, or a firewall (including fail2ban) is blocking loopback traffic. This guide walks through diagnosing each cause systematically, with real commands and DirectAdmin-specific paths, so you are not guessing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Before You Start: Read the Logs First<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Do not touch configuration files before you know what the error actually is. The logs will tell you exactly which category of 502 you are dealing with, and skipping this step wastes time. Run these three commands in sequence and read the output carefully before proceeding to any fix section.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <a href=\"https:\/\/www.serverspan.com\/en\/blog\/application-layer-performance-tuning-php-8-5-and-nginx-for-high-traffic-vps\/\">NGINX error log<\/a> \u2014 tells you what NGINX saw when the request failed\ntail -50 \/var\/log\/nginx\/error.log\n\n# PHP-FPM log \u2014 tells you if FPM is crashing, running out of workers, or refusing connections\ntail -50 \/var\/log\/php8.5-fpm.log\n\n# Kernel ring buffer \u2014 tells you if the OOM killer fired and terminated a process\ndmesg | grep -iE \"oom|killed process\" | tail -20<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The NGINX error log is the most important. A line containing <code>connect() to unix:\/var\/run\/php\/php8.5-fpm.sock failed (2: No such file or directory)<\/code> points to Fix 1 or Fix 2. A line containing <code>connect() failed (111: Connection refused)<\/code> points to Fix 1. A line containing <code>upstream timed out (110: Connection timed out)<\/code> points to Fix 4. A line with no upstream error at all, combined with a kernel OOM entry, points to Fix 5. Match the error pattern to the fix.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 1: PHP-FPM Is Stopped<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is the most common cause. PHP-FPM crashes silently after a memory exhaustion event, a failed update, or a pool configuration error, while NGINX stays running. DirectAdmin&#8217;s automatic service monitoring watches NGINX but does not always restart PHP-FPM alongside it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl status php8.5-fpm\n\n# If inactive or failed:\nsystemctl start php8.5-fpm\n\n# Check why it failed to start (read the full output):\njournalctl -xeu php8.5-fpm --no-pager | tail -30<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If it starts cleanly and the 502 disappears, that is not the end of the investigation. Something stopped it. Check the PHP-FPM log and the kernel OOM log (Fix 5) to find the root cause, or it will crash again within hours. If systemctl reports a syntax error in the pool configuration, find and fix the offending directive before restarting.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 2: Socket Path Mismatch Between NGINX and PHP-FPM<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is the second most common cause and is almost always triggered by a PHP version upgrade through DirectAdmin&#8217;s CustomBuild. When CustomBuild upgrades PHP (for example from 8.4 to 8.5), it creates a new socket file at a new path but does not automatically update every NGINX virtual host configuration that references the old path. The socket file referenced in the NGINX config no longer exists, producing the &#8220;No such file or directory&#8221; error in the NGINX error log.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, list what socket files actually exist on the server right now.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -la \/var\/run\/php\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then find what your NGINX virtual host configuration for the affected domain is actually requesting.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># DirectAdmin stores virtual host configs here:\ngrep -r \"fastcgi_pass\" \/etc\/nginx\/conf.d\/virtual\/yourdomain.conf\n\n# Or search across all virtual hosts at once:\ngrep -r \"fastcgi_pass\" \/etc\/nginx\/conf.d\/virtual\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The value after <code>fastcgi_pass unix:<\/code> must exactly match a socket file that exists in <code>\/var\/run\/php\/<\/code>. If they differ, edit the virtual host configuration to reference the correct socket path, then test and reload NGINX.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nginx -t && systemctl reload nginx<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In DirectAdmin, the per-user NGINX configuration is typically located at <code>\/etc\/nginx\/conf.d\/virtual\/username.yourdomain.conf<\/code>. Changes made here survive CustomBuild rebuilds if they are inside the correct user-controlled configuration file. Changes made to the global NGINX configuration will be overwritten on the next CustomBuild run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 3: PHP-FPM Worker Pool Exhausted (pm.max_children)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PHP-FPM can be running with the correct socket and still produce 502 errors under traffic if every available worker process is busy. When all <code>pm.max_children<\/code> slots are occupied and a new request arrives, PHP-FPM cannot accept the connection. NGINX waits until the <code>fastcgi_read_timeout<\/code> expires and then returns a 502. This produces intermittent errors that worsen as traffic grows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The FPM log will show the explicit warning when this is the case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep \"max_children\" \/var\/log\/php8.5-fpm.log<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you see entries like <code>server reached pm.max_children setting (X), consider raising it<\/code>, the pool is undersized. Open the pool configuration and recalculate the limit based on actual RAM. Find the average memory size of your current PHP worker processes first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ps --no-headers -o \"rss,cmd\" -C php-fpm8.5 | awk '{ sum+=$1 } END { printf (\"%d%sn\", sum\/NR\/1024,\"M\") }'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the output is 45MB per process and you have 1.8GB of RAM available for PHP after accounting for NGINX, MySQL, and OS overhead, your safe maximum is 40 children (1800 \/ 45 = 40). Open <code>\/etc\/php\/8.5\/fpm\/pool.d\/www.conf<\/code> (or the per-user pool file in DirectAdmin&#8217;s setup) and adjust accordingly. Also switch the process manager from <code>ondemand<\/code> to <code>dynamic<\/code> to keep baseline workers pre-warmed and eliminate cold-start latency.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pm = dynamic\npm.max_children = 40\npm.start_servers = 8\npm.min_spare_servers = 4\npm.max_spare_servers = 12\npm.max_requests = 500<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl reload php8.5-fpm<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 4: NGINX FastCGI Timeout Too Short<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Heavy WordPress operations \u2014 large WooCommerce product imports, Elementor page renders with dozens of widgets, or admin screens running complex database queries \u2014 can exceed NGINX&#8217;s default 60-second FastCGI timeout. NGINX abandons the connection and returns a 502 even though PHP-FPM is still processing the request normally in the background.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The NGINX error log will show <code>upstream timed out (110: Connection timed out) while reading response header from upstream<\/code> if this is the cause. In DirectAdmin, add timeout overrides to the domain&#8217;s custom NGINX configuration block, not to the global NGINX config. The correct location for user-level customizations that survive a CustomBuild rebuild is in the per-user virtual host file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fastcgi_connect_timeout 300;\nfastcgi_send_timeout 300;\nfastcgi_read_timeout 300;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is a mitigation, not a fix. A PHP process taking 120 seconds to generate a page has a problem \u2014 a slow database query, a blocking external API call, or a poorly coded plugin. After increasing the timeout to stop the 502s, install the Query Monitor plugin and profile the slow request to find the actual bottleneck. Increasing timeouts without investigating the root cause will surface again as a database connection problem during peak traffic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 5: Linux OOM Killer Terminated PHP-FPM<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If PHP-FPM is found stopped with no errors in its own log, and restarting it temporarily resolves the 502 before it crashes again, the Linux OOM killer is the likely culprit. The kernel OOM killer is a last-resort mechanism that terminates the most memory-hungry process to prevent total system failure. It writes its kills to the kernel log, not to the PHP-FPM log, which is why it is so frequently missed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dmesg | grep -iE \"oom|killed process\" | tail -20\n# Or via journalctl:\njournalctl -k | grep -iE \"oom|killed\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you find entries like <code>Out of memory: Kill process [PID] (php-fpm8.5) score X or sacrifice child<\/code>, the RAM on your VPS is genuinely insufficient for the current <code>pm.max_children<\/code> setting. Restarting PHP-FPM without reducing memory consumption will result in the same crash during the next traffic spike. Your options are: reduce <code>pm.max_children<\/code> to a value your RAM can actually sustain (calculate it properly per Fix 3), identify and remove memory-leaking plugins that inflate worker process size, or upgrade to a <a href=\"https:\/\/www.serverspan.com\/en\/blog\/which-vps-plan-should-you-buy-for-a-personal-website-in-2026\/\">VPS plan<\/a> with more RAM. A <a href=\"https:\/\/www.serverspan.com\/en\/virtual-servers\">ct.Ready plan with 2GB of RAM<\/a> handles a properly configured WordPress stack comfortably; a ct.Entry plan with 512MB is tight and requires extremely lean configuration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 6: MySQL Is Hanging PHP-FPM Workers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is the most underdiagnosed cause of 502 errors and is rarely covered in general WordPress guides. When MySQL is overloaded, locked, or running an extremely slow query, every PHP-FPM worker that makes a database call stalls while waiting for a response. Because PHP workers are blocked \u2014 not crashed \u2014 they appear as active processes, and <code>pm.max_children<\/code> fills up instantly. New requests queue and then time out, producing a 502. MySQL itself does not throw an error visible to WordPress until the connection timeout expires.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check if MySQL is running and identify any long-running or locked queries.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl status mysql\n\n# Connect and check active process list:\nmysql -u root -p -e \"SHOW FULL PROCESSLIST;\"\n\n# Look for queries with a long Time value or State showing \"Waiting for table lock\"\n# Kill a blocking query by its process ID (replace ID with the actual number):\nmysql -u root -p -e \"KILL QUERY ID;\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If MySQL is running but the processlist shows dozens of queries piling up, the issue is likely a missing database index, a runaway WP-Cron job triggering mass simultaneous queries, or a WordPress plugin performing a full table scan on every page load. The <a href=\"https:\/\/www.serverspan.com\/en\/blog\/how-to-fix-the-wordpress-critical-error-complete-recovery-guide-for-any-website\/\">WordPress critical error recovery guide<\/a> covers disabling WP-Cron and isolating plugin conflicts at the database level.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 7: Fail2Ban Blocking Loopback or Cloudflare IP Ranges<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is a specific and frustrating cause that presents as a completely normal 502 from the visitor&#8217;s perspective but leaves no obvious PHP-FPM error in the logs. Fail2ban, which is a legitimate and important security tool, can be misconfigured to accidentally ban <code>127.0.0.1<\/code> (the loopback address) or the IP ranges used by Cloudflare as a proxy. When NGINX attempts to forward a request to PHP-FPM over the loopback interface, it is blocked at the firewall level and returns a connection refused error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Check if the loopback address or Cloudflare IPs are currently banned:\nfail2ban-client status\nfail2ban-client status nginx-http-auth\n\n# Check the active iptables rules for any DROP on loopback:\niptables -L -n | grep \"127.0.0.1\"\n\n# If 127.0.0.1 is banned, unban it immediately:\nfail2ban-client set JAILNAME unbanip 127.0.0.1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To prevent this from recurring, add the loopback address and Cloudflare&#8217;s published IP ranges to fail2ban&#8217;s <code>ignoreip<\/code> whitelist in <code>\/etc\/fail2ban\/jail.local<\/code>. Cloudflare publishes its current IP ranges at <code>https:\/\/www.cloudflare.com\/ips\/<\/code>. If your site runs behind Cloudflare and you are seeing 502 errors only from certain geographic regions, a Cloudflare-side routing issue or an outdated <a href=\"https:\/\/www.serverspan.com\/en\/blog\/the-mechanical-reality-of-network-speed-and-ip-addresses\/\">origin IP<\/a> in Cloudflare&#8217;s DNS configuration is worth checking before digging deeper into the server stack.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bonus: Xdebug Left Enabled in Production<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Xdebug is a PHP debugging extension that should never be loaded on a production server. When active, it adds 30 to 60MB of overhead per PHP worker and significantly slows execution. On a VPS where 40 normal workers fit in RAM, Xdebug reduces capacity to 15 workers before exhausting memory, directly causing the pool exhaustion 502 described in Fix 3. This is particularly common after a developer tests locally, exports a configuration, and it lands on production with the Xdebug ini still enabled.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Check if Xdebug is active:\nphp -m | grep -i xdebug\n\n# Find the configuration file enabling it:\nfind \/etc\/php\/8.5\/ -name \"*.ini\" | xargs grep -l \"xdebug\"\n\n# Disable it by commenting out the extension line:\n# Change: zend_extension=xdebug.so\n# To:    ;zend_extension=xdebug.so\n\nsystemctl reload php8.5-fpm<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Rapid-Fire Diagnostic Checklist<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you are mid-incident and need a structured sequence to follow, use this order. Each step eliminates a category of causes before moving to the next.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Read <code>\/var\/log\/nginx\/error.log<\/code> \u2014 identify the exact upstream error string before touching anything.<\/li>\n<li>Run <code>systemctl status php8.5-fpm<\/code> \u2014 is the service running? If not, start it and check <code>journalctl -xeu php8.5-fpm<\/code> for the reason it stopped.<\/li>\n<li>Run <code>ls \/var\/run\/php\/<\/code> and compare to <code>grep fastcgi_pass \/etc\/nginx\/conf.d\/virtual\/yourdomain.conf<\/code> \u2014 do they match exactly?<\/li>\n<li>Run <code>dmesg | grep -i oom<\/code> \u2014 was the process killed by the kernel?<\/li>\n<li>Run <code>grep \"max_children\" \/var\/log\/php8.5-fpm.log<\/code> \u2014 is the worker pool hitting its ceiling?<\/li>\n<li>Run <code>systemctl status mysql<\/code> and <code>SHOW FULL PROCESSLIST<\/code> \u2014 is MySQL blocked or overloaded?<\/li>\n<li>Run <code>iptables -L -n | grep 127.0.0.1<\/code> \u2014 is fail2ban blocking loopback traffic?<\/li>\n<li>Run <code>php -m | grep xdebug<\/code> \u2014 is a debugging extension inflating memory consumption?<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">If you work through this entire checklist and the 502 persists, the issue is almost certainly environmental: a corrupted PHP binary after a botched update, a full disk preventing socket file creation, or a hardware-level problem on the underlying node. Check disk space with <code>df -h<\/code> and inode usage with <code>df -i<\/code> before escalating. If the server is managed through our <a href=\"https:\/\/www.serverspan.com\/en\/webhosting\">web hosting<\/a> plans, open a support ticket with the output of the NGINX error log attached \u2014 a 502 on managed infrastructure means something on the server side needs investigation at the <a href=\"https:\/\/www.serverspan.com\/en\/blog\/beyond-proxmox-the-2026-virtualization-landscape-and-what-comes-next\/\">hypervisor level<\/a>.<\/p>\n\n ","protected":false},"excerpt":{"rendered":"<p>A WordPress 502 Bad Gateway error means NGINX received the request, forwarded it to PHP-FPM for processing, and got no valid response back. The fix is almost always one of seven things: PHP-FPM is stopped, the socket path is mismatched, the FPM worker pool is exhausted, NGINX&#8217;s FastCGI timeout is too short, the Linux OOM [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2553,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,12],"tags":[14300,14313,14314,14305,14308,14303,14310,14298,14307,14315,14302,14311,14299,14306,14301,14309,14312,14304],"class_list":["post-2552","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-virtual-servers","tag-directadmin-502-bad-gateway","tag-fail2ban-loopback-502","tag-fastcgi_read_timeout-502","tag-fastcgi_read_timeout-nginx","tag-fix-502-bad-gateway-directadmin","tag-fix-502-vps-wordpress-2026","tag-mysql-hanging-php-fpm-502","tag-nginx-php-fpm-502-error","tag-nginx-php-fpm-502-error-directadmin","tag-oom-killer-php-fpm-wordpress","tag-php-fpm-not-running-wordpress","tag-php-fpm-not-running-wordpress-vps","tag-pm-max_children-502","tag-pm-max_children-502-bad-gateway","tag-wordpress-502-bad-gateway-fix","tag-wordpress-502-bad-gateway-fix-2026","tag-xdebug-production-502","tag-xdebug-production-server"],"_links":{"self":[{"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2552"}],"version-history":[{"count":2,"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2552\/revisions"}],"predecessor-version":[{"id":2555,"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2552\/revisions\/2555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/2553"}],"wp:attachment":[{"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.serverspan.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}