Beginner guide to Caddy Server | 2022 | HackerRahul

Β·

3 min read

Beginner guide to Caddy Server | 2022 | HackerRahul

Hi, My First ever Blog after so long and many things have been changed since then like:

  • Platform Changed (From WordPress -> hashnode)☺️
  • I Got a Job as a Full Stack Developer
  • I am Working on my Startup, will announce that soon.
  • and Much more.

So here we go again, writing down my first ever blog on my not so first blog!

In this article, we are going to learn how to setup Caddy Server on a VPS, So Lets get started,

What Is Caddy Server?

Caddy is a web server that allows user to do things like reverse proxy, Serving static files, Serving PHP Sites, load balancing and much more.

Installing Caddy Server

For this tutorial we are going to take an example of a linux machine but you can download on any machine you have.

So To install caddy server, lets run these commands.

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

You can also take a reference on how to install from the official Documentation

So now that we have our caddy server installed and running, we can confirm this by running the following command

sudo systemctl status caddy

If it shows active, then its successfully installed ! πŸ₯³πŸ₯³

So now lets move to the next step.

Caddyfile Setup!

So Caddyfile is one of the way to setup applications in Caddy Server. So this is a pretty straight forward syntax unlike apache or nginx.

To Setup an application in Caddy file, Lets assume we have a Node JS (Express) app running on Port 3000

but instead of giving IP Address to your User, You prefer to give domain to easy access to the site.

So Now The Step to achieve the same are:

  1. Add these DNS Record in your Domain DNS setting, for ex. your VPs IP is x.x.x.x, you need to add an A Record
TypeHost
Ax.x.x.x
AAAAx.x.x.x

One you added these records, you need to wait upto 24 hours for DNS to propogate globally.

  1. Now you have your domain pointed to the server and we have a Node Js app running on port 3000. Lets Reverse Proxy using Caddy!

The default path you can find your caddyfile is etc/caddy/Caddyfile

To Edit your Caddy file lets Nano into it

nano /etc/caddy/Caddyfile

or enter the path where your Caddyfile is.

Lets get our hands dirty now! πŸ˜›

To reverse proxy you xyz.com domain to your Node JS app running on port 3000

xyz.com {
  reverse_proxy x.x.x.x:3000
}

After adding it make sure you reload your caddy service

sudo systemctl reload caddy

Thats Basically it! Sweet right? 😁

The fun Part is, now if you access your domain you have a node JS App running and

A Free SSL that renews automatically like a magic. ✨✨

Let me tell you one more thing, Hashnode itself uses caddy to serve its website and blogs. so whatever you are seeing right now is running using caddy server. cool right?

Now if your want to add more app and domains or do advance things like load balancing and all, you can edit the same Caddyfile and do it.

for Ex. if we add a new React app that is running on 3001 Port, our caddy file will look like

xyz.com {
  reverse_proxy x.x.x.x:3000
}

reactapp.xyz {
  reverse_proxy x.x.x.x:3001
}

and Restart, it will get a new SSL or this new domain and you can see the site up and running in just seconds.

So if you like the tutorial, make sure you subscribe and add a comment and like it.

ta-da! ✌️

Did you find this article valuable?

Support Rahul Gangotri by becoming a sponsor. Any amount is appreciated!

Β