A 504 Gateway Timeout means a server that was waiting for a response from another server gave up. In a WordPress context, it typically means PHP or the database took too long to respond and the web server stopped waiting. Here's how to work out what's causing it and what to fix.
A 500 Internal Server Error is a generic "something went wrong on the server" response, it could be caused by dozens of different things. A 503 Service Unavailable means the server is temporarily unable to handle the request, often due to overload or scheduled maintenance. A 504 is specifically a timeout: one server was waiting for an upstream response that never arrived within the allotted time window.
The distinction matters for diagnosis. A 500 points you toward error logs and PHP errors. A 503 suggests resource exhaustion. A 504 tells you something is taking too long to execute, a specific process, query, or external call is the bottleneck, not a crash.
Slow PHP execution: A plugin or theme running a heavy PHP process that exceeds the server's execution time limit. Common culprits are import/export plugins, backup plugins running at peak traffic time, WooCommerce tax/shipping calculation plugins, or poorly written queries loading thousands of database rows on each request.
Server resource overload: Too many concurrent requests for the server's available CPU and memory. Common on shared hosting during traffic spikes. LiteSpeed with page caching significantly reduces this risk because cached pages are served without executing PHP at all, the PHP process that would time out simply never runs.
CDN or proxy timeout: If your site is behind Cloudflare or another CDN, the CDN may time out waiting for your origin server. Cloudflare has a default 100-second origin timeout before returning a 524 error (a Cloudflare-specific variant of the 504). If you're seeing 524s rather than 504s, the problem is specifically between Cloudflare and your origin.
External API calls blocking execution: A plugin making calls to an external service, a payment gateway, email provider, social media API, stock feed, can hold up PHP execution if that external service is slow or unresponsive. The PHP process waits for a response that doesn't come, and eventually the web server times out waiting for PHP.
Long-running database queries: Complex or unoptimised MySQL queries can take many seconds to complete, particularly on large databases with poor indexing. The Query Monitor plugin can identify queries taking over 1 second, which are candidates for optimisation.
Check your server error logs first. In cPanel this is under Logs > Error Log. The log will typically show which script was running when the timeout occurred, and how long it ran before being terminated. An entry like execution of script '/wp-cron.php' timed out after 60 seconds immediately tells you the cause.
If you have WP_DEBUG_LOG enabled, check /wp-content/debug.log as well. Some plugins log their own errors there before timing out. Enable debug logging temporarily if you don't already have it on:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
If the 504 is intermittent rather than consistent, check your server resource usage in your hosting control panel. Consistent CPU spikes or memory usage near your plan limit during the errors point to a resource capacity issue rather than a specific plugin problem.
Deactivate recently installed or updated plugins and test. If the 504 stops, the new plugin was the cause. Re-enable plugins one at a time to isolate it. Pay attention to plugins that run background tasks, import data, or make external API calls, these are the most frequent 504 triggers.
Increase PHP max_execution_time. If a legitimate process (a large import, a database migration) is timing out, you may need to increase the execution time limit. Add to your .htaccess:
php_value max_execution_time 300
Or via php.ini if your host provides one: max_execution_time = 300. Note that increasing execution time is appropriate for one-off admin operations, not for fixing slow front-end pages, those should be optimised, not given more time to run slowly.
Enable server-level page caching. On LiteSpeed with LSCache, cached pages bypass PHP entirely. A request that would have timed out waiting for PHP to execute never reaches PHP in the first place, the cached response is returned in milliseconds. This is the single most effective mitigation for 504s caused by overloaded PHP.
Reschedule background tasks. Backup plugins and maintenance tasks should run at low-traffic times (2am–5am) rather than during business hours. Most backup plugins allow you to configure the backup schedule.
If you're seeing "Error 524: A Timeout Occurred" rather than a standard 504, this is Cloudflare giving up on your origin server. Cloudflare waited 100 seconds for your origin to respond and got nothing. The fix is on the origin side: whatever PHP process is running needs to complete faster, or you need to reduce the number of uncached PHP requests reaching origin. Increasing Cloudflare's origin timeout via their dashboard (available on Pro plans and above) buys time but doesn't address the underlying cause.
Repeated 504 errors that don't correlate with specific plugins or traffic spikes often indicate that the shared server is consistently overloaded, more sites using CPU and memory than the hardware can support. This is a hosting capacity problem that no amount of plugin optimisation will fix. LiteSpeed page caching can help by dramatically reducing the PHP requests the server needs to handle, but if the underlying hardware is overcommitted, timeouts will continue. The real solution is moving to a host with better resource allocation per account.
Cached pages are served before PHP executes. Traffic spikes that would timeout a standard server won't touch a cached LiteSpeed site.
View Hosting Plans