Tuesday, January 31, 2012
Having a blog that's snappy & responsive is just one of the many factors that makes a great blog. Even with the best content in the world, a slow blog will frustrate readers & potentially force them elsewhere. In this post we'll focus on optimising Wordpress, a popular free open source blogging platform that runs on PHP.
In order to figure out if any of the changes we're about to make have an effect it's important to benchmark current performance. There's a number of tools & a small piece of code that allows us to do this.
Pingdom does a quick visual benchmark of all the elements on your Blog. You can instantly get a snapshot of elements that may be taking longer to load than they should & other problem areas. Here's the benchmark for the StoreCrowd Blog (Around 1.4s load time & 596kb).

YSlow is a firefox plugin that plugs into Firebug (possibly the best web development tool in the history of man). It analyses 22 factors that can slow down the performance of your blog & scores you out of 100. The StoreCrowd articles section gets 86/100 & a grade B.

Some of the things we've been alerted to clean up:
It's possible using this small piece of PHP code in your website footer to show the number of Queries & Load time publically:
<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.
A quick test on the Homepage & I get: 17 queries in 1.058 seconds.
Believe it or not your Web Hosting is critical to the speed at which Wordpress performs.
Without going into too much detail on which host you should pick here's a very simple but quick overview:
$97 Off Yearly or Bi-Yearly Plans
103 days agoYou can get an idea of the load on your server by logging in via ssh & using the command: top
My rule of thumb is that anything over 1.00 is worrying & could impact performance.
This isn't to say that you can't get Wordpress running snappily on a Shared Web Host. But just remember that you'll always get better performance from higher end web hosts. For the record this article is hosted on a Dedicated Server at Secured Servers.
Location of your server is also important, think about your target market. If it's the whole USA you'll want a server that's located in the middle of the country, if it's New York then a server on the East Coast. You get the idea.
Making sure your server is tuned to handle the load & queries you're throwing at it depends largely on your hardware (RAM & CPU). Wordpress is built with PHP & it runs on an Apache http server normally. There have been some good experiments using other http servers such as Nginx or Lighttpd, I'll leave this for another post though.
Few quick things to do first:
The more RAM you have available the higher you'll be able to tweak MYSQL & Apache for performance.
Since Wordpress communicates quite heavily with a Database you'll need to ensure that your my.cnf configuration suits your Hardware setup. There's a few quick tweaks to speed things up.
The Query Cache does exactly what it says, its Caches queries. This means that the first time the query is run it'll get cached so the second time it runs there'll be a significant speed improvement.
Add these lines to your my.cnf:
query_cache_type = 1 query_cache_limit = 2M query_cache_size = 20M
Remember you'll need to restart MYSQL for these changes to take effect.
The Compiler cache increases the performance of compiled scripts on your server by caching them, this can have a dramatic effects on the execution time of PHP scripts.
I personally recommend playing around with both here, however I've found a slight performance increase from Xcache over Eaccelerator on Wordpress. About 5%.
Increasing the max connections in your httpd.conf can increase performance & as the server is handlging more connections simultaneously. However you may end up running out of RAM, so test a few configurations first.
150 connections is a good start:
max_connections = 150
Then increase in 50 connection intervals until you're satisfied.
Remember to restart apache every time you make a change.
Once your server is humming along you can now start playing with the Core Wordpress code & also the coding of your Theme.
Whenever you serve images on your server you're essentially using resources & queries. Quite often people will borrow you images & hotlink them on their own server. This not only uses up your bandwidth, but it also puts necessary strain on your server.
You'll need to add the following code to your .htaccess file & replace example.com with your domain:
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/.*$ [NC]
RewriteRule .*\.(gif|jpg|png|ico)$ - [F,L]
</ifmodule>
Hosting images externally can reduce resources & server load considerably. In the example below you can see the decrease in memory usage a Wordpress Blogger saw when moving static images off his server:

Read more about the steps taken to move images to Amazon S3.
Compressing Javascript is quite simple, since this is loaded on every pageview you can reduce the size of Javascript by removing all of the White Space. There's a simple tool to do this for you called JavaScript Compressor.
Often your site can run incredibly slowly or stall because another site that you call Javascript from is down or offline (i.e. Digg badges, Twitter etc). Move anything that you can to the end of your page to avoid this, you can also include the Javascript that can't be moved to the end of the page in an iFrame.
The Browser Cache itself won't actually make your blog load faster however it will help reduce strain on the server by caching objects that are getting loaded often (like images & styles)
Test the following code in your .htaccess file:
FileETag MTime Size <ifmodule mod_expires.c><filesmatch> ExpiresActive on ExpiresDefault "access plus 1 year </filesmatch></ifmodule>
You can reduce the size of your pages by letting the browser compress & uncompress the data. This will reduce the bandwidth & amount needed to download.
Test the following in your .htaccess file:
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4.0[678] no-gzip BrowserMatch bMSIE !no-gzip !gzip-only-text/html
Serving all your images from the same domain can means that your browser is waiting to download all the items one after the other. Lets say you have 12 items, if you split these out across 3 subdomains then they can be downloaded concurrently (as there's 3 sources), instead of the browser waiting to download them from one source.
A good way to experiment here is to have your css & javascript files on files.yoursite.com & your templates/images on static.yourserver.com
A CDN or Content Delivery Network works in much the same way. It allows you to host your static files on a subdomain which is linked to a wide network of servers all over the world. This means that your static content is not only downloaded in parallel but it's more than likely pushed to a server that's closer to the intended recipient making it load MUCH faster.
The following includes tweaks that you can make to Wordpress itself to improve performance.
Keeping up to date with the latest Version is paramount, not only does it fix security vulnerabilities but they are constant trying to improve performance. For example Wordpress 3.5 has significant database performance improvements.
In Wordpress 2.6 & later revisions of your posts are kept every time they are autosaved. This can clog up the database & increase its size unnecessary.
To disable post revisions add this line to wp-config.php:
define('WP_POST_REVISIONS', false);
You can also run the following query in PHPmyadmin to delete all current revisions:
DELETE a,b,c FROM wp_posts a LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision'
A query happens anytime in your theme when you use PHP. So for example this is a query:
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>">
Which we can change to:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
All of a sudden we've removed two queries from the template. Pretty simple?
There's a number of plugins that can improve performance. Once everything above has been completed this is where you'll notice most of the performance gain.
Possibly one of the best plugins you'll ever install. WP Super Cache creates a static HTML version of each page & loads that instead of querying the database every time. This increases the speed at which pages load (upwards of 45%) & reduces strain on the server.
This plugin takes care of one of the other issues we talked about earlier in the post & that's removing the white spaces in CSS files & Javascript. This plugin however is not compatible with WP Super Cache currently.
This plugin allows you to optimize the tables of your MYSQL database & reduce their overhead without actually going into PHPmyadmin.