{"id":143,"date":"2009-03-27T14:14:54","date_gmt":"2009-03-27T06:14:54","guid":{"rendered":"http:\/\/www.computersolutions.cn\/blog\/?p=143"},"modified":"2009-03-27T14:15:20","modified_gmt":"2009-03-27T06:15:20","slug":"speeding-up-apache","status":"publish","type":"post","link":"https:\/\/www.computersolutions.cn\/blog\/2009\/03\/speeding-up-apache\/","title":{"rendered":"Speeding up Apache"},"content":{"rendered":"<p>When I get time, I go through the logs and check out how the servers are doing.<\/p>\n<p>One thing that I haven&#8217;t really done recently is to optimize the way things are configured.<br \/>\nThe typical solution in most scenario&#8217;s is to throw faster hardware at things (something we do when necessary!), but sometimes a few minutes configuration can help speed things up tremendously.<\/p>\n<p>Below are some tips for optimizing apache a little.<\/p>\n<p><!--more--><\/p>\n<p>I&#8217;m going to assume you&#8217;re running apache2.\u00a0 If not, why not?<\/p>\n<p>First thing to do is to enable 2 modules.\u00a0 Mod_Deflate and Mod_Expires<\/p>\n<p>a2enmod deflate<\/p>\n<p>a2enmod expires<\/p>\n<p>Mod_Deflate is a nice Apache module that allows us to gzip files\u00a0 when we send them to the browser.\u00a0 This helps load times quite nicely &#8211; as network traffic is reduced.<\/p>\n<p>In our systems Apache is over in \/etc\/apache2\/<br \/>\nThe module configuration is done in mods-enabled<\/p>\n<p>First up, we&#8217;ll configure Mod_Deflate.<\/p>\n<p>pico \/etc\/apache2\/mods-enabled\/deflate.conf<\/p>\n<p style=\"padding-left: 30px;\">&lt;IfModule mod_deflate.c&gt;<br \/>\nAddOutputFilterByType DEFLATE text\/plain<br \/>\nAddOutputFilterByType DEFLATE text\/html<br \/>\nAddOutputFilterByType DEFLATE text\/xml<br \/>\nAddOutputFilterByType DEFLATE text\/css<br \/>\nAddOutputFilterByType DEFLATE text\/javascript<br \/>\nAddOutputFilterByType DEFLATE application\/xml<br \/>\nAddOutputFilterByType DEFLATE application\/xhtml+xml<br \/>\nAddOutputFilterByType DEFLATE application\/rss+xml<br \/>\nAddOutputFilterByType DEFLATE application\/javascript<br \/>\nAddOutputFilterByType DEFLATE application\/x-javascript<br \/>\nAddOutputFilterByType DEFLATE application\/x-httpd-php<br \/>\nAddOutputFilterByType DEFLATE application\/x-httpd-fastphp<br \/>\nAddOutputFilterByType DEFLATE application\/x-httpd-eruby<br \/>\nAddOutputFilterByType DEFLATE image\/svg+xml<\/p>\n<p>DeflateCompressionLevel 9<\/p>\n<p>BrowserMatch ^Mozilla\/4 gzip-only-text\/html<br \/>\nBrowserMatch ^Mozilla\/4\\.0[678] no-gzip<br \/>\nBrowserMatch bMSIEs(7|8) !no-gzip !gzip-only-text\/html<\/p>\n<p>SetEnvIf User-Agent &#8220;.*MSIE.*&#8221; nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0<\/p>\n<p>&lt;\/IfModule&gt;<\/p>\n<p>I&#8217;ll go over what we&#8217;ve done below;<\/p>\n<p>If you examine the file above, you&#8217;ll see that the first thing we do, is to check if mod_deflate is loaded with the IfModule directive.<\/p>\n<p>We then setup compression for various file types &#8211;<\/p>\n<p><strong>AddOutputFilterByType DEFLATE\u00a0 &#8230;.<\/strong><\/p>\n<p>While we can compress everything, and just exclude certain files, its usually safer to specify whats compressed.<\/p>\n<p>We then set compression to maximum &#8211; computers these days have ample excess CPU, so its not really a concern anymore.<\/p>\n<p><strong>DeflateCompressionLevel 9<\/strong><\/p>\n<p>The next lines are to specify exceptions to the compression rules.\u00a0 Ancient versions of Netscape don&#8217;t work so well with compression, so we disable it.\u00a0 Internet Explorer also has a few issues also, so we setup an appropriate rule.<\/p>\n<p><strong>BrowserMatch ^Mozilla\/4 gzip-only-text\/html<br \/>\nBrowserMatch ^Mozilla\/4\\.0[678] no-gzip<br \/>\nBrowserMatch bMSIEs(7|8) !no-gzip !gzip-only-text\/html<\/strong><\/p>\n<p>The last line is to force compatibility with older IE versions.\u00a0 IE is a bit buggy, as we&#8217;ve noted.<\/p>\n<p><strong>BrowserMatch bMSIEs(7|8) !no-gzip !gzip-only-text\/html<\/strong><\/p>\n<p>We&#8217;ll now check to see that we haven&#8217;t made any mistakes, and Apache2 runs ok.<\/p>\n<p>apache2ctl configtest<\/p>\n<p>If you see any errors, fix and retest.<\/p>\n<p>Restart apache2<\/p>\n<p>apache2ctl restart<\/p>\n<p>Load up FireFox.\u00a0 You&#8217;ll need to have some plugins installed to test.<br \/>\nGo get FireBug, and YSlow, install and come back.<\/p>\n<p>Done?<\/p>\n<p>Ok, now open a page from the site, and click YSlow.<\/p>\n<p>You should see now that the css, js, and php files are compressed with GZip.<\/p>\n<p>Our next step is to setup Mod_Expires<\/p>\n<p>pico \/etc\/apache2\/mods-enabled\/expires.conf<\/p>\n<p style=\"padding-left: 30px;\">&lt;IfModule mod_expires.c&gt;<br \/>\nExpiresActive On<br \/>\nExpiresByType image\/gif A21600<br \/>\nExpiresByType image\/jpeg A21600<br \/>\nExpiresByType text\/css A21600<br \/>\nExpiresByType application\/x-javascript A21600<br \/>\n&lt;\/IfModule&gt;<\/p>\n<p>Essentially, what we&#8217;re doing above is turning on Content Expiry.\u00a0 Content Expiry is the time that a browser will cache a file for before it reloads it.\u00a0 For your average site things like images, javascript and css don&#8217;t change that much, so we can tell the browser to explicitly cache them.<\/p>\n<p>The number next to the Expiry is the time that the file is valid for in seconds.<br \/>\n(after that point, the browser should reload the file from the server).<\/p>\n<p>In our setup above, we&#8217;ve set that images and css and javascript files should be kept for 6 hours.<br \/>\nA21600 means &#8211; after 6hrs (60seconds x 60minutes x 6 = 21600)<\/p>\n<p>I&#8217;d suggest using a low, but similar time span to avoid caching issues.\u00a0 This would mostly affect website developers, rather than end users though, as they&#8217;re more likely to upload and change files frequently during testing.<\/p>\n<p>Restart apache, and check again in YSlow \/ Firebug to see that the images and CSS are being cached.\u00a0 If they are, congratulations, you&#8217;re done.<\/p>\n<p>You should see a nice improvement on site load times, and things should feel snappier to end users.<br \/>\nIt will also reduce your server load, which is a nice benefit!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I get time, I go through the logs and check out how the servers are doing. One thing that I haven&#8217;t really done recently is to optimize the way things are configured. The typical solution in most scenario&#8217;s is to throw faster hardware at things (something we do when necessary!), but sometimes a few [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[25],"tags":[41,42,43,44],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-technical-mumbo-jumbo","tag-apache","tag-mod_deflate","tag-mod_expires","tag-tuning"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":3,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/posts\/143\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}