Apache is one of the most used web servers with lots of power and capability along with simplicity. You can configure apache very easily which proves its simplicity. For power and capability, I must say that for almost everything you have some configuration to manage. Also, apache has proven track record of handling very high traffic site or to perform a very heavy operation on the web. But still sometimes depending upon your environment you need to tune our apache for better performance. In this post, we will discuss some of the common apache performance tuning tips which can improve apache performance.
Why need Apache Performance Tuning
Well, this is a very odd topic, but one should understand why we need apache performance tuning. Also one should understand that when we should look for tuning the performance of apache. Primarily Apache performance tuning needed for following two reasons:
- Optimal use of your server hardware resources: Your server hardware should not be used too much in normal load.
- Fast loading of your pages at your web site user end: If your server load or traffic is not too high and still page load is very slow then you should think to tune your apache.
In further sections, we will look into the configuration and practice for apache performance tuning.
Remove Unused Apache module
Only load the module which is required and in used for your web application hosted with apache and remove unused apache module. Because if you are loading the module which is not for your use then you are creating unnecessary overhead on your apache which will increase use of your server hardware also may be it will impact your website page load. So always load the required module.
You can disable unused module by going to your httpd.conf. Below is few rule to disable module:
- Search for your module on apache website and check if it is experimental, if the module is experimental then you should not use and removed.
- Remove the auto indexing if it is not used.
- Apache authentication module need to be removed if it is not used.
- The mod_status module need to be removed if you are not doing debugging.
- The mod_env module need to be removed if you are not setting any apache environment variable.
- IF you are not using alias then you should remove mod_alias module.
- Please remove mod_php module if your application hosted on apache is not in PHP.
- If you are not using URL rewriting then you should remove mod_rewrite module.
AllowOverride to None
If you will set directive AllowOverride to none the apache will not lookup for configuration file .htaccess in every directory. This is a real performance gain. If you think that your application does not have .htaccess file then it is always a good idea to make AllowOverride none. Also if you have dedicated server then you can try to write all your domain specific configuration into the virtual host file and set AllowOverride to none.
Carefully write your Rewrite Rule
I have seen that most of the people write rewrite rule to only fulfill their purpose not to have optimized rewrite rule. You should write rewrite rule with less number of internal or external redirect. Also, write the rule in a correct precedence and with correct regular expression matching the pattern. Do not go crazy and write too may rewrite rule which works in few specific cases. Every rule gets match against your request if you will write generic rule without putting any hard condition. So it is always to write the optimized rewrite rule to get better page load time.
Optimize logging and I/O
Logging is very helpful for debugging and analysis purpose. If you have enabled the access log then you can easily check the user IP, browser and user behavior. Enabling error logs are always a helpful to determine the error and behavior of the error.
Now just think about the very heavy traffic website. Every hit to the website will try to write in a log file. The log file will also grow bigger and bigger and write in bigger files is always an affair of time. A high rate of disk I/O can impact the performance of your overall web server. So you should always optimize the logging and decide on how to log and what to log. Below is some suggestion to optimize logging to decrease some disk I/O.
- Always rotate logs: Always rotate your apache log event on daily or hourly basis. If you site has millions of page views then rotate it on hourly basis.
- Exclude un-necessary files from your log: If you think that the log for your static media file does not require then please do not log it.
- Always use Correct Format: Use correct format of log. If you do not think that any information is required then please remove it.
- Log Level: Do not use log level to debug on production.
Put HostNameLookups to off
Turning HostNameLookups to off is good idea if not require. Because if you will set it on then to finish every request apache will do DNS lookup and the un-necessary latency in every request will be added. This is very simple yet powerful point to optimize your web page load time.
Various time people use domain name instead of IP in Allow From or Deny From. This will create two unnecessary DNS lookup and add unnecessary latency to your every user request. Always avoid this if possible.
Always enable FollowSymlinks. This will reduce the system call by apache when looking for any file during any request. For example if you will access index.html without FollowSymlinks enable then it will make call to /www, /www/htdocs and /www/htdocs/index.html. So you can save the call count by enabling FollowSymlinks.
Use KeepAlive Correctly
If you will enable KeepAlive your server will use the same connection to deliver multiple files of the file of a particular request made by the browser. Since the same connection is used to deliver multiple files in some cases the user experience of your website will be faster. If you will configure the KeepAlive correctly then it will even decrease the loading time to 50%.
But incorrect use of the KeepAlive will also badly impact your server and page load. Because KeepAlive consumes more RAM and turns down the CPU utilization. So you need to enable KeepAlive and its related configuration very carefully. There are various aspects you need to consider while changing KeepAlive setting for doing apache performance tuning.
- Think About your hardware: If you have the crisis of RAM please do not enable KeepAlive on. But on another side, if you have sufficient RAM then you can think of enabling KeepAlive.
- Traffic behavior of your website: If your site has a high volume of the concurrent user then enabling KeepAlive will bump up the RAM use, so enabling KeepAlive will not be a good idea in this case. But if your traffic is disconnected and frequency of concurrent user is low then definitely you should enable the KeepAlive.
- Type of website: If your site has a high amount of images and other media on every page then enabling the KeepAlive will always be beneficial for you. Because on the same TCP connection, multiple requests will serve.
Since all above 3 might contradict with each other, always use KeepAlive correctly to get better performance.
KeepAlive has following 3 Configuration variable setting which needs to be configured correctly:
- KeepAlive: Value of KeepAlive can either be on or off. To enable KeepAlive, set its value to on.
- MaxKeepAliveRequests : Variable defines number of maximum number of request server by one connection. It should depend on the type of website you have hosted.
- KeepAliveTimeout : Number of second to wait for request from the same persistent connection. A lower number will be good for your RAM. Maybe you can set between 2-5 second.
Optimize your Server Operating System
For Apache performance tuning, first, your operating system of the server should be optimized. If we will about server operating optimization in detail then a book of content will be less. But if you are buying any server from any hosting provider then they provide server fairly optimized. All you need to do is to modify some setting as per your need. Below is some area where you need to do optimization to gain some good response on your web page load:
- Think about your server firewall rules: Sometimes complex rule written on iPtable or any system firewall tool might slow down inbound and outbound connectivity. This might slow down your server performance and connectivity of user to your server.
- Optimize RAM: Optimize your overall RAM uses of your operating. To get better performance you can also increase memory cache so that more file get placed in memory so that operation will be faster.