Cloud Computing idea PHP Tricks wordpress

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

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 from WeLoveServers. 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
[sourcecode language=”shell”]
wget –no-check-certificate

Unzip the tarball
[sourcecode language=”shell”]
tar -zxvf Copy.tgz

Run the client
[sourcecode language=”shell”]
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

[sourcecode language=”shell”]
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.

[sourcecode language=”shell”]
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.

[sourcecode language=”php”]
function link_replacer($content){
return str_replace("://your_wordpres_blog_url/wp-content/uploads/",
"://", $content);

The Surprise: Now it’s time for a surprise, which I’d promised earlier in this article. Did you know that supports seamless media streaming for popular audio and video formats? As our media contents are properly synced with, you can now upload videos and audios which will be streamed without any problem at all, directly from 🙂

I hope you’ve enjoyed this article 🙂

14 replies on “Saving a HUGE bandwidth cost in WordPress by automatically serving media contents from”

have to try it out. Looks exciting 😀
একটা ছোট্টো টাইপো সম্ভবত ভাইয়া – in the last potion, shouldn’t it be “Now I owe you a surprise” instead of “Now you owe me a surprise” ?

Boss, is it still working?? I’ve created a publicly shared folder in my account, uploaded some images in that public folder, but when I try to access any image via url, that url loads the file viewer page instead of serving the image over http. Or I’m missing any other configuration/setting in account to achieve this??

I’ve verified that, static file serving only works with http protocol, accessing files via https protocol loads the file in a hosted viewer page in

You may want to update the post to mention about this as well as changing the wordpress filter to something like below:

function link_replacer($content){
return preg_replace(“#http(s)?://” . preg_quote(‘your_wordpres_blog_url/wp-content/uploads/”,
“”, $content);

Also it might be worth noting that, all text files are served with text/plain mime types, even though the file extension is css or html or js

Correction in above code:

function link_replacer($content){
return preg_replace(‘#http(s)?://’ . preg_quote(‘your_wordpres_blog_url/wp-content/uploads/’,’#’) . ‘#m’,
“”, $content);

Interesting!! Now when I tried it again, it is working as expected. May be I was doing something wrong while building the url. Anyway, thanks for the great idea & sharing it.

BTW, I would be more happy, if static text files could also be served from with appropriate mime type and I could move the css & js files along with the images there. If you can figure out an way for this, please share as well.

Thanks again (Y)

Leave a Reply

Your email address will not be published. Required fields are marked *