Optimizing WordPress For Shared Hosting

This post is now quite old, please feel free to read it, but I would advise that you read my new version of this post as well. You can find it here.

WordPress is actually fairly lightweight on a default install, but there are quite a few plugins essential for a good SEO friendly WordPress install. With those installed you can easily end up with a footprint of between 20-30MB per request. On a shared server, with say a memory limit of 90MB, you are pretty much up the creek without a paddle it 3/4 people enter your website. Here I am going to share my top tips for making WordPress work it’s best in a limited resource environment.

Minified Scripts

The first tip is a small one. Whenever possible use the minified versions of scripts such as jQuery. This doesn’t really help much with memory, but does give a little bit of a speed increase. Feel free to skip this one, but if you are all about squeezing out those few extra milliseconds then give it a go.

WP Super Cache

My all time favorite caching system for WordPress has to be WP Super Cache. It has always worked wonderfully on my shared hosting environment. It can be a little tricky to set up, but there are great instructions provided and it’s worth it. I, personally, find having ‘Cache Rebuild’ and ‘Coarse File Locking’ switched on helps too. I also have compression switched on, although that tends to only help with bandwidth usage.

Reduce Amount Of Plugins

This may seem obvious, but the less plugins you have the less memory you are likely to use. You might also want to look out for plugins that load scripts/options all the time, even when they are not needed. This can add extra weight to WordPress by loading information even when the plugin is not required to do anything.

Timthumb!

Otherwise known as a dynamic thumbnail generation script, Timthumb uses PHP & GD to create alternate sized thumbnails. It is quite often in themes, especially magazine style themes to create thumbnail sizes WordPress doesn’t normally create. While Timthumb does actually have a cache to store the images it creates it isn’t as efficient as a WP Super Cache style cache.

More often than not you do not want to spawn a PHP process when it isn’t needed, especially on shared hosting. Why? Well because loading a WordPress page takes, as I said before, about 20-30MB of memory. If you can load a page without PHP spawning you won’t use large amounts of your available memory & you could probably survive a front page feature on Digg. That is what WP Super Cache does, it saves the result of PHP processing your request for a page to a flat HTML file & redirects you to that flat file using Mod Rewrite (Apache Module) the next time you want to view it.

What’s That Got To Do With Timthumb?

Well Timthumb caches images after they are created, which is great. The problem is that to find if an image is cached it needs to use PHP, but you don’t want to run PHP as a normal PHP process still uses between 8-13MB of memory. That’s memory you can’t afford if WP Super Cache is re-caching your page (20-30MB + 8-13MB = 28-43MB). Therefore the best option is to modify Timthumb to work exactly as Super Cache does, using Mod Rewrite to direct you to the cached file. This will probably make this post huge, but here’s how I got mine working. A big thanks to 2 examples (linked to below) of Timthumb being used with Mod Rewrite I found on the web, although the exact same code didn’t work for me, the code here is only very slightly modified.

First you need to find this section of code in Timthumb:

Now comment out the highlighted lines, and/or replace them with these lines:

Thanks to speedingupwebsite.com for some of this code. We basically change Timthumb so it doesn’t md5 the filename, as we need a nice sensible filename for us to rewrite to.

Next we can go onto the Mod Rewrite. I can’t deny that Mod Rewrite is powerful, but I hate the stuff so please forgive any mistakes I make here.

Again thanks to speedingupwebsite.com & also thanks to hungryhacker for the code here. I’ve modified it (very slightly) as while it was very helpful it just refused to work for some reason.

Let’s explain what’s going on here. First we check if the script we are asking for is timthumb.php. If you called the script something else change that part. The next part checks the query string. We want to match the images filename, the width, the height & if zoom crop is turned on. The sections in parenthesis can then be used in the next part to fill in the new filename we gave thumbnails saved by timthumb. We use %{DOCUMENT_ROOT} to get the path to the root of our site & then we fill in the path to the cache folder created by timthumb. The -f tells Mod Rewrite to only return true if the file actually exists. If those three conditions are all true we try to redirect the request for timthumb to the actual saved image file. If those rules are not true it will hand the query to timthumb as normal.

This has worked wonders keeping the strain on the server down for my website. Just remember that you will need to change both the paths to where your timthumb cache is. It works in exactly the same way as Super Cache, redirecting requests to Timthumb to the actual image file should it exist.

There we have it. My list of ways to help optimize WordPress for running on shared servers. I just want to make it clear that the Timthumb Mod Rewrite cache was not my idea, the code is a slightly modified version made with help from the two sites I linked to above. All credit goes to them for the idea & the code.

I hope this post was useful & it has helped you optimize your WordPress install. I currently have all of the tips here implemented here on Return True & I’ve noticed a dramatic increase in performance. As always if you have any questions about anything in this post, please post them in the comments.

written by

WordPress wizard, coding ninja and all round cool guy. Loves Sci-Fi, geeky stuff, and of course Firefly. Also a long time sufferer of Strahovski-itis. That's the love of Yvonne Strahovski for those who don't know.

Advertisement

111 Comments

Author’s gravatar

Great tips man.

For the TimThumb optimization, there is a simpler-but-less-efficient alternative that you can just extend the Expires Header from 9999 secs to 1 week or so for Browser caching.

Reply
Author’s gravatar

Thanks Totti,

that is a great tip regardless of if you are using Timthumb or not. It saves a lot of bandwidth telling your browser not to re-download the image if it doesn’t need to.

Author’s gravatar

Have been researching a lot on this albeit no – success… these codes do not work with the current version of timthumb.

I’ve posted a solution here but that too is not a sure shot – http://stackoverflow.com/questions/3729249/how-would-timthumb-php-affect-the-seo-of-a-website-or-image-seo

Can we together solve this puzzle?

Reply
Author’s gravatar

There are a lot of people continuing to stick with Timthumb & I’m not sure why. While it still has it’s uses it really isn’t needed in WordPress theme development as much thanks to the WP thumbnail system. If you are interested in SEO and speed then I would advise using the thumbnail system first and then reverting to Timthumb if it is still needed.

I don’t have a tutorial on how to use the WP thumbnail system as it’s already been documented far to many times, I did however do a passing tutorial on it in one of my earlier Nivo slider tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">