Optimizing WordPress For Shared Hosting

/ PHP, Wordpress / by Paul Robinson / 111 Comments
This post was published back on May 25, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

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.

111 Comments

Author’s gravatar

I’m having the same issue with HostGator…

When looking at your instructions above, the “highlighted lines” do not look right on my browser. The blue highlight stops in the middle of the word “version”.

What exact part of the TimThumb code do I need to replace?

Reply
Author’s gravatar author

Hi Matthew,

That’s not a problem with just your browser, unfortunately it happens on all browsers I’ve tried.

As for what to replace, the entire highlighted line. The highlighting just stops due to a bug in the syntax highlighting plugin I use here on the blog.

Author’s gravatar

Hello Paul,

First of all let me thank you for this awesome post. My main concern was to put timthumb generated thumbnails on CDN which after many hours i was able to implement. Now, timthumb generated images are coming from CDN but on testing i found that images are not compressed obviously because of &h &w parameters. I tried your technique but in my cache folder it created cache -100-100-80.png format and file name was not generated. Is there something which i may be missing. Moreover, these generated thumbs were never called (obviously because file name is missing).

Is there are particular reason of generating all the thumbs in .png format ( i think .jpg saves bandwidth). Correct me if i am wrong.

Reply
Author’s gravatar author

Hi Katie,

This post is a little out of date now in regards to Timthumb. With WordPress now having a custom thumbnail generation system of it’s own there is no need to use a script like Timthumb, and hence I don’t really recommend the use of Timthumb much any more, although there are still some circumstances where it would be of benefit to use it.

The problem with Timthumb is that it is called asynchronously (just like real images) so if it is on the page 4 times, it is called 4 times pretty much all at the same time. This uses 4 times the memory on your server (depending on the setup) than normal & can cause problems, especially on a shared server.

The only problem with WP’s new system is that it involves editing your theme & can be difficult to do if you aren’t used to doing that sort of thing.

If you like I can help you out via email. Just drop me an email via the contact form and I’ll get back to you ASAP.

Author’s gravatar

Thank you for sharing this one.. Hostgator send me this link for me to optimize my site. I hope it will help me a lot.

Reply
Author’s gravatar

Hey Hostgator try me to analyze your article about optimization for my wordpress site. I will implement this plugin now. really right now.. because hostgator kill my site for some reason. Very disgusting I don’t like it anymore..

Reply
Author’s gravatar

Very helpful.. Optimizing my site now.. Great I found it here.

Reply
Author’s gravatar

Terima kasih atas Artikel dan Info yang selalu menambah wawasan.semoga sukses

Reply
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 author

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 author

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.

Newer Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more