Alternative Use of Google App Engine for Optimizing WordPress

Alternative Use of Google App Engine for Optimizing WordPress

Tutorial Details
  • Program: WordPress
  • Version: 3.0+
  • Difficulty: Intermediate
  • Estimated Completion Time: 30-45 mins.

I was looking for a solution to optimize my site, in a very cheap way. Often a Content Delivery Network is a good solution but not cheap, so I used Google App Engine as a CDN with Ubuntu.


Step 0: A Little Bit of Theory

Things like stylesheets, images and videos are often the major hit taken by a webserver when a webpage is loaded. It’s been proven that loading these assets from different domains helps the browser with multi-tasking, because it doesn’t have to wait until Asset 1 on the domain example.com is loaded to start downloading Asset 2 on the same domain. There are a number of approaches to this, and in this tutorial we’ll analyze three of them.

The First Approach

Most people simply create sub-domains such as “images.domain.com”, “styles.domain.com” and so on. The assets are then called from the sub-domains, so the browser ‘thinks’ it’s coming from a different location, when in reality, they normally always target the same structure.

This approach is useful, and there’s a noticeable difference in loading times. A downside to it is that you’re still hitting your webserver, and now even harder, as you’re making multiple connections at once to it. If you are loading images or small files, that’s OK, but things will start to get uglier if you try to load videos or heavier files; and also, especially for cheap hosting, it’s not always possible to create sub-domains.

The Second Approach

This consists of having a second webserver only to load assets while your main webserver deals with the other calls. You have two webservers using two different ports. The main webserver is the one responsible for all the requests, but will redirect every asset call to your second webserver. What people normally do here is use two different kinds of webservers. Most commonly Apache (as the main webserver) and lighttpd as the secondary webserver.

The concept is really pretty, but it also means you’ll now have to maintain two webservers. The second one (lighttpd) should really be a one off, as once it’s configured, it will just serve static content. Still, it’s two webservers on your server, and if anything goes wrong, it’s a bit difficult to troubleshoot.

The Third Approach

Basically it consists of hosting your assets somewhere else, where you won’t have to maintain or keep an eye on performance, as this server’s primary task is to serve static content. This heaven is called “Cloud Computing” and it’s a “buzz word” at the moment. Basically it consists of a network of servers in strategic places. We call it strategic places, as the servers are located near you. Well, not near you, but from your IP, it redirects you to the closest server, from where you’re going to be picking up the assets as they are needed.

It works pretty much as a load balancer, but based on location, instead of number of connections. It makes sure that the bits travel the shortest distance in order to get to you. In other words, if you’re in Italy (like me), why should you have to wait for the data to travel from the USA, if there’s a server just next door to you. There are some companies offering CDN for a very reasonable price, but today we’re going to be talking about free stuff!

Before We Start

Before I start with this tutorial, I have to make it clear that the form of CDN we’re going to be using here, is not truly CDN, as it doesn’t offer a very high scalability, and hasn’t been developed for the sole purpose of serving static files. This means it won’t have the same high performance of a service designated specifically for this task. For some (like myself), it won’t make such a huge difference, as this method is still better than nothing, and is immensely faster than many servers around. Also for most people who use Hosting Companies and have limited bandwidth, this will be a huge improvement.


Step 1: Google Is Your Friend

Create a Google Account. If you have a Gmail account that will do. I won’t explain it in detail, but visit here to create one. Sign up for a Google App Engine Application. And now…


Step 2: Let’s go! Create our CDN

Simply login to your newly created App Engine Account and click the button Create an Application, I named mine “mynewcdn”. I use Python with Ubuntu (but some steps are the same for Windows users) so:

  • With the default installation of Ubuntu, Python interpreter is already present in the system. Otherwise just install Python. If you’re Windows users you can download Python from Python.org.

  • Download the latest Google App Engine SDK for Python (currently at 1.5.5);

  • Create a folder called Projects and extract Google App Engine SDK for Python into it

    fabrizio@fabrix:~/Projects$ unzip google_appengine_1.5.5.zip
    fabrizio@fabrix:~/Projects$ cd google_appengine
    fabrizio@fabrix:~/Projects/google_appengine$ mkdir mynewcdn
    

    and now you should have this (the name of the folder SHOULD to be the same as the name of the application)

    fabrizio@fabrix:~/Projects/google_appengine$ ls
    appcfg.py           dev_appserver.py  mynewcdn              tools
    BUGS                gen_protorpc.py   new_project_template  VERSION
    bulkload_client.py  google            README
    bulkloader.py       lib               RELEASE_NOTES
    demos               LICENSE           remote_api_shell.py
    
  • Now inside of your folder (for me mynewcdn) you should copy all the folders with the static contents of your website (for example the folder /wp-content/uploads/). In my case I created a folder inside mynewcdn called fdicarlo with all data. Inside a folder with the name of the application that you created, make a file app.yaml. The content of this file should look as follows:

    application: mynewcdn
    version: 1
    runtime: python
    api_version: 1
    
    handlers:
    - url: /fdicarlo
      static_dir: fdicarlo
    
    threadsafe: true
    
  • Of course you must replace mynewcdn with the name of your application and fdicarlo with the name of the folder of your static content. You can find more info about the app.yaml file on the Python Application Configuration page. Now the situation looks like this:

    fabrizio@fabrix:~/Projects/google_appengine/mynewcdn$ ls
    app.yaml  fdicarlo
    
  • And now:

    fabrizio@fabrix:~/Projects/google_appengine/mynewcdn$ cd ..
    fabrizio@fabrix:~/Projects/google_appengine$ python appcfg.py update mynewcdn
    

Once the upload has finished, your CDN is ready at the address nameofapp.appspot.com, mine for example is http://mynewcdn.appspot.com/fdicarlo. To verify the CDN is working correctly try to view some pictures and/or files from the CDN.


Step 3: Configuration of CDN

When you have created the instance on the Google app engine, and have the URL of the app, the only thing left to do is to add it to the settings in WP Super Cache and test how the page works. If somewhere in those steps you have a feeling that nothing is happening, then try to clear your cached pages from enabled cache, in the other tab called “Contents” and save the settings. I used WP Super Cache because it’s useful (and powerful) also for the cache but you can use other plugins like CDN linker and the steps are similar.

In the field “Off-site URL” I insert http://mynewcdn.appspot.com/fdicarlo. Of course, you should to replace mynewcdn and fdicarlo with your name.

In the field “Include dirs” I put wp-content.

And in the last field “Exclude if substring” I suggest you to insert “php” to exclude php files. Set your parameters how you like, save and now you have configured your CDN. Now when you add pictures or other data in WordPress you should also copy to the local folder and update all with:

fabrizio@fabrix:~/Projects/google_appengine$ python appcfg.py update mynewcdn

Conclusion

I hope that has helped someone to create and start using Google CDN. Using a CDN is just one aspect of SEO and a slow performance is often caused by many factors. If you use a CDN, minifying and caching you can have good improvements to your blog so there’s really no reason not to try them.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://daankortenbach.nl Daan Kortenbach

    I would suggest checking out CloudFlare since it is much easier and has many more advantages.

  • http://www.waltdesign.com/ Walter Wimberly

    A couple of comments:

    1) Browsers can download multiple items from a server at a time. Old browsers (IE6) can download 2 items at a time, modern browsers between 6 and 8 items in parallel (depending upon which browser).

    2) Also App Engine is no longer free. It is cheap at only $.12 per gig of transfer, but not free anymore.

  • http://www.fdicarlo.com/ Fabrizio Di Carlo
    Author

    @Daan I didn’t know CloudFlareand its services, when I try, I do not exclude to write a review about it :)

    • http://daankortenbach.nl Daan Kortenbach

      Looking forward to your review. I achieve excellent results with it, both in server load as in parallel downloads. Also check out their Rocket Loader technology for maximum performance (still in beta but worth to take a look at).

  • http://www.iuditg.com Udit Goenka

    I have been using CloudFlare from a long time and I think it’s so much easier and quicker to use that. Even the free version of CloudFlare creates a huge difference. So CloudFlare + WP Total Cache can do wonders.

  • http://www.webmaster-source.com redwall_hp

    Amazon CloudFront isn’t really “expensive” for a smaller site, and it’s easy to set up W3 Total Cache to work with it. It’s 12 cents per gigabyte of data transfer. This month, I haven’t even reached one gigabyte, and I pull in about 40k page views per month.

    • http://www.webmaster-source.com redwall_hp

      Update: I have used 1.333GB this month, for a grand total of $0.25.

      • http://www.webmaster-source.com redwall_hp

        Palm, meet face. That should have been $0.64. Not a huge difference, but some pedantic type would probably have done that math…

  • Pingback: Alternative Use of Google App Engine for Optimizing WordPress | Shadowtek | Hosting and Design Solutions

  • http://www.dynamicguru.com Dynamicguru.com

    Seems interesting,
    does any one have approx. figures about how much difference using a cdn makes on page load times and server load?
    I intend to implement this on my wordpress blog

    • http://www.fdicarlo.com Fabrizio Di Carlo
      Author

      @Dynamicguru send me your mail address on twitter or contact me on my site fdicarlo [dot] com and I’ll send you a pictures about improving :)

  • http://xtendedview.com How To Tech

    how can i set my default way of uploading files in wordpress to google app engine? Or i need to manually upload all files to it?

    • http://www.fdicarlo.com Fabrizio Di Carlo
      Author

      You should to upload manually, alternative I think that you can write a shell script for this operation but I can’t help you in this :)

  • http://www.andreamoro.co.uk/ Andrea Moro

    Very nice and informative article, just don’t understand why we have to go through all these complicated python stuff.

    Would not the online configuration do everything we need?

    • http://www.fdicarlo.com Fabrizio Di Carlo
      Author

      @Andrea Moro: thanks for the comments, you should to use Pyhton (or Java) because GAE is not designed for CDN use but for host your web applications, CDN is a simple work-around ;)

  • http://db.tt/Kq9uuOd Vinod

    Why not use Dropbox instead? Which offers Free upto 2 GB.
    By placing static contents in Public folder.

    • http://www.fdicarlo.com Fabrizio Di Carlo
      Author

      @vinod: yes I can use Dropbox, CloudFlare, S3, etc etc… But in this tutorial I used GAE :) DropBox is a nice product, I have more than 6GB account but ok if you have more file than 6GB? So you should to use a scalable solution ;)

  • Charul Shukla

    Here are couple of workarounds i would like to suggest

    Scripts Files and CSS files:

    1). Can use google code service to create a new project and then host all the files from there
    2) Always load stuff like jquery from their google CDN
    3) In case of jquery, don’t always go for latest version, as it’s quite possible that some older version will meet your requirements and will be smaller is size too
    —————————————————————-

    Also use a service like cloudflare / Google Pagespeed + some good caching plugin(in WP case)
    —————————————————————-

    Images

    PNG will be your friend, do some experiment for correct ratio of quality and size and host them on different subdomain

    ———————————————————————————

    try to reduce the number of JS files you are using and yes always use a minified version of JS and CSS

    ———————-

    I am sure from my experiences of last decade, a sum of all these will do wonder for any website :)

    • http://www.fdicarlo.com Fabrizio Di Carlo
      Author

      Nice tips :)

  • Pingback: WordPress: Google App Engine als CDN für statische Dateien nutzen » Google, Dateien, Engine, Application, AppEngine, Ordner » BLOGRAMMIERER