Ever visited a website and wondered why the loading time is so slow? This may be due to the Apache server maxing out its load. Depending on how much traffic your site has, from time to time, tweaking Apache can be beneficial as it helps to maximize the server’s load. When just beginning a website, such as an eCommerce site or blog, tweaking your server to optimize Apache may not be crucial, but as you start seeing your traffic increase, it will use up more resources. This is when tweaking can be applied and play a significant part in page loading time.
Below are some steps to improve and optimize Apache, and therefore your website’s loading time.
Note: This applies to any Linux OS.
Pre-Check
Ping/Test server
Before we do any tweaking, double check and test your server using Pingdom. It is recommended to use Pingdom or a similar tool when testing a website rather than simply pinging.
Resources
For the sake of this example, we’ll be using a server with Xeon Quad Core Processor with 2GB of RAM that was set up incorrectly, which caused the server to crash due to high loads.
Tweaking Apache
Check/Edit “KeepAlive” in Apache config file
- Check your server to verify what type of code it is utilizing. Is it static (HTML, HTM, text, images, etc.) or dynamic (PHP, JavaScript, etc.)? In the following example, our server contains static contents. Do make sure that KeepAlive is set to on in your Apache config or .htaccess file.
- Check KeepAliveTimeout and make sure it is set to a low value such as 3 seconds. The higher the number is set, the more resources will be used—which is not what we want.
Use “TOP” command
- Type in “nice top -d 2 -u nobody -c” user nobody is set for Apache. You can learn all the top Linux commands here.
Here is the sample output:
———–
22511 nobody 15 0 83128 6916 3060 S 0.0 0.3 0:00.00 /usr/local/apache/bin/httpd -k
restart -DSSL
22512 nobody 15 0 82996 5400 1720 S 0.0 0.3 0:00.00 /usr/local/apache/bin/httpd -k
restart -DSSL
22513 nobody 15 0 83128 6916 3060 S 0.0 0.3 0:00.00 /usr/local/apache/bin/httpd -k restart -DSSL
22514 nobody 17 0 82996 5448 1756 S 0.0 0.3 0:00.00 /usr/local/apache/bin/httpd -k restart -DSSL
———–
The red column outlines roughly how much RAM each process is using (6MB). This shows us how much we can increase our MaxClient before the server crashes. A simple equation to this is as follows:
MaxClients = TotalServerRam / ApacheChildProcessRAM
Since we have 2GB of RAM and roughly 6MB of RAM being used for each process, the input would be:
MaxClients = 2048 / 6 -> MaxClients = 340 (rounded down)
Because the memory varies from each process, the value range would be +/- 100; however, this is all dependent on what it is utilized for.
To obtain the MaxClient number, we have set this to 500 as it is the most optimum for trial and error.
Edit MaxRequestsPerChild
MaxRequestsPerChild indicates how many requests can be handled in one process. In our case, since MaxClient is 500, this would define how many requests can be served by those 500 processes. This variable is dependent on the traffic your site achieves. If our client wanted 5,000 users accessing a website, we would then set the MaxRequestsPerChild to 10,000. To figure out what the max number of site visits your server can handle is, use the following equation:
Number of visitors = (MaxRequestsPerChild * MaxClients) / Number of requests per visit
Our input -> (1000 * 500) / 1000 = 5000
Post Check
Once all the tweaking has been completed, it’s time to test out if our efforts have been rewarded to optimize Apache. After benchmarking your website, you should see a significant difference prior to the tweaking you’ve made. To benchmark using Apache’s tool, run the following:
ab -kc 1000 -n 10000 https://entersitehere.ca/index.html
You can learn more about Apache’s ab benchmarking tool here.
**KeepAlive: when set to on, it optimizes your website speed and reduces CPU usage.
**KeepAliveTimeout: session is closed after x seconds.
*MaxClient: how many child processes Apache can generate.
*MaxRequestsPerChild: number of requests that can be handled per process.
That’s it! If you have any questions about how to optimize Apache further, feel free to ask us in the comments below.
Be First to Comment