#!/bin/bash ###Adding Apache Optimizations from command line### # #Test to see if CentOS if [ -e /etc/redhat-release ]; then #Test to see if cPanel if [ -e /var/cpanel/conf/apache ]; then #View current opts echo "Current Apache Optimizations:" egrep '(^StartServers|^MinSpare|^MaxSpare|^MaxCli|^ServerLim|^MaxReqests|^KeepAlive|^Timeout)' /usr/local/apache/conf/httpd.conf || echo "No Optimizations Currently Set" echo #Ask user to enter desired optimizations echo "Let's update the Apache Optimizations" echo echo -n StartServers: read var_ss echo $var_ss echo -n MinSpareServers: read var_minss echo $var_minss echo -n MaxSpareServers: read var_maxss echo $var_maxss echo -n MaxClients: read var_mc echo $var_mc echo -n ServerLimit: read var_sl echo $var_sl echo -n MaxRequestsPerChild: read var_mrpc echo $var_mrpc echo -n KeepAlive On/Off: read var_ka echo $var_ka echo -n KeepAliveTimeout: read var_kat echo $var_kat echo -n MaxKeepAliveRequests: read var_mkar echo $var_mkar echo -n Timeout: read var_t echo $var_t #Test to see if /var/cpanel/conf/apache/local file exists if [ -e /var/cpanel/conf/apache/local ]; then #set httploc to be /var/cpanel/conf/apache/local httploc=/var/cpanel/conf/apache/local #If it does, copy existing file to .lwbak to be safe cp /var/cpanel/conf/apache/local{,.bak} #sed in the entered variables sed -i 's/\"timeout\"\: [0-9].*/\"timeout\"\: '$var_t'/' $httploc sed -i 's/\"startservers\"\: [0-9].*/\"startservers\"\: '$var_ss'/' $httploc sed -i 's/\"maxclients\"\: [0-9].*/\"maxclients\"\: '$var_mc'/' $httploc sed -i 's/\"minspareservers\"\: [0-9].*/\"minspareservers\"\: '$var_minss'/' $httploc sed -i 's/\"maxspareservers\"\: [0-9].*/\"maxspareservers\"\: '$var_maxss'/' $httploc sed -i 's/\"serverlimit\"\: [0-9].*/\"serverlimit\"\: '$var_sl'/' $httploc sed -i 's/\"maxrequestsperchild\"\: [0-9].*/\"maxrequestsperchild\"\: '$var_mrpc'/' $httploc sed -i 's/\"keepalive\"\:\ .[A-Za-z].*/\"keepalive\"\:\ '\'$var_ka\''/g' $httploc sed -i 's/\"keepalivetimeout\"\: [0-9].*/\"keepalivetimeout\"\: '$var_kat'/' $httploc sed -i 's/\"maxkeepaliverequests\"\: [0-9].*/\"maxkeepaliverequests\"\: '$var_mkar'/' $httploc #rebuild httpd.conf and restart apache /scripts/rebuildhttpdconf /etc/init.d/httpd restart #If /var/cpanel/conf/apache/local does not exist, create it else touch /var/cpanel/conf/apache/local #set httploc to be /var/cpanel/conf/apache/local httploc=/var/cpanel/conf/apache/local #Add default 1gb opts to /var/cpanel/conf/apache/local cat > /var/cpanel/conf/apache/local < $httploc < StartServers 5 ServerLimit 50 MinSpareServers 5 MaxSpareServers 10 MaxClients 50 MaxRequestsPerChild 400 ServerLimit 2 StartServers 1 MinSpareThreads 25 MaxSpareThreads 50 ThreadsPerChild 25 MaxClients 50 MaxRequestsPerChild 400 Timeout 50 EOF #Substitute in Opts entered earlier. perl -i -p -e 's/^((\s+)?)[Tt]imeout\s+[0-9].*/Timeout\ '$var_t'/g' $httploc perl -i -p -e 's/^((\s+)?)[Ss]tart[Ss]ervers\s+[0-9].*/StartServers\ '$var_ss'/g' $httploc perl -i -p -e 's/^((\s+)?)[Mm]ax[Cc]lients\s+[0-9].*/MaxClients\ '$var_mc'/g' $httploc perl -i -p -e 's/^((\s+)?)[Mm]in[Ss]pare[Ss]ervers\s+[0-9].*/MinSpareServers\ '$var_minss'/g' $httploc perl -i -p -e 's/^((\s+)?)[Mm]ax[Ss]pare[Ss]ervers\s+[0-9].*/MaxSpareServers\ '$var_maxss'/g' $httploc perl -i -p -e 's/^((\s+)?)[Ss]erver[Ll]imit\s+[0-9].*/ServerLimit\ '$var_sl'/g' $httploc perl -i -p -e 's/^((\s+)?)[Mm]ax[Rr]equests[Pp]er[Cc]hild\s+[0-9].*/MaxRequestsPerChild\ '$var_mrpc'/g' $httploc perl -i -p -e 's/^((\s+)?)[Kk]eep[Aa]live\s+[A-Za-z].*/KeepAlive\ '$var_ka'/g' $httploc perl -i -p -e 's/^((s\+)?)[Kk]eep[Aa]live[Tt]imeout\s+[0-9].*/KeepAliveTimeout\ '$var_kat'/g' $httploc perl -i -p -e 's/^((s\+)?)[Mm]ax[Kk]eep[Aa]live[Rr]equests\s+[0-9].*/MaxKeepAliveRequests\ '$var_mkar'/g' $httploc fi #restart apache2 service apache2 restart #If not CentOS or Debian based, fail else echo "At this time, this script only works on RedHat and Debian based servers" fi