Categories
Tutorials

How to Make A Cheap CDN with Cloudflare

If you run a site and want to speed it up, a content delivery network (CDN) can be a great option. Today, I’m going to explain my cheap and cheerful solution using Cloudflare, an 80p/year (including renewal) domain, and Apache – and show you how to implement your own.

Basically, Cloudflare takes the files I store on my CDN and caches them on servers across the world – from Europe to North America, Asia to Australia. Cloudflare also does some neat stuff like offering free SSL certificates and a nice DNS management console.

The Domain

You’re going to want to navigate over to gen.xyz and register a 6 to 9 number long .XYZ domain. These domains only cost $0.99 to buy and renew – they’re great value for this sort of stuff.

Cloudflare Setup

Now we need to add our new domain to Cloudflare. Navigate to dash.cloudflare.com, and login or sign up. When you’ve verified your email, you’ll be able to add a domain.

Hit add a new domain and walk through the steps.

Choose the free plan – we don’t need anything more!

Now Cloudflare’s going to ask us to change our nameservers. These are different for everyone (sort of), but generally follow the rule of name.ns.cloudflare.com – eg bob.ns.cloudflare.com. Add the two records they add you to ask by navigating to gen.xyz/account/clientarea.php.

Once you’re at the gen.xyz client area, hit manage next to your new domain, then scroll down to nameservers and enter the nameservers that Cloudflare gave you – hit save.

It normally takes about 15 minutes for the nameservers to update. Cloudflare will email you when they’re updated.

Adding DNS Records

It’s more or less smooth sailing from here! Open dash.cloudflare.com and click on your new domain. Now press on DNS in the top nav-bar. You’ll need to add your server’s IPV4 and/or IPV6 address(s) now on the “Name” (subdomain) cdn.

Adding the IPV4 address:

Adding the IPV6 address:

If you added both IPV6 and IPV4, your configuration will look like this:

This is very important: navigate to SSL/TLS at the top and change your SSL setting from Flexible to Full.

Server Side

Now all we really need to do is add a virtualhost to make the CDN work.

Login to your server and run the following command:

sudo nano /etc/apache2/sites-available/cdn.conf

Now, use my VirtualHost generator to make your virtualhost. Just enter cdn.YOURDOMAIN.xyz as the domain (replace YOURDOMAIN with, shockingly, your domain), and /var/www/cdn as the directory.

Next, paste the results after pressing generate into cdn.conf – you can remove the last two parts that are www.cdn.YOURDOMAIN.xyz (port 80) and www.cdn.YOURDOMAIN.xyz (port 443) or leave them, we aren’t resolving them either way.

Hit CTRL + X and then press Y.

Now run the following commands:

sudo a2ensite cdn.YOURDOMAIN.xyz
sudo systemctl reload apache2

Finally, we just need to add an SSL cerificate to the domain, run this:

 sudo certbot --apache --redirect -d cdn.YOURDOMAIN.xyz

Adding Content

That’s it. Sorted. Now just upload your CSS, JavaScript, images, videos, audio, and whatever else you’d like to your CDN and it’ll work.

Finishing Notes

Whenever you’re updating existing files, turn on development move in Cloudflare, it’ll save you a huge headache.

I recommend you turn off indexing of files, to do this, create a .htaccess file in your CDN’s folder (where you put all the content) and add this:

Options -Indexes

Leave a Reply

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