Search This Blog

Tuesday, October 15, 2013

Saving a HUGE bandwidth cost in WordPress by automatically serving media contents from copy.com | The Storyteller

Saving a HUGE bandwidth cost in WordPress by automatically serving media contents from copy.com | The Storyteller:

The title almost says it all. Our mission is to save the bandwidth cost (and ensure better deliverability) by leveraging the power of headless installation of copy.com client in Linux, and then integrating it into WordPress. The integration must work seamlessly so that the viewers don’t see a difference, and at the same time you don’t have to put any extra effort. Beside saving bandwidth, this also reduces extra load from your web server. There’s another surprise which I will tell you later. For now, keep reading :)
You need at least a VPS to make this setup working, preferably with root access. These days VPSes are cheap. You can purchase an 128MB VPS for ~14/yr from Ramnode (such a fantastic provider) or may be for ~19/yr fromWeLoveServers. Or feel free to use your existing VPSes if you have one.
Step 1: Headless installation of copy.com app
You can use your existing copy.com or register a new one using my referral code http://copy.com?r=Tbcrni, you and I both will be getting an extra 5GB if you do so.
Now log into your linux box via SSH, you need to have root privilege to complete this step.
Download the linux client
1
wget https://copy.com/install/linux/Copy.tgz --no-check-certificate
Unzip the tarball
1
tar -zxvf Copy.tgz
Run the client
1
2
3
cd copy 
cd x86_64
nohup ./CopyConsole -u=your_copy_email -r=/root/copyhome -p=yourpassword &
The benefit of using `nohup` is that it makes sure your process will not be interfered even when you logout from your current ssh session. And adding a trailing `&` makes the process run into the background, without blocking the screen. You can do the same thing with `screen` command in linux and then detaching the active screen.
Oh one more thing, nohup runs the process with default priority (0). If you want to run it as a low priority process, you can use `nice` command with nohup like this. By default -20 is the maximum priority, and 19 or 20 is the lowest priority. The following command runs the copy.com headless client with a priority level 5
1
nohup nice -n 5 ./CopyConsole -u=your_copy_email -r=/root/copyhome -p=yourpassword &
Security Tip: Always remove such commands from history which contains your password/username. To do that you can simply run a `history -d <commandid>` command
If you read the command above, you may have noticed that we told copy app to use `/root/copyhome` directory as it’s root folder. It’s important that you remember it.
Step 2: Map the WordPress upload folder with copy.com
This is a comparatively easier step. If your WordPress installation directory is /var/www/wordpress then there are 95% chance that your upload directory is located at /var/www/wordpress/wp-content/uploads, unless you had changed it exclusively. So we are going to map this directory into copy.com so that everything you upload from WordPress, will be properly synced into your copy.com account. It’s easier, remember? Just create a symbolic link like this and you are all done.
1
ln -s /var/www/wordpress/wp-content/uploads /root/copyhome/uploads
And copy.com app will automatically start syncing the contents of your uploads directory almost immediately. Impressive, eh?
Step 3: Share this folder in copy.com:
This is an important step, you must share the uploads directory from your copy.com account publicly. Just log into your copy.com account in the browser, right click on the uploads folder, select share and share publicly. Immediately you will see a public URL for this folder in your copy.com screen (something like http://copy.com/rNvQU2t2o4Z8), copy that url.
Step 4: Serve images directly from copy.com instead of your server.
You can do this step by several ways, for example, by registering a hook or doing it via http redirection using .htaccess. Lets do it using a simple hook this time. Of course you can fine tune it any way you’d like to, I am just keeping it simple in this article.
1
2
3
4
5
function link_replacer($content){
    return str_replace("://your_wordpres_blog_url/wp-content/uploads/",
"://copy.com/rNvQU2t2o4Z8/uploads/", $content);
}
add_filter("the_content",link_replacer);

No comments:

Post a Comment