Archive for September, 2011

Using Content Delivery Network (CDN) with WordPress

A brief for a recent project included a requirement for the site to be fast.

I always optimise my WordPress installations so it’s delivering cached pages to visitors. The benefit of caching is that the pages load faster as the size of the page requests are smaller. This is because the database queries are stored and do not have to be ran each time a visitor requests a page. Such technique is essential for a CMS like WordPress; otherwise the site is going to take a long time to load and your visitor/customer is going to go elsewhere.

The difference for this site it was an image intensive site for photography meaning big images had to be loaded regularly – even more of a problem. The solution then, a Content Delivery Network (CDN).

The products

Amazon Web Services

My CDN of choice was Amazon’s Cloudfront. Files are stored on Amazon S3 and Cloudfront then distributes them when requested. There are others on the market but just through Amazon’s reputation I chose them. I’d be open to using others in the future but only if I could see a real difference.

The details

When a new image is uploaded through the media uploader it is pushed out to the Amazon S3 bucket. The Amazon S3 bucket therefore is a mirror image of the uploads folder in wp-content.

When a visitor hits the site, they are served the image from Amazon S3 which is faster than most other web servers. There’s also the added benefit that the images can be requested at the same time as other files from the web server which means more requests can happen concurrently – again, speeding things up.

The benefits

The hosting package I use for my client is a standard package – so it’s not necessarily the quickest. If my client wanted to have quicker hosting then they’d be looking at £30 per month, instead of £60 a year – a massive difference and something most clients would not be prepared to do.

With Amazon S3 and Cloudfront you are only paying for the amount of requests per files, rather than a flat fee. This invariable costs less due to the ‘Pay-as-you-Go’ type billing.

My costs for the last month for this site and Ally’s site were $0.78 so it hasn’t exactly broke the bank, and the speed, well there’s no doubt it’s much faster.

Do you need help setting up an CDN for your site, or do you need other areas of your site speeding up? Get in contact and I’ll be able to help you out!

How to upload multiple files to a web server

When I recently moved house I had to go three weeks without broadband. In it’s place I used a 3G modem in order to be able to keep running my business. It felt like going back to the 56k modem days at times, but it was tolerable for a short period.

One thing I did struggle with however was uploading multiple files to a remote web server. I usually upload WordPress other applications to web servers at least once per day which include thousands of files – that’s thousands of individual requests, giving my FTP client more opportunities, especially with slower upload speeds, to timeout.

So, instead of uploading multiple files I zipped them all into one file and transfers them onto the server. I then unzip them on the server using a PHP function, which takes seconds.

Here’s how I did it:

  1. Zip the files up into a file call it myfile.zip
  2. Upload myfile.zip to the folder on your web server into the correct folder.
  3. Create a PHP file named unzip.php in the same folder as your zip file with the following in it:
    <?php
    $zip = new ZipArchive;
    $location = $_SERVER['DOCUMENT_ROOT'];
    $zip->open('myfile.zip');
    $zip->extractTo("/$location/");
    $zip->close();
    ?>
  4. RUN unzip.php from your web browser and it will instantly unzip all the files.
  5. For security purposes delete myfile.zip and unzip.php from your server so nobody else can unzip files on your server.

And that’s about it. It was a massive time saver me for and increased the stability of my uploads over a slower internet connection.