27
Speeding up Apache
When I get time, I go through the logs and check out how the servers are doing.
One thing that I haven’t really done recently is to optimize the way things are configured.
The typical solution in most scenario’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.
Below are some tips for optimizing apache a little.
I’m going to assume you’re running apache2. If not, why not?
First thing to do is to enable 2 modules. Mod_Deflate and Mod_Expires
a2enmod deflate
a2enmod expires
Mod_Deflate is a nice Apache module that allows us to gzip files when we send them to the browser. This helps load times quite nicely – as network traffic is reduced.
In our systems Apache is over in /etc/apache2/
The module configuration is done in mods-enabled
First up, we’ll configure Mod_Deflate.
pico /etc/apache2/mods-enabled/deflate.conf
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
AddOutputFilterByType DEFLATE image/svg+xml
DeflateCompressionLevel 9
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch bMSIEs(7|8) !no-gzip !gzip-only-text/html
SetEnvIf User-Agent “.*MSIE.*” nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
</IfModule>
I’ll go over what we’ve done below;
If you examine the file above, you’ll see that the first thing we do, is to check if mod_deflate is loaded with the IfModule directive.
We then setup compression for various file types –
AddOutputFilterByType DEFLATE ….
While we can compress everything, and just exclude certain files, its usually safer to specify whats compressed.
We then set compression to maximum – computers these days have ample excess CPU, so its not really a concern anymore.
DeflateCompressionLevel 9
The next lines are to specify exceptions to the compression rules. Ancient versions of Netscape don’t work so well with compression, so we disable it. Internet Explorer also has a few issues also, so we setup an appropriate rule.
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch bMSIEs(7|8) !no-gzip !gzip-only-text/html
The last line is to force compatibility with older IE versions. IE is a bit buggy, as we’ve noted.
BrowserMatch bMSIEs(7|8) !no-gzip !gzip-only-text/html
We’ll now check to see that we haven’t made any mistakes, and Apache2 runs ok.
apache2ctl configtest
If you see any errors, fix and retest.
Restart apache2
apache2ctl restart
Load up FireFox. You’ll need to have some plugins installed to test.
Go get FireBug, and YSlow, install and come back.
Done?
Ok, now open a page from the site, and click YSlow.
You should see now that the css, js, and php files are compressed with GZip.
Our next step is to setup Mod_Expires
pico /etc/apache2/mods-enabled/expires.conf
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif A21600
ExpiresByType image/jpeg A21600
ExpiresByType text/css A21600
ExpiresByType application/x-javascript A21600
</IfModule>
Essentially, what we’re doing above is turning on Content Expiry. Content Expiry is the time that a browser will cache a file for before it reloads it. For your average site things like images, javascript and css don’t change that much, so we can tell the browser to explicitly cache them.
The number next to the Expiry is the time that the file is valid for in seconds.
(after that point, the browser should reload the file from the server).
In our setup above, we’ve set that images and css and javascript files should be kept for 6 hours.
A21600 means – after 6hrs (60seconds x 60minutes x 6 = 21600)
I’d suggest using a low, but similar time span to avoid caching issues. This would mostly affect website developers, rather than end users though, as they’re more likely to upload and change files frequently during testing.
Restart apache, and check again in YSlow / Firebug to see that the images and CSS are being cached. If they are, congratulations, you’re done.
You should see a nice improvement on site load times, and things should feel snappier to end users.
It will also reduce your server load, which is a nice benefit!
Archives
- November 2024
- November 2019
- October 2019
- August 2019
- April 2019
- February 2017
- September 2016
- June 2016
- May 2016
- September 2015
- August 2015
- June 2015
- April 2015
- December 2014
- October 2014
- September 2014
- July 2014
- June 2014
- April 2014
- October 2013
- July 2013
- May 2013
- April 2013
- March 2013
- January 2013
- December 2012
- October 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- December 2011
- November 2011
- October 2011
- September 2011
- July 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
Categories
- Apple
- Arcade Machines
- Badges
- BMW
- China Related
- Cool Hunting
- Exploits
- Firmware
- Food
- General Talk
- government
- IP Cam
- iPhone
- Lasers
- legislation
- MODx
- MySQL
- notice
- qmail
- requirements
- Reviews
- Service Issues
- Tao Bao
- Technical Mumbo Jumbo
- Things that will get me censored
- Travel
- Uncategorized
- Useful Info