Introduction
A sudden spike in CPU usage or server load on a web server can slow down websites, trigger 504 Gateway Timeout errors, or cause complete downtime. Whether you are running Nginx, Apache, or a reverse-proxy combination, high resource usage usually stems from unoptimized configurations, heavy database/PHP queries, bot scraping, or traffic spikes.
In this guide, we will walk through a step-by-step process to diagnose the root cause of high CPU load on Linux servers and execute the exact fixes needed to keep your server running fast and stable.
Step 1: Diagnose the Overloaded Resource
Before tweaking web server settings, identify which process or user is consuming the CPU.
1. Check Real-Time Process Activity
SSH into your server and run:top
Or, if installed, use htop for a better visual interface:htop
Look at the %CPU column to identify whether the heavy processes belong to nginx, httpd/apache2, php-fpm, or mysqld.
2. Sort Heavy Processes by CPU Usage
Run the following command to sort the top 10 CPU-hogging processes:ps aux --sort=-%cpu | head -n 10
Step 2: Fix High CPU Usage in Nginx
If Nginx processes are spiking, it is typically due to worker process misconfigurations, lack of caching, or worker connection limits.
Fix 1: Optimize Nginx Worker Processes
Open your Nginx configuration file:nano /etc/nginx/nginx.conf
Ensure your worker_processes matches your physical CPU cores (or set it to auto so Nginx handles it dynamically):
worker_processes auto;
worker_connections 2048;
Fix 2: Enable Static File Caching & Compression
Serving uncompressed static files forces Nginx to work harder on every request. Enable GZIP compression and browser caching in your server block:
Enable Gzip Compression
gzip on; gzip_comp_level 5; gzip_types text/plain text/css application/json application/javascript text/xml;
Fix 2: Lower KeepAlive Timeout
After modifying settings, test and reload Nginx: `nginx -t` `systemctl reload nginx`Step 3: Fix High CPU Usage in Apache
Apache high load is frequently caused by process-spawning loops in old MPM configurations or excessive KeepAlive connection holding times.
### Fix 1: Switch to Event MPM
If Apache is using the prefork module, every connection creates a full process, destroying CPU and RAM efficiency. Switch to the Event MPM module paired with PHP-FPM.
Check your active MPM module:
`apachectl -M | grep mpm`
Adjust your event configuration inside `/etc/httpd/conf.modules.d/00-mpm.conf` or `/etc/apache2/mods-available/mpm_event.conf`:
Long KeepAlive timeouts keep Apache worker slots busy waiting for idle clients. Reduce KeepAliveTimeout to 2 or 3 seconds in your Apache config:
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 3
Restart Apache to apply: systemctl restart apache2
Step 4: Address Underlying PHP & Database Bottlenecks
In many cases, the web server (Nginx/Apache) appears to take high CPU only because it is waiting on slow PHP executions or unindexed MySQL queries.
- Enable PHP OPcache: OPcache stores precompiled script bytecode in memory so PHP doesn't have to load and parse scripts on every request.
- Enable Slow Query Logging in MySQL: Find queries that block CPU threads by adding this to
/etc/my.cnf:
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
Need Proactive 24/7 Server Management?
Tuning web servers and troubleshooting intermittent CPU spikes requires constant monitoring and expertise.
At CoreServerSupport, our team of Linux & Cloud infrastructure experts provides 24/7 server monitoring, performance tuning, hardening, and emergency troubleshooting to guarantee 99.99% uptime for your business.
Contact CoreServerSupport Today for a free server performance audit!