Apache high CPU usage – how to protect your server from overload?
In our role as Server management specialists for web hosting companies, server health monitoring and maintenance is a routine activity we perform in our customers’ servers.
A common issue we notice during server monitoring is the server load intermittently going high, usually caused by Apache high CPU usage. As Apache is a commonly used web server, we give it top priority in our support services.
By running the
top command, we can see that Apache takes up about 100% CPU, in this snippet:
%CPU PID USER COMMAND
100 28480 www-data /usr/sbin/apache2 -k start
A server that has its CPU maxed out, can crash and cause a downtime. So, identifying the reason for Apache high CPU usage and resolving it promptly is a task we perform, as soon as we notice such server issues.
Today, we’ll see how we debug and prevent high CPU usage due to Apache service.
What causes Apache high CPU usage?
In our server management services, we’ve noted various scenarios that can cause Apache to use up the server CPU, some of them being:
- A poorly coded web application
- 3rd party plugins in applications
- Certain Apache modules
- Too many users or connections
- A single user abusing the server
- Vulnerabilities or exploits in web applications
- Outdated or insecure Apache version
- Malware or malicious scripts in websites or server
To dig in to the actual root cause, we follow an efficient and fool-proof debugging strategy.
[ You don’t have to lose your sleep to keep your web server fast and stable. Click here to know how our Dedicated Server Admins can help you deliver reliable services. ]
How we debug Apache high CPU usage?
In every load debugging scenario, we follow a systematic 3-step approach to fix the issue.
- Find the over-loaded resource – Resource could be CPU, memory, I/O, etc. We examine the ‘top’ results for sometime to pinpoint the problematic resource and service, or else it can lead to wrong debugging.
- Find the service hogging that resource – Web, mail, database or any other service in the server can abuse the resource. We map the resource usage with the listed command, to identify the service.
- Find the user abusing that service – In a shared server, there may be multiple accounts. So, detecting and suspending the abusive user is very important to assure server stability.
For troubleshooting a physical server or a hardware virtualized instance, ‘atop’ is the tool we use. In an OS virtualization environment, we usually use the regular ‘top’ command. For VPS node troubleshooting, we go for ‘vztop’ utility.
From the results of these utilities, we detect the service that is taking up the CPU resource. Once the service is pinpointed, we find out the process and the user that is hogging up the resources.
To list the files and actions being done by each process, we use ‘lsof‘ command. Using ‘pstree’, we look for any suspicious or high number of processes.
We use ‘netstat‘ and other network related commands to check for too many connections from one particular IP or an IP range. To track the details of certain suspicious commands, we use ‘strace‘ utility.
Enabling extended logging in servers helps us to examine the user specific logs such as access log and error log and to find out the details of high CPU usage for Apache.