#!/usr/bin/perl # Aptweak v1.1 # Edit httpd.conf for best performance and enable # accf_http chomp($host = `hostname`); # Make sure we're modern FreeBSD ($os, $ver) = ($1, $2) if (`uname -a` =~ /^(.+?)\s.+?\s+(\d).+/); die("$host not FreeBSD\n") if ($os != /^FreeBSD$/); die("$host OS ver < 5\n") if ($ver < 5); # Make sure apache is running as /usr/local/sbin/httpd die("$host no httpd running\n") unless (`ps ax` =~ /\/usr\/local\/sbin\/httpd/); # Make sure it's apache 1.3 die("$host not apache 1.3\n") unless (`/usr/local/sbin/httpd -v` =~ /Apache\/1.3/); # Edit the config file. Pass 1 - just change some lines & analyze open(IN, "/usr/local/etc/apache/httpd.conf") or die("$host $!"); open(OUT, ">/tmp/aptweak.$$") or die("$host $!"); print OUT ("# Edited by aptweak v1.1 - MadCamel\n"); while() { chomp; # Timeout 30 s/^\s*Timeout\s+\d+/Timeout 30/i; # KeepAlive On s/^\s*KeepAlive\s+.+/KeepAlive On/i; # KeepAliveTimeout 2 s/^\s*KeepAliveTimeout\s+\d+/KeepAliveTimeout 2/i; # StartServers 10 s/^\s*StartServers\s+\d+/StartServers 10/i; # MaxRequestsPerChild 1024 (Helps with leaking) s/^\s*MaxRequestsPerChild\s+\d+/MaxRequestsPerChild 1024/i; # Special handling for ServerTokens and AcceptFilter # as we have to add them if they are not already there. s/^\s*ServerTokens\s+.+/ServerTokens Prod/i; s/^\s*AcceptFilter\s+.+/AcceptFilter On/i; s/^\s*SendBufferSize\s+.+/SendBufferSize 229376/i; $has_accf++ if (/^\s*AcceptFilter\s+On/); $has_toks++ if (/^\s*ServerTokens\s+Prod/); $has_sbuf++ if (/^\s*SendBufferSize\s+229376/); print OUT ("$_\n"); } close(IN); close(OUT); # Edit configfile pass 2 - add missing lines # Hook this around Timeout as it's easiest open(IN, "/tmp/aptweak.$$") or die("$host $!"); open(OUT, ">/tmp/aptweak2.$$") or die("$host $!"); while() { chomp; if (/^Timeout\s+\d+/) { print OUT ("AcceptFilter On\n") unless ($has_accf); print OUT ("ServerTokens Prod\n") unless ($has_toks); print OUT ("SendBufferSize 229376\n") unless ($has_sbuf); } print OUT ("$_\n"); } close(IN); close(OUT); # Backup & replace config, clean up system( "cp /usr/local/etc/apache/httpd.conf /usr/local/etc/apache/httpd.pretweak;". "mv /tmp/aptweak2.$$ /usr/local/etc/apache/httpd.conf;" . "rm /tmp/aptweak.$$"); # Load accf_http system("/sbin/kldload accf_http"); # Make sure it loaded correctly $accf_loaded++ if (`/sbin/kldstat` =~ /accf_http/); # If it did, enable it in loader.conf if ($accf_loaded) { # Remove any mention of accf_http_enable.. # and add our line.. open(I, "/boot/loader.conf") or die("$host $!"); open(O, ">/tmp/loader.$$") or die("$host $!"); while() { s/accf_http_.*//g; print O; } print O ("accf_http_load=\"YES\"\n"); } print O ("\n"); close(I); close(O); # Backup & Replace loader.conf system( "cp /boot/loader.conf /boot/loader.bak;". "mv /tmp/loader.$$ /boot/loader.conf"); # Restart apache system("/usr/local/sbin/apachectl restart"); # Remove any crap in rc.local leftover from previous tweaks open(I, "/etc/rc.local") or die("$host rc.local $! (Non-Fatal)"); open(O, ">/tmp/local.$$") or die("$host $!"); while() { next if (/apachectl restart/); next if (/kldload/); print O ("$_"); } close(I); close(O); system("mv /tmp/local.$$ /etc/rc.local");