Save a HUGE bandwidth cost in WordPress by automatically serving media contents from copy.com
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 from WeLoveServers. 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 https://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
wget https://copy.com/install/linux/Copy.tgz --no-check-certificate
Unzip the tarball
tar -zxvf Copy.tgz
Run the client
cd copy
cd x86_64
nohup ./CopyConsole -u=your_copy_email -r=/root/copyhome -p=yourpassword &
The benefits 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 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
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 <command_number>'.
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.
ln -s /var/www/wordpress/wp-content/uploads /root/copyhome/uploads
And copy.com app will start automatic 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.
function link_replacer($content){
return str_replace("http://url_to_your_wordpress_blog/wp-content/uploads/", "http://copy.com/rNvQU2t2o4Z8/uploads/", $content);
}
add_filter("the_content",link_replacer);
The Secret: Now you owe me a secret, which I'd promised earlier in this article. Did you know that copy.com supports seamless media streaming for popular audio and video formats? As our media contents are properly synced with copy.com, you can now upload videos and audios which will be streamed without any problem at all, directly from copy.com :)
I hope you've enjoyed this article :)