Server Tuning: Apache

Even though you probably shouldn't be running Apache on the front, it's important to optimize as if you are. Below are techniques to optimize apache.

prefork.c

These techniques have been taken from: http://kb.mediatemple.net/questions/246/Making+it+Better%3A+Basic+Apache+performance+tuning+%28httpd%29#dv and they have been modified to work with <code>free -m</code>

Step 1:

ramCount=$(free -m | perl -pe 's/^Mem:\s*(\d+)\s*.*/$1/ || s/.*//' | grep . | xargs -i{} perl -e 'print eval"({}*1024\)/8"')

Step 2:

ramBase=-16 && for ((;ramCount>1;ramBase++)); do ramCount=$((ramCount/2)); done

Step 3: If you do not have any settings like the following, use these:

<IfModule prefork.c>
StartServers 0
MinSpareServers 0
MaxSpareServers 0
ServerLimit 0 
MaxClients 0 
MaxRequestsPerChild  0 
</IfModule>

Step 4: Run the following (back up your httpd.conf file first!):

perl -0 -p -i -e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?StartServers\s*?)\s\d+/\1\ '"$ramBase"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MinSpareServers\s*?)\s\d+/\1\ '"$ramBase"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxSpareServers\s*?)\s\d+/\1\ '"$(($ramBase*2 + 1))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?ServerLimit\s*?)\s\d+/\1\ '"$(( 50 + (($ramBase**2)*10) + (($ramBase-2)*10) ))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxClients\s*?)\s\d+/\1\ '"$(( 50 + (($ramBase**2)*10) + (($ramBase-2)*10) ))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxRequestsPerChild\s*?)\s\d+/\1\ '"$(( 2048 + ($ramBase*256) ))"'/;' /etc/httpd/conf/httpd.conf

In One Step

Copy this whole block of code, paste into terminal, and hit enter to make this modification (ONCE YOU KNOW THAT STEP 3 IS SATISFIED!!!).

ramCount=$(free -m | perl -pe 's/^Mem:\s*(\d+)\s*.*/$1/ || s/.*//' | grep . | xargs -i{} perl -e 'print eval"({}*1024\)/8"');
ramBase=-16 && for ((;ramCount>1;ramBase++)); do ramCount=$((ramCount/2)); done;
perl -0 -p -i -e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?StartServers\s*?)\s\d+/\1\ '"$ramBase"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MinSpareServers\s*?)\s\d+/\1\ '"$ramBase"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxSpareServers\s*?)\s\d+/\1\ '"$(($ramBase*2 + 1))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?ServerLimit\s*?)\s\d+/\1\ '"$(( 50 + (($ramBase**2)*10) + (($ramBase-2)*10) ))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxClients\s*?)\s\d+/\1\ '"$(( 50 + (($ramBase**2)*10) + (($ramBase-2)*10) ))"'/;' \
-e 's/(\<IfModule\sprefork\.c\>(\n|[^\n])*?MaxRequestsPerChild\s*?)\s\d+/\1\ '"$(( 2048 + ($ramBase*256) ))"'/;' /etc/httpd/conf/httpd.conf;
service httpd reload;
Topic revision: r1 - 2016-07-07 - ZachBornheimer
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback