Thursday, July 16, 2015

Fine Tune Apache Web Server

Tuning HTTPD - Apache2 Web Server

Hello there!
I will show you how to configure apache2 httpd web server to withstand heavy load conditions. This is indeed a pre-configuration if you are about to do a performance analysis on your web application running on apache. Adding/modifying the following lines of code to your config file is all you need to do.

Config file can be found either in [/etc/apache2/apache.conf] or [/etc/httpd/httpd.conf]

<IfModule prefork.c>
StartServers       4
MinSpareServers    3
MaxSpareServers   10
ServerLimit      256
MaxClients       256
MaxRequestsPerChild  10000
First of all, whenever an apache is started, it will start 2 child processes which is determined by StartServers parameter. Then each process will start 25 threads determined by ThreadsPerChild parameter so this means 2 process can service only 50 concurrent connections/clients i.e. 25x2=50. Now if more concurrent users comes, then another child process will start, that can service another 25 users. But how many child processes can be started is controlled by ServerLimit parameter, this means that in the configuration above, i can have 16 child processes in total, with each child process can handle 25 thread, in total handling 16x25=400 concurrent users. But if number defined in MaxClients is less which is 200 here, then this means that after 8 child processes, no extra process will start since we have defined an upper cap of MaxClients. This also means that if i set MaxClients to 1000, after 16 child processes and 400 connections, no extra process will start and we cannot service more than 400 concurrent clients even if we have increase the MaxClient parameter. In this case, we need to also increase ServerLimit to 1000/25 i.e. MaxClients/ThreadsPerChild=40

Once you appended the above lines you need to restart apache in order for the configurations to be loaded.

sudo service apache2 restart

Watch The Server

Keep an eye on the number of Apache processes, and the total RAM used. Here's a command that wraps this into a single output that updates every second:

watch -n 1 "echo -n 'Apache Processes: ' && ps -C apache2 --no-headers | wc -l && free -m"

It produces output like this:

Every 1.0s: echo -n 'Apache Processes: ' && ps -C apache2 --no-headers | wc -l && free -m

Apache Processes: 27
            total       used        free        shared      buffers     cached
Mem:        8204        7445        758         0           385         4657
-/+ buffers/cache:      2402        5801
Swap:       16383       189         16194

No comments:

Post a Comment