If you’re looking for a lightweight, flexible web server for static sites, web apps or containers, Caddy might be just what you want. Jack Wallen shows you how to install this lightning-fast web server.

caddy-ubuntu-installtutorial
Image: standret/Adobe Stock

Caddy is a powerful open-source web server, written in Go, that can be used to host web applications in a production environment. Caddy features built-in automated TLS certificate renewals, OSCP stapling, static file serving, reverse proxy, Kubernetes ingress and much more. Caddy can be run as a stand-alone web server, an app server or even within containers.

In this tutorial, I’m going to walk you through the steps of installing Caddy on Ubuntu Server 22.04 and then how to create a simple, static site.

SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)

What you’ll need

To get Caddy up and running, you’ll need an instance of Ubuntu Server 22.04 and a user with sudo privileges. With those two things at the ready, it’s time to install.

How to install Caddy

Log into your instance of Ubuntu Server and add the necessary dependencies with:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https -y

Once that installation completes, add the official Caddy GPG key with:

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

Create the repository file with the command:

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

Update apt:

sudo apt-get update

Finally, install Caddy with the command:

sudo apt-get install caddy -y

Start and enable the Caddy service with:

sudo systemctl enable --now caddy

You should now be able to point a browser to http://SERVER (Where SERVER is the IP address or domain of your hosting server) and see the default Caddy welcome page (Figure A).

Figure A

install-caddya
Image: Jack Wallen/TechRepublic. The Caddy welcome page was served up by Ubuntu Server 22.04.

How to create your first Caddy site

Out of the box, the Caddy document root is /usr/share/caddy but we want to change it to a more standard directory. Before we configure Caddy, create the new document root with:

sudo mkdir -p /var/www/html

Next, let’s create a basic static site file with:

sudo nano /var/ww/html/index.hml

In that file, paste the following contents:

<!DOCTYPE html>

<html>

<head>

<title>Hello, TechRepublic!</title>

</head>

<body>

<h1 style="font-family: sans-serif">Hello, TechRepublic, from the Caddy web server!</h1>

</body>

</html>

Save and close the file.

Open the Caddy configuration file with:

sudo nano /etc/caddy/Caddyfile

Near the top of that file, you’ll find the following section:

:80 {

# Set this path to your site's directory.

root * /usr/share/caddy

Change that to:

:80 {

# Set this path to your site's directory.

root * /var/www/html/

Save and close the file.

Reload the Caddy configuration with:

sudo systemctl reload caddy

Point your web browser back to http://SERVER (where SERVER is the IP address or domain of the hosting server) and you should see our new welcome message (Figure B)

Figure B

install-caddyb
Image: Jack Wallen/TechRepublic. Our new Caddy welcome page says, “Hello” to TechRepublic.

Caddy has another fun trick up its sleeve for static websites. Let’s create yet another page that will print out the Hello, TechRepublic message and then, using curl, upload it to the Caddy server.

Create a new file with:

nano caddy.json

In that file, paste the following:

{

"apps": {

"http": {

"servers": {

"example": {

"listen": [":2015"],

"routes": [

{

"handle": [{

"handler": "static_response",

"body": "Hello, TechRepublic!"

}]

}

]

}

}

}

}

}

Save and close the file.

Upload our caddy.json file to the Caddy server with the command:

curl localhost:2019/load -X POST -H "Content-Type: application/json" -d @caddy.json

The upload should happen instantaneously. Once it’s done, point your browser to http://SERVER:2015 (Where SERVER is the IP address or domain of your hosting server) and you should see the Welcome, TechRepublic! message printed (Figure C).

Figure C

install-caddyc
Image: Jack Wallen/TechRepublic. Our new static site was pushed to the server via curl.

Congratulations, you now have the lightweight, lightning-fast Caddy web server up and running. We’ll return to this later to learn more ways to leverage this platform.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.