Bobcares

How to fix Apache “Too many connections” issue

by | Jul 17, 2018

Are your websites showing blank pages? Is your server load high? Do you see hundreds of Apache process in your admin console?

You are not alone.

Too many connections” is a common issue in LAMP (Linux-Apache-MySQL-PHP) servers that’s most commonly used in Web Hosting.

Here at Bobcares, our Hosting Support Engineers support LAMP servers of web hosts, digital marketers, etc., and we’ve seen these reasons for Apache’s too many connections error:

– Unoptimized Apache settings

The default settings of Apache can induce a heavy load on memory if the sites are content rich dynamic pages (eg. WordPress). If the memory usage is not carefully calibrated and balanced between HTTP and Database servers, the server will freeze.

– Brute force attacks

The internet is infested with Bots and Bot-masters that try to infect websites and spread their malware everywhere. They do this by exploiting website vulnerabilities and cracking login details through brute forcing. When such a large scale attack occurs, the server can freeze.

– Database server (eg. MySQL) limits

Database servers also have built-in connection limits that can fail to rise up to the traffic, causing “too many connections” error.

Now, let’s take a look at the ways in which we resolve these issues in our customer servers:

 

1. Fix Apache configuration

Misconfigured Apache is the most common reason we’ve seen for the “too many connections” error.

We’ve seen these common configuration issues while troubleshooting customer servers:

  • Unsuitable MPM for the server – Apache servers use Prefork MPM (Multi-Processing Module) by default. This takes too much memory and suited for low traffic, simple (text based) websites. Content rich dynamic websites (eg. WordPress) are better served by Worker MPM (or Event MPM for very busy sites).
  • Insufficient memory allocation – Most web servers also run Database services, DNS services and Mail services. So unless the MaxClient (MaxRequestWorkers in Apache 2.4) and StartServer settings are carefully calibrated to match available memory, Apache will resort to using the slower swap space, and the whole server will grind to a halt.
  • Too liberal KeepAlive values – Apache uses a feature called “KeepAlive” to make connections faster, but malicious bots and slow devices can hang on to those connections for a long time, and starve server resources.

When we are called in to fix an unoptimized server, we estimate the (1) average & max traffic on the server, (2) memory usage per server request, and (3) the trend of resource usage in all services.

Based on this data, we tweak the server settings to keep the server load less than 1.0.

We follow-up with our 24/7 monitoring service and make continual adjustments to the configuration until the server can comfortably take care of the traffic directed at it.

 

2. Block DoS and brute force attacks using firewalls

Most attacks behave in predictable ways.

Some send hundreds of connections in seconds, some establish no-response connections, and some others use abuse-listed IPs.

Our dedicated support engineers fight this in 4 ways:

  • Harden the network & kernel settings – Some attacks such as Smurf attacks, Slow Loris attacks and Syn Flood attacks can be blocked by hardening the basic network settings. So, that’s the first step in our network security measures.
  • Setup a strong firewall – Once the kernel is secure, we configure the firewall to detect port flooding, port scanning, and other such behavior that’s indicative of abusive behavior. We then set these IPs to be automatically blocked, so that legitimate requests are not affected.
  • Enable anti-DoS modules in Apache – As the final layer of defense, we setup anti-DoS modules in Apache such as mod_evasive and mod_qos which limits the bandwidth per visitor. In this way, the server won’t be affected even if one user initiates a lot of connections (common to DoS attacks and bots).
  • Setup a web application firewall – Web Application Firewalls such as mod_security blocks many common attacks (such as XSS, CSRF, SQLi, etc.) based on connection signatures. We configure the system to detect and block attacker IPs, so that attackers won’t be able to upload a malware, much less execute it.

 

3. Use a reverse proxy or caching server

The most effective solution that we’ve found against Apache overload issues is to use a Reverse Proxy server in front of Apache.

The reality is, Apache is not all that great in handling multiple simultaneous connections.

So, we put caching service such as Nginx or Varnish in front of Apache that reduces the hits sent to the Apache service.

These services store a copy of all pages served in the memory, and when another request comes for the same page, it quickly sends back the cached copy.

In this way, we’ve been able to cut down the load to Apache to less than 40%, while using less resources than the previous Apache stand-alone process.

 

4. Optimize database settings

In a few rare cases, we’ve seen MySQL settings to be unsuitable for the traffic the server was receiving.

When we detect MySQL issues, we use the same approach as we did with Apache. We log the requests sent to the database, and find out the actual number of connections.

Once we know the exact load exerted on the database, we tweak the Buffer size, Cache size, Sort table size, Max user connections, and more to eliminate aborted queries.

The server performance is observed for a few days to make sure the resource allocation works well with Apache settings and incoming traffic.

If needed, we add PHP opcode caches to further reduce database server load.

 

Conclusion

Apache’s “too many connections” is caused by memory overage, brute force attacks, MySQL limits, and more. Today we’ve seen the top causes for this error, and how Dedicated Support Engineers at Bobcares resolve and prevent it from happening again.

 

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

SEE SERVER ADMIN PLANS

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF