How to Create Your Own Caddy Webserver in Linux

A photograph of a computer monitor on top of a table with a green backlight.

Caddy is a modern, easy-to-use web server for Linux. It works by streamlining the process of creating site configuration files and SSL certificates. This article will guide you through the process of installing Caddy on an Ubuntu Linux server. Further, it will also show you how to use Caddy to deploy a simple web page as well as an SSL reverse proxy.

Why Use Caddy as a Webserver?

While Nginx and Apache are powerful web server daemons, they can be unwieldy and complex for a new user. Caddy cuts that complexity by providing the “Caddyfile”, a single flat file with a simple syntax that is easy to learn even for beginners.

A terminal showing an example Caddyfile.

Another selling point of Caddy is that it provides HTTPS for your web server right out of the box. This makes it handy for users who find setting up SSL for their website daunting and complicated. As such, Caddy is perfect if you’re looking for a “no-fuss” web server in Linux that’s both easy to maintain and use.

Good to know: learn how you can fix the “404 Not Found” error in Nginx.

Installing Caddy

The first step in installing Caddy in Ubuntu Linux is to make sure that you have the tools to import its repository keys and info:

sudo apt install curl debian-keyring debian-archive-keyring

Fetch the repository signing key for Caddy from the developer’s website:

curl -fsSL 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg

Download and save the Caddy project’s repository file to your machine’s “sources.list.d” directory:

curl -fsSL 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy.list

Reload your system’s apt repositories by running the following:

sudo apt update && sudo apt upgrade

Install the Caddy package for your system using apt install:

sudo apt install caddy

Running Your First Caddy Website

To run your first website, create your site’s root folder in your home directory:

mkdir ~/my-first-website && cd ~/my-first-website

Create an index.html file using your favorite text editor:

nano ./index.html

Paste the following block of code inside your new HTML file:

<!DOCTYPE html>
<html>
<head> <title> Hello World! </title> </head>
<body>
 
<h1> Hello World! </h1>
<p> Hello MakeTechEasier! </p>
 
</body>
</html>

Save your index.html file, then run the following command:

caddy file-server --listen :8080

Confirm that your webserver is working properly by navigating to it using your browser.

A screenshot showing a sample website running on localhost:8080.

Creating a Website Using Caddyfiles

While the CLI tool is great for serving simple webpages, Caddy also provides an easy-to-use “Caddyfile” for more complex setups. To start, create a new Caddyfile under “/etc/caddy” using your favorite text editor:

sudo rm /etc/caddy/Caddyfile && sudo nano /etc/caddy/Caddyfile

Paste the following block of code inside your new Caddyfile:

your-domain.com {
    redir https://www.{host}{uri}
}
 
www.your-domain.com {
    root * /var/www/html
    file_server
}

Note: You can also host a LAN-only website using Caddyfiles by replacing “your-domain.com” with “localhost.”

Copy the index.html file from your home directory to your system’s “/var/www”:

sudo mkdir /var/www/html/
sudo cp ~/my-first-website/index.html /var/www/html

Go to your DNS registrar and make sure that your root and www subdomains has either an A or AAAA record pointing to your machine’s IPv4 and IPv6 addresses.

A screenshot showing two A records pointing to the IP address of the machine.

Note: You don’t need to tweak any DNS settings for LAN-only websites since Caddy will generate a self-signed certificate for it.

Enable the built-in Caddy service on your machine:

sudo systemctl enable --now caddy

Check if your website is working properly by navigating to your domain name.

A screenshot showing the sample website working with an external domain name.

Creating an SSL Reverse Proxy with Caddy

Just like Nginx and Apache, you can also use Caddy as a reverse proxy for an internal service on your machine. To do this, open your system’s Caddyfile:

sudo nano /etc/caddy/Caddyfile

Paste the following block of code inside your Caddyfile:

your-domain.com {
    reverse_proxy 127.0.0.1:LOCAL-PORT
}

Replace “LOCAL-PORT” with the port of your web application. In my case, I will replace mine with 3001 to redirect all incoming traffic to my Uptime Kuma server.

A terminal showing a modified Caddy reverse proxy configuration.

Save your Caddyfile, then reload the Caddy service to apply your new settings:

sudo systemctl reload caddy

Test if your reverse proxy is working properly by navigating to your domain using your web browser.

A screenshot showing an Uptime Kuma instance being proxied through Caddy.

Good to know: learn more about web encryption by creating your own SSL certificate using OpenSSL.

Deploying Multiple Websites and Services using Caddy

You can also use the same Caddyfile to serve both websites and proxies under the same host. This makes it easy to manage multiple different services without cluttering your webserver’s config directory.

To start, go to Caddy’s Download page then type “caddy-dns” on the search bar.

A screenshot highlighting the caddy-dns search box on the Caddy Download page.

Look for the DNS provider that manages your domain name. In my case, I’m using DigitalOcean.

Select your DNS provider, then click “Download” on the page’s upper right corner. This will download a custom Caddy binary with the appropriate module for your DNS provider.

A screenshot highlighting the Download button after selecting the caddy-dns build for DigitalOcean.

Stop the Caddy service using systemctl:

sudo systemctl disable --now caddy.service

Create a backup of the original Caddy binary file, then copy the custom binary to your “/usr/bin/” directory.

cp /usr/bin/caddy ~/caddy.bin.bak
sudo rm /usr/bin/caddy
sudo cp ~/Downloads/caddy_linux_amd64_custom /usr/bin/caddy
sudo chmod +x /usr/bin/caddy

Test if your custom Caddy binary is working properly by checking its version:

caddy --version

Enabling Caddy SSL for Wildcard Domains

Open your system’s Caddyfile using your favorite text editor:

sudo nano /etc/caddy/Caddyfile

Paste the following block of code inside your Caddyfile:

*.your-domain.com {
	tls {
		dns digitalocean API-KEY
    # Replace the value of digitalocean with your DNS provider.
	}
 
	@www host www.your-domain.com
	handle @www {
          root * /var/www/html
          file_server
	}
 
	@subdomain host subdomain.your-domain.com
	handle @uptime {
		reverse_proxy 127.0.0.1:LOCAL-PORT
	}
 
	handle {
		abort
	}
}

Get an API key from your DNS provider. In DigitalOcean, you can get this by going to your DigitalOcean dashboard, then clicking API on the page’s left sidebar.

A screenshot highlighting the API button on the DigitalOcean dashboard.

Click Generate New Token, then provide the details for your API key.

A screenshot showing the token's details for automated SSL wildcard certificates.

Copy your API secret, then paste it on the “API-KEY” variable in your Caddyfile.

Customize the Caddyfile for your specific setup, then save it.

A terminal showing a modified multi-site Caddy configuration file.

Make sure that your domain name has the appropriate A and AAAA records for your domain and subdomain.

Restart your disabled Caddy daemon to apply your new settings:

sudo systemctl enable --now caddy

Test if your “multi-service” setup works properly by opening both domains on a web browser.

A screenshot showing the two services running under SSL with Caddy.

Learning how to deploy simple and secure web services using Caddy is just the first step in exploring the wonderful world of self-hosting in Linux. Dive deeper into this universe by creating your own email alias server with SimpleLogin.

Image credit: Jonathan Ybema via Unsplash and Wikimedia Commons. All alterations and screenshots by Ramces Red.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red
Ramces Red - Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.