How to optimize Apache Server for Better Performance
Website speed is totally connected with server performance, and thus, you have to optimize apache and reduce RAM usage in order to get the most from your online presence. Apache has great features and a strong development and support base, but the problem with Apache is associated with high consumption of RAM or memory, and this will be really frustrating for any webmaster because upgrading the RAM on a VPS or dedicated server isn’t so cheap. I faced a similar problem with my VPS account. I have 8GB of RAM, but Apache sucks almost 90% of my RAM memory.
So I decided to look into this and fix the issue once and for all, so I started digging deep into the issue by first exploring its cause, and as usual the problem is, Apache is simply reserving the RAM memory and obviously resulting in to my VPS node going down. Even dedicated or cloud based servers can also get performance level issues. So it is better to fix this memory consuming issue ASAP to avoid future circumstances.
The Solution:
There are two possible solutions, one by tweaking and reducing RAM usage. Secondly, simply switch to Nginx, which consumes very low memory and the performance level is really astonishing as compared to Apache. I did a lot of load testing etc and finally agreed that Nginx is way better in terms of performance and handling multiple concurrent connections at a time. I will write a complete comparative report and review of Apache and Nginx in my next article.
Optimizing Apache and Reducing RAM usage:
Apache has a special feature called Multi-Processing Module (MPMs), which helps it handle the multiprocessing of threads, which obviously increases your server load by running spare servers unnecessarily. This will easily suck up all your RAM in no-time. I tried many ways to reduce the RAM usage by disabling unused PHP modules, uninstalling useless packages etc. But it barely reduced the usage, so I took this issue seriously (because my hosting provider keeps on sending me warnings about crossing the RAM usage limits).
So obviously as a primary step I started digging into the HTTPD configuration and I find out about MPM, so I tweaked a few settings which dramatically reduced my RAM usage, also I didn’t see any performance issues with the website as well. Let me explain to you about what exactly I did…
Note: Please apply these settings at your own risk, I do not hold any responsibility if something goes wrong, don’t try this on a production server directly.
Steps to Optimize Apache:
1) Open your Apache configuration file which is normally located at..
/etc/httpd/conf/httpd.conf
2) Then go to following location where you can find this code..
StartServers 8 MinSpareServers 1 MaxSpareServers 10 ServerLimit 50 MaxClients 50 MaxRequestsPerChild 4000
For your understanding here is what every term means..
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# ServerLimit: maximum value for MaxClients for the lifetime of the server
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
3) Just reduce the number of server start to lower number, in my case I set it as “1”. Also set the minimum spare server value to “1”. Set Max Spare server value to 4 or 5, server limit to 50 or 60 (test with different values).
4) Just below to the prefork settings, you can see worker settings as well, workers can better your website performance if you optimize it correctly. It will look like this.
StartServers 8 MaxClients 50 MinSpareThreads 5 MaxSpareThreads 4 ThreadsPerChild 25 MaxRequestsPerChild 0
You can actually change the values according to your convenience, also check out what those terms mean by…
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
5) This will almost do the job, but as I said, work a little on your enabled modules which are unnecessarily lying in your configuration and wasting server resources. Please do check before you disable any module what its functionality is, then you can go ahead with the disabling.
6) Remove unnecessary packages lying on your servers, which run in the background and sucks all your memory without your knowledge. Disable unnecessary background running processes as well.
7) Another simple way to optimize your website performance is by installing the mod_pagespeed module, this can also help in optimizing your website performance dramatically.
Please try experimenting with different values in prefork and worker configurations to see which can improve your website performance and reduce your RAM usage as well.
Server performance and website speed are almost everything now, as the recently revealed Google updates indicate that website speed is considered as the first preference in the rankings. So, Apache Performance Tuning is a compulsory step you have to carry out if you are a webmaster and want a better user experience on your website or app.
I hope you enjoyed this article and if you have any questions, suggestions and comments, please ask in the below section, also if you have any specific questions to ask, please ask me in the comments section.