Search This Blog

Tuesday, October 15, 2013

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

Saving a HUGE bandwidth cost in WordPress by automatically serving media contents from | 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 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 app
You can use your existing or register a new one using my referral code, 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 --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 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 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 <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
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 so that everything you upload from WordPress, will be properly synced into your 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 app will automatically start syncing the contents of your uploads directory almost immediately. Impressive, eh?
Step 3: Share this folder in
This is an important step, you must share the uploads directory from your account publicly. Just log into your 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 screen (something like, copy that url.
Step 4: Serve images directly from 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("://your_wordpres_blog_url/wp-content/uploads/",
"://", $content);

No comments:

Post a Comment