In our role as Server Support Engineers 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 Support Engineers 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.
How to fix Apache high CPU usage?
Once we identify the abusive user, process or the IP address, we take immediate corrective action to prevent further resource abuse in the server.
In our Dedicated server maintenance services, Bobcares helps server owners deliver high uptime and performance for their websites, with these actions:
- Blocking the suspicious IP address in firewall
- Disabling the problematic plugin or module
- Suspending the user account that is abusing
- Patching and updating vulnerable software
- Tweaking Apache and PHP configuration
- Scanning and removing infected files
- Limiting the number of connections per user
- Hardening Apache and related services
[ You don’t have to be a server expert to keep your site fast and stable. Our Support Engineers will keep your server blazing fast through 24/7 monitoring & maintenance. ]
How to prevent Apache from hogging the CPU?
A fast CPU and adequate memory (RAM) are the basic necessities for a high-speed Apache web server. So, whenever we configure and setup a server, the first thing we do is to ensure that the server resources are adequate for the traffic and websites hosted in them.
But simply adding resources, without utilizing them efficiently, can lead to wastage and loss. That’s where Apache performance tuning comes into picture.
With the optimal hardware resources, we’ve been able to limit CPU usage of Apache by about 50%, by tweaking its configuration settings. Here we’ll shed some light into how we do that performance tuning.
- By loading only the required Apache modules during compile time, we limit the resources required for Apache to start
- By running the web server processes in the respective usernames, we are able to easily track and isolate abusers.
- We disable unnecessary modules and maintain the minimal required installation of Apache to reduce resource usage.
- Configuring web application firewalls and limiting the access helps us to ward off attackers.
- Enabling firewalls and DDoS protection is a crucial measure we do to block abusive IPs before they crash the webserver.
- Setting up resource limits for each user helps us to keep a limit on the resource usage spikes.
- Using malware scanning, we track and disable any malicious scripts that can hog the CPU.
- Monitoring the web server for security issues and resource usage round the clock, helps us to promptly identify the issues.
- We custom-compile Apache MPMs to handle the traffic and resources in each server optimally.
- Tweaking MySQL and PHP settings helps to avoid the bottle-necks they incur on the web server.
In addition to these proactive tips, we also configure Apache with certain run-time parameters, that enables us to further reduce the CPU usage incurred by the web server. These include:
- Disabling DNS lookups
- Setting Off AllowOverride
- Enabling FollowSymLinks
- Limiting MaxClients
- Configuring MaxSpareServers and MinSpareServers
- Rate limiting MaxRequestsPerChild
- Setting KeepAlive On
- Configuring TimeOut values
[ Use your time to build your business. Leave your servers in our expert hands. Support Engineers plans start as low as $12.99/hr. ]
Conclusion
Configuring Apache for minimal CPU usage is tricky, as there are no hard and fast rules or ‘one-size fits all’ tips. Each web server is different and understanding these requirements is vital before experimenting with the settings.
Our 24/7 expert technicians keep an eye on the resource usage, process performance, network connections, etc., in the servers and curb the spikes instantly to avoid server crashes.
At Bobcares, we also design and implement custom webserver architecture using NginX, Apache, etc. to ensure high available and high speed infrastructure for our customers.
If you’d like to know how to manage your webservers efficiently for your business purposes, we’d be happy to talk to you.
0 Comments