How to create your own CDN for $5/month
So what is a CDN? A Content Delivery Network (CDN) is a network of servers distributed geographically, designed to deliver internet content more quickly and efficiently to users. When a user requests content (like a webpage, video, or image), the CDN redirects the request to the server closest to the user, reducing the distance the data has to travel. This proximity reduces loading times, improves website performance, and decreases the likelihood of experiencing buffering or delays, especially for websites with high traffic or global reach. CDNs also provide additional benefits like enhanced security against DDoS attacks and reduced bandwidth costs for website owners. They essentially act as a network of proxy servers, optimizing the delivery of content to users worldwide.
Most importantly, for those on a budget, a CDN will reduce server bandwidth significantly allowing for a smaller server to host your website. Images, videos and multimedia content are heavy and will bog down your server if you are hosting them yourself. Not only does it improve overall site performance but end users will get the content faster on a CDN. So it's a win-win to use a CDN, but they can get pricey. Here is a simple way to create your own CDN on a shoestring budget.
Requirements:
- Cloudflare account - $0
- Digital Ocean account - $5/month
- Your own domain
First, create a Digital Ocean spaces object storage. The Spaces Object Storage feature is an S3-compatible object storage service that lets you store and serve large amounts of data. Each Space is a bucket for you to store and serve files over Digital Ocean's CDN. While creating, be sure to select the CDN option to enable this feature.
Once you have a bucket, create your file structure and upload some test files. Be sure to make them publicly accessible. After that is complete, copy your CDN endpoint provided at the top left of your newly created Spaces Object Storage bucket. You will need this for the next step.
Next, choose your CDN subdomain. Some good examples include: cdn.example.com, static.example.com or media.example.com. Once you have your subdomain picked, log in to your Cloudflare account. Go to your domain DNS for the domain you chose to use for your CDN (under Websites->"domain"->DNS). In the examples above it is "example.com". Once in your DNS, create a new CNAME entry by selecting "Add record".
- In the setup, set the entry type to CNAME.
- For Name, enter just the subdomain you decided on, like static, media or CDN.
- Under target, paste your CDN link you copied from Digital Ocean.
- For proxy status, leave this on.
- Click Save
After that is complete, jump down to the SSL/TLS Overview section on the left had navigation in Cloudflare. Be sure you have already completed setup to use "Full" of "Full Strict" mode on Cloudflare SSL/TLS encryption. If that is completed, select "Origin Server" on t he left hand navigation. Under Origin Certificates, click create certificate. Follow these steps to complete setup:
- Leave "Generate private key" selected.
- Remove any hostnames listed and type in your selected subdomain like so: cdn.example.com
- For Certificate Validity, select a long enough option so you do not have to modify this frequently. It can always be revoked early.
- On the next page, copy and save both your newly generated Origin Certificate and Private Key.
- Be sure to store the key in a safe place as it will not be accessible later.
- Click "OK"
Now that you have your cert, lets finish the setup in Digital Ocean. Hop back to your Digital Ocean account and go into your new Spaces Object Storage. Next, select the settings tab. Under settings, select Change->Edit CDN Settings on the CDN line. If you cannot select change, enable CDN first. Next do the following:
- Click "Add a new subdomain certificate"
- Select the Bring your own certificate tab
- Name your certificate something easy to remember like "subdomain-domain"
- In the Certificate box place your certificate you created on Cloudflare. Copy all lines including the lines with dashes.
- In the Private Key box, copy your private key you created on Cloudflare. Copy all lines including the lines with -----
- Leave the certificate chain blank
- Click Generate Certificate and Use Subdomain
Under the CDN options, you should now see your subdomain. Click save. If you do not see your subdomain, the certificate does not match the subdomain, meaning one of the previous steps was done incorrectly. If you see your subdomain, give it a minute or two and then type your new CDN domain into your browser followed by your file structure and a file you uploaded: cdn.exmaple.com/folder/img.jpeg. If everything was set up correctly, you should now see your content. You successfully created a CDN and have your own network to upload your content to.
I hope you enjoyed the tutorial. If you have a question on setting up technology on a budget, feel free to reach out to us and maybe we'll create an article to address your needs.