Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ozanerturk/721dd6b60fd33d0704044e09c2a7c647 to your computer and use it in GitHub Desktop.
Save ozanerturk/721dd6b60fd33d0704044e09c2a7c647 to your computer and use it in GitHub Desktop.
Wordpress Stuck/hang/inresponsive/504/timeout/CPU
If you're seeing 100% CPU usage and `504 Gateway Timeout` errors from NGINX, but you've disabled plugins and the issue persists, you're likely dealing with a **lower-level issue** in WordPress or the server environment. Here's how to **debug this methodically and critically**:
---
## 🔍 1. **Check What Is Actually Using CPU**
Run the following command on the server to identify what's consuming CPU:
```bash
top -o %CPU
```
Or for a more readable breakdown:
```bash
htop
```
Look for:
* Is it `php-fpm`, `mysql`, or some rogue process?
* Are there many worker processes?
* Are multiple cores maxed, or just one?
---
## 🔧 2. **PHP-FPM Tuning / Bottleneck**
If `php-fpm` is the top CPU consumer:
### Check the pool config:
```bash
cat /etc/php/*/fpm/pool.d/www.conf
```
Things to review:
```ini
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
```
If `pm.max_children` is too low, requests queue and time out.
If too high, server swaps → CPU spikes.
---
## 🧠 3. **Run strace on High-CPU Processes**
To inspect what a high-CPU process is doing:
```bash
sudo strace -p <PID>
```
You'll see if it's waiting on I/O, stuck in loops, calling specific PHP files, etc.
---
## 🔎 4. **Find the Entry Point of the Problem**
If it’s `php-fpm`:
```bash
sudo lsof -p <php-fpm-pid>
```
See what file is being processed. Could be a heavy page or loop.
Also, log slow PHP scripts:
In `php.ini` or `www.conf`:
```ini
request_slowlog_timeout = 3s
slowlog = /var/log/php-fpm.slow.log
```
---
## 🔄 5. **Check MySQL / MariaDB Queries**
If it's `mysqld` using CPU, run:
```sql
SHOW PROCESSLIST;
```
or from CLI:
```bash
mysqladmin processlist
```
Look for long-running or locked queries.
Also useful:
```sql
SHOW ENGINE INNODB STATUS\G;
```
If CPU is high due to `mysql`, it could be:
* A bad plugin leaving a query open
* A WooCommerce table like `wp_options` bloated
* Autoloaded options
Check this:
```sql
SELECT COUNT(*) FROM wp_options WHERE autoload = 'yes';
SELECT SUM(LENGTH(option_value)) AS autoload_size FROM wp_options WHERE autoload = 'yes';
```
> Should ideally be < 1MB in size. If it's >10MB, this can cripple performance.
---
## 📉 6. **Check NGINX and PHP Logs**
* NGINX error logs:
```bash
/var/log/nginx/error.log
```
* PHP error log (often in `/var/log/php*`)
* Check for:
* Upstream timeouts
* Script errors
* Socket errors (`upstream: "fastcgi://unix:/run/php/php7.x-fpm.sock"`)
---
## 🔥 7. **Disable Themes + Core Check**
Since you disabled plugins, try **switching to a default theme** (`twentytwentyfour`) to rule out theme issues:
```php
define('WP_USE_THEMES', false);
```
Or via DB:
```sql
UPDATE wp_options SET option_value = 'twentytwentyfour' WHERE option_name = 'template' OR option_name = 'stylesheet';
```
Also try:
```php
define('WP_DISABLE_FATAL_ERROR_HANDLER', true);
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
```
---
## 🧰 8. **Run WordPress Through CLI**
Try using `wp-cli` to run:
```bash
wp cron event list
wp option list --autoload --format=csv > autoload.csv
```
And check for stuck cron jobs or huge autoloaded options.
---
## 🧪 9. **Profiling with Xdebug or Tideways**
If you still can’t isolate the cause, install a PHP profiler:
* [`xdebug`](https://xdebug.org/docs/profiler)
* [`tideways`](https://tideways.com/profiler)
This gives you exact lines of PHP code causing high CPU.
---
## 🛡️ 10. **Other Considerations**
* DoS? Check NGINX access logs for sudden surges:
```bash
tail -n 1000 /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head
```
* Malware? Run:
```bash
clamscan -r /var/www/html
```
---
## Next Step
If you want, you can upload the output of:
```bash
top -b -n 1
```
and your:
* `/etc/php/*/fpm/pool.d/www.conf`
* MySQL processlist
* Last 100 lines of `access.log` and `error.log`
I'll help you pinpoint the bottleneck exactly.
---
Would you like help writing a short script to log CPU usage and top 5 PHP scripts in real-time?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment