Over the past week or so as part of the project I am working on I wanted to speed up a WordPress site that is being used as a CMS. During this process I discovered a two methods (well three technically) that can be used to dramatically shorten the load time for content and speed up WordPress. These tweaks will only work with Apache though they may work on a Windows version of Apache it has not been tested. One method only requires editing your template files while the other requires that you are able to edit your .htaccess
file. The first two methods work by compressing data before sending it and the third uses plugins that do a variety of things to speed up WordPress.
[ad#Google 468×60-2010.03.12]
You should back up the files you are going to modify before making these changes. It is also advised to backup your WordPress database if you are using method three.
Method One: Speed up WordPress by adding compression via .htaccess
This is my preferred and recommended method as it is a more comprehensive solutions and works either domain or server wide depending on your configuration. This is also a more advance and difficult to implement method so if you do not understand what I am talking about you should probably skip this method. The easiest place to add these directive are in the .htaccess fill though you can also add the directives to your vhosts.conf of your apache configuration. Method one does not apply to Apache 1.x please do not try it unless you are using Apache2.
First you will need to enable both mod_deflate and mod_headers with Apache. This can be done in one of the following two methods. Apache 2 uses mod_deflate which has some built in detection to determine whether and how to compress data for browsers.
You can enter the following commands on some systems including Debian and Ubuntu.
sudo a2enmod deflate
sudo a2enmod headers
sudo /etc/init.d/apache2 force-reload
This will enable mod_deflate and mod_headers in the apache config and the the last command will reload the configuration.
You can also manually enable the mods by looking in your apache configuration files and locating where mod are configured (hint: look for lines similar to those below). You can either ad the following two lines or uncomment them if they already exist.
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
Again you will need to reload the apache configuration as above.
There are two option that for compressing outgoing content with apache2, compress specific content or compress all content except specified. Choose one option only.
Option 1: Compress specific content
You can add type such as text/html, text/css, text/plain or even x-application/javascript. See Webmaster Toolkit Mime Types for a simple but exhaustive list.
#-- compress content with type html, text, css and javascript
AddOutputFilterByType DEFLATE text/html text/plain text/css x-application/javascript
# correctly handle http requests that come from behind a proxy
Header append Vary User-Agent
Option 2: Compress all content
Manually excluding specified file types such as images, zip files, pdfs, or other files that already have some sort of compression on them. In this case you exclude file by their extension using regular expressions.
# set filter mod_deflate (chooses gzip or deflate compression based on browser support) on all outgoing content
SetOutputFilter DEFLATE
# exclude content that is already compressed or does not compress well via file type
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|rar|zip)$ no-gzip
# correctly handle http requests that come from behind a proxy
Header append Vary User-Agent
Additional Apache2 Directives
This directive should probably be added to properly handle old browsers that do not support compression.
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Additionally you can add the following directives for additional logging
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
CustomLog logs/deflate.log deflate
Note: that the client browser has to request compression via headers sent on request and IE 6/7 will only request compression on .html, but not on .txt nor .css. Mozilla/Firefox/Chrome/Safari can handle all three types. I am not sure about Opera or IE 8.
You should now test your site using various browsers and you can also check to make sure your are sending compressed content using the GIDZipTest page.
Method Two: Speed up WordPress by adding compression via PHP
If you don’t have the ability to modify you apache configuration files or .htaccess you can still try to get some compression by adding a small snippet of PHP code to your template. This may work with Apache 1.x bu tI have not tested it.
This method is very simple to implement. Open your header.php
in the theme you are using and add the following line to top of the file or just above the <DOCTYPE!
line.
That’s all.
Again you should test with various browser to make sure your site is working and you can use the GIDZipTest page to see if you page is being compressed.
Method Three: Speed up WordPress using a plugin
There are many plugins for WordPress that will combine, minify and compress your WordPress output. I haven’t had much luck with any of them due to some quirks with this site but they may be the simplest way to save bandwidth and speed up WordPress. I am not going to go into detail on how to use these plugins but I will merely list the most promising – those which other WordPress developers I know have used successfully to achieve that same results.
PHP Speedy – PHP Speedy can consolidate javascript and CSS and combining them all into two files, one for the javascript code and the other for CSS. It also minifies and compresses the combined files and caches them then removes all the javascript and CSS links in the <head> tag and serves the combined file from the cache to the readers.
W3 Total Cache – Increases you site’s performance by using a variety of techniques including file caching, database query caching, minifying/combining/compressing files, content delivery network (CDN) integration and more.
Webo Site SpeedUp – This is a commercial plugin but it also have a free version that does quite a bit to speed up your site. It is without a doubt the most configurable and the free version allows you to do file caching, minifying/combining/compressing files and more. The commercial version gives you total control over optimizations ranging from the .htaccess tweaks listed here to various file and database caching methods.
Leave a Reply