Recently i have posted ’bout how to configure Apache, php and MySQL, as i have already talked, it’s quite common, we don’t bother ’bout the small details like ’cause of apache memory leak’ during configuring apache, php and MySQL in absence of which the performance of the server could really drop down to worst. The idea behind this post is to draw attention towards some of those important but commonly missed details during and after configuration of the server, obviously to avoid conflicts later on.
So let’s first talk about apache memory leak, it’s cause and avoidance. During earlier phase of my carrier in Tekriti, i was asked to install a staging/test server for one of my mega project, i missed those few details casing a lot of trouble later on. Apache was eating a lot of memory causing chock down the server few times during peak load, generated by apache bench and our QA guys, it then takes hell of efforts/googleing, to exactly track down the cause and it’s solution, ultimately i have to reconfigure apache with one of required Multi-Preprocessing Module (read more configuring MPM) and then tweaking apache settings accordingly to avoid the situation.
Assuming that we don’t have any CPU bottleneck (if we really have Server range of Machines) and we are basically concentrating on what’s taking too much of memory. First let’s track down exactly how much memory apache is eating. Fire a command as a root user on terminal window ps -ylC httpd –sort:rss below the RSS column will return non swapped memory used by per apache process in KB. Each process can consumed upto15 MB of memory. Now to exactly know how many established connections are there to your apache server fire the command lsof -i | grep httpd | grep ESTABLISHED | wc -l, the count will tell you how much busy is your server
. Now use top, to ensure if real culprit is apache in your case press shift + M to sort the result, to displaying top memory consuming process, if you see apache all over with very few memory left for other processes this is the right time to get worried. Command ‘top’ will return you exactly how much memory is used, well the real memory used at that moment would be equal to {Total memory – (used memory – cached memory)} obviously to what is shown in your top result, hope it’s not too much so that swap memory is in action if it is let’s pay more attention to apache and then will see if it helps.
So now we have track down our problem, now what should we do in real. Below are some thing we should do to avoid such situation. We should avoid certain things in particular to avoid unwanted load to Apache Server.
- Load Only required Modules
- Handle Fewer Simultaneous Requests
- Recycle Apache Processes
- Use KeepAlives, but not for too long
- Lower your timeout
- Log less
- Don’t Resolve Hostnames
- Don’t use .htaccess
Well these are some of few things which still can help but don’t count on that only, i repeat you really have to reconfigure your apache, if your apache havn’t been configure for it’s best performance.Here is what we have to deal with, “MPM – Multi-Processing Modules“, which is responsible for binding to network ports on the machine, accepting requests, and dispatching children to handle the requests. Only one MPM can be loaded into the server at any time(Configuring MPM with apache). Linux systems can choose to use a threaded MPM like worker or a non-threaded MPM like prefork:
The worker MPM uses multiple child processes. It’s multi-threaded within each child, and each thread handles a single connection. Worker is fast and highly scalable and the memory footprint is comparatively low. It’s well suited for multiple processors. On the other hand, worker is less tolerant of faulty modules, and a faulty thread can affect all the threads in a child process.
The prefork MPM uses multiple child processes, each child handles one connection at a time. Prefork is well suited for single or double CPU systems, speed is comparable to that of worker, and it’s highly tolerant of faulty modules and crashing children – but the memory usage is high, and more traffic leads to greater memory usage.
If you are useing prefork.c
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 100
MaxRequestsPerChild few thousand // (let’s say 1000, you can reset it according to how apache behaves further).
If you are using worker.c
StartServers 5
MaxClients 100
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild few thousand // (let’s say 1000, you can reset it according to how apache behaves further).
Few other Settings to tweak :
Use KeepAlives, but not for too long The default seems to be 15 seconds, but you can get by easily with 5 to 10 seconds.
Lower Your Timeout The default seems set to 300 seconds, since you’re limiting the number of processes, you don’t want one to be “stuck” timing out for too long, make it 180.
Don’t Resolve Hostnames HostnameLookups Off
Don’t Use .htaccess AllowOverride None