{"id":3274,"date":"2014-06-28T23:09:45","date_gmt":"2014-06-28T15:09:45","guid":{"rendered":"http:\/\/rmohan.com\/?p=3274"},"modified":"2014-06-28T23:09:45","modified_gmt":"2014-06-28T15:09:45","slug":"apache-webserver","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3274","title":{"rendered":"Apache webserver"},"content":{"rendered":"<p>Versions<\/p>\n<p>The default with Centos\/Redhat is 2.2.x (CentOS like the Red Hat OS it derives from, are geared towards stability and so tend to lag behind what\u2019s cutting edge)<\/p>\n<p>2.4 is available and offers improved performance<br \/>\n2.4 moves away from Prefork MPM and moves to \u201cevent\u201d mpm which uses a lot less memory<\/p>\n<p>new features are listed here http:\/\/httpd.apache.org\/docs\/2.4\/new_features_2_4.html<\/p>\n<p>If you want to upgrade source compiling seems the mist popular way although here is a link to do it via yum<br \/>\nhttp:\/\/developerblog.redhat.com\/2013\/10\/24\/apache-httpd-2-4-on-red-hat-enterprise-linux-6\/<\/p>\n<p>Terminology and how it all fits together<\/p>\n<p>An apache thread is essentially another term for an apache child process and can be used interchangeably.<\/p>\n<p>Apache has a root process, and then for each client (i.e. whenever any separate user goes to your website) it creates a \u201cchild\u201d process.<\/p>\n<p>Each of these processes will use a certain amount of memory (which is dictated by your application). This memory usage is not static, so can vary between each process. It can also vary depending on what the application is serving to each client.<\/p>\n<p>Apache doesn\u2019t allow multiple versions of PHP using mod_php, so you need to compile it using fastcgi support. This will allow us to configure multiple sites, each one with a different version of PHP<\/p>\n<p>If you are using Name Based Virtual Hosting for SSL, if a domain is unconfigured for the ip:port it will be sent to a default virtual host.<br \/>\nApache defaults the first virtual host it sees for an ip:port combination to be the default. This can be confirmed with the command httpd -S<\/p>\n<p>Commands<\/p>\n<p>httpd \u2013S # show the virtuahost settings (taken from the config file)<br \/>\nhttpd \u2013M # list of loaded Static and Shared Modules.<br \/>\nhttpd \u2013l # Output a list of modules compiled into the server. This will not<br \/>\nlist dynamically loaded modules<br \/>\nhttpd \u2013X # run in debug mode<br \/>\nhttpd -k start -c \u201cDocumentRoot \/var\/www\/html_debug\/\u201d Start up Apache using an alternative DocumentRoot.<br \/>\nThis is useful if you\u2019re trying out alternative versions of your web site, as it avoids editing the DocumentRoot option.<br \/>\nYou can also use -c to set any other directive. Note that -c processes the directive after reading the config files (so it will overwrite config file settings),<br \/>\nwhereas -C will process the directive before the config files.<br \/>\nhttpd -k start -e debug# While you are debugging an issue, you can change the LogLevel of the Apache temporarily, without modifying the LogLevel directive in the httpd.conf as shown below using option -e.<br \/>\nthe LogLevel is set to debug.Possible values you can pass to option -e are: debug, info, notice, warn, error, crit, alert, emerg<\/p>\n<p>Config<\/p>\n<p>The main apache config is \/etc\/httpd\/conf\/httpd.conf<br \/>\nA lot lot of the parameters in that file can be tweaked to improve performance.<br \/>\nSome of the more useful settings (with typically assigned values):<\/p>\n<p>StartServers 8<\/p>\n<p>MinSpareServers 5<\/p>\n<p>MaxSpareServers 20<\/p>\n<p>ServerLimit 256<\/p>\n<p>MaxClients 50<\/p>\n<p>The StartServers, MinSpareServers, MaxSpareServers, and MaxClients regulate how the parent process creates children to serve requests.<br \/>\nIn general, Apache is very self-regulating, so most sites do not need to adjust these directives from their default values.<\/p>\n<p>MaxSpareServers and MinSpareServers determine how many child processes to keep while waiting for requests. If the MinSpareServers is too low and a bunch of requests come in, then Apache will have to spawn additional child processes to serve the requests. Creating child processes is relatively expensive. If the server is busy creating child processes, it won\u2019t be able to serve the client requests immediately. MaxSpareServers shouldn\u2019t be set too high, it can cause resource problems since the child processes consume resources.<br \/>\nTune MinSpareServers and MaxSpareServers such that Apache need not frequently spwan more than 4 child processes per second (Apache can spwan a maximum of 32 child processes per second). When more than 4 children are spawned per second, a message will be logged in the ErrorLog.<\/p>\n<p>The StartServers directive sets the number of child server processes created on startup. Apache will continue creating child process until the MinSpareServers setting is reached. Doesn\u2019t have much effect on performance if the server isn\u2019t restarted frequently. If there are lot of requests and Apache is restarted frequently, set this to a relatively high value.<\/p>\n<p>http:\/\/rudd-o.com\/linux-and-free-software\/tuning-an-apache-server-in-5-minutes has some good hints<\/p>\n<p>To know which fork (prefork MPM or worker MPM to alter) normally its prefork mpm<\/p>\n<p>run httpd -l and you can see what appache was compiled with<br \/>\nCompiled in modules:<br \/>\ncore.c<br \/>\nprefork.c<br \/>\nhttp_core.c<br \/>\nmod_so.c<\/p>\n<p>a good page on performance tuning http:\/\/fuseinteractive.ca\/blog\/drupal-performance-tuning-0<\/p>\n<p>appache full status is a great help in troubleshooting, the following pages cover basic setup and troubleshooting when it doesn\u2019t work if you have multiple vhosts it can get a bit messy \u2013<\/p>\n<p>http:\/\/articles.slicehost.com\/2010\/3\/26\/enabling-and-using-apache-s-mod_status-on-centos<\/p>\n<p>http:\/\/www.mydigitallife.info\/request-url-server-status-or-404-page-not-found-apache-error\/<\/p>\n<p>RedirectMatch 301 ^\/bbc.co.uk http:\/\/www.bbc.co.uk # the following can be added to .htaccess or httpd.conf<\/p>\n<p>Useful Modules<\/p>\n<p>Modules allow you to add features to the default service, here are some of the more useful:<\/p>\n<p>https:\/\/httpd.apache.org\/docs\/trunk\/mod\/mod_log_forensic.html example of how to use at http:\/\/northernmost.org\/blog\/mod_log_forensic-howto\/<\/p>\n<p>mod_limitipconn.c can be used to limit the amount of download from an IP<\/p>\n<p>mod_log_forensic This module provides for forensic logging of client requests. Logging is done before and after processing a request, so the forensic log contains two log lines for each request.<\/p>\n<p>Useful bits of code<\/p>\n<p>ps aux | grep [h]ttpd | awk \u2018{sum+=$6} END {print \u201chttpd mem:\u201d,sum \/1024}\u2019 &#8211; shows amount of memory being taken up by apache<\/p>\n<p>apachectl fullstatus|grep -Eo \u2018[1-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\u2019|grep -v \u201c127.0.0.1?|sort -n|uniq -c|sort -nr|head -1 &#8211; shows who is connected at present by most connections<\/p>\n<p>grep -c \u201913\/Jan\/2014:14? \/etc\/httpd\/logs\/* | sort -t\u2019:\u2019 -nk 2 | grep -v :0 \u2013 good to see which logs files where populated at a certain hour<\/p>\n<p>grep -c \u201cInvalid URI in request\u201d \/etc\/httpd\/logs\/* &#8211; check all logs for text displays number of instances each was found<\/p>\n<p>awk \u2018{print $4}\u2019 access_log | cut -d: -f1 | uniq -c -shows number of requests per day<\/p>\n<p>grep \u201c13\/Jan\u201d access_log | cut -d[ -f2 | cut -d] -f1 | awk -F: \u2018{print $2?:00?}\u2019 | sort -n | uniq -c &#8211; shows number of connections per hour<\/p>\n<p>grep \u201c13\/Jan\/2014:19? access_log | cut -d[ -f2 | cut -d] -f1 | awk -F: \u2018{print $2?:\u201d$3}\u2019 | sort -nk1 -nk2 | uniq -c | awk \u2018{ if ($1 > 10) print $0}\u2019 \u2013 number of requests per minute<\/p>\n<p>cat error_log | grep \u201cJan 16? | grep \u201c01:[0-9][0-9]:\u201d|awk \u2018{print $4}\u2019 | awk -F: \u2018{print $1 \u201c:\u201d $2}\u2019 | uniq -c -show number of events at 15:xx on the 14 Jan<\/p>\n<p>cut -d\\\u201d -f 2 \/etc\/httpd\/logs\/access_log | sort | uniq -c | sort -rnk 1 | head -n 30 -shows most popular pages<\/p>\n<p>cat access.log |grep wp-login # check what wp-login pages have been called<\/p>\n<p>cat .\/access_list grep bot > bot_report.txt # output any mentions of the word bot<\/p>\n<p>grep -e \u201c01\/Jun\/2014? -e \u201c31\/May\/2014? access_log* | awk \u2018{print $1}\u2019 | cut -d\\? -f1 | sort | uniq -ic | sort -rn | head -25 check the access logs for connections via IP on the dates specified , order via number of connections<\/p>\n<p>Scripts<br \/>\nto log when appache hits a high number of users<\/p>\n<p>#!\/bin\/bash<br \/>\nlog_path=\/root\/scripts\/number_of_appache_users.log<br \/>\nif [ $number -gt 150 ]<br \/>\nthen<br \/>\ndate >> $log_path<br \/>\necho \u201cnumber of indivdual connections\u201d >> $log_path<br \/>\n\/usr\/sbin\/apachectl fullstatus | awk \u2018{print $11}\u2019 | grep -v \u201c::1? | grep -v ^$ | sort | uniq -c | sort -n | tail >> $log_path<br \/>\necho \u201cpages being view\u201d >> $log_path<br \/>\n\/usr\/sbin\/apachectl fullstatus | awk \u2018{print $11,$12,$13,$14}\u2019 | grep -v \u201c::1? | grep -v ^$ | sort | uniq -c |sort -n >> $log_path<br \/>\nfi<\/p>\n<p>Apache Bench<\/p>\n<p>To load test your system use appache bench, check the performance (memory and load) and then retest.<\/p>\n<p>Apache Bench (ab) is installed by default<\/p>\n<p>(n= number of requests c=councurent connections i.e current clients)<\/p>\n<p>example:<\/p>\n<p>ab \u2013 apache bench ab \u2013n 100 \u2013c 10 url (100 requests, and concurrency 10)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Versions<\/p>\n<p>The default with Centos\/Redhat is 2.2.x (CentOS like the Red Hat OS it derives from, are geared towards stability and so tend to lag behind what\u2019s cutting edge)<\/p>\n<p>2.4 is available and offers improved performance 2.4 moves away from Prefork MPM and moves to \u201cevent\u201d mpm which uses a lot less memory<\/p>\n<p>new features [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3274"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3274"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3274\/revisions"}],"predecessor-version":[{"id":3275,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3274\/revisions\/3275"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}