How To Install Web Caddy Server On Ubuntu Server 22.04

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

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 offers built-in automated TLS certificate renewals, OSCP stapling, static file serving, reverse proxy, Kubernetes ingress, and more. Caddy can be run as a standalone web server, application server or even in containers.

In this tutorial, I will walk you through the steps to install 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 will need

To get Caddy up and running, you will need an Ubuntu Server 22.04 instance and a user with sudo privileges. With these two elements ready, it is time to proceed with the installation.

How to install Caddy

Connect to your Ubuntu Server instance and add the necessary dependencies with:

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

Once this installation is complete, 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 activate 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 view Caddy’s default home page (Figure A).

Figure A

install-caddya
Image: Jack Wallen/TechRepublic. Caddy homepage was served by Ubuntu Server 22.04.

How to Create Your First Caddy Site

By default, the Caddy document root is /usr/share/caddy but we want to change it to a more standard directory. Before configuring 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 this file, paste the following content:

Hello, TechRepublic!

Hello, TechRepublic, from the Caddy web server!

Save and close the file.

Open the Caddy configuration file with:

sudo nano /etc/caddy/Caddyfile

At the top of this file, you will 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

Redirect your web browser 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 homepage says “Hello” to TechRepublic.

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

Create a new file with:

nano caddy.json

In this 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 download should happen instantly. Once 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 message Welcome, TechRepublic! printed message (Figure C).

Figure C

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

Congratulations, you now have the light and ultra-fast Caddy web server up and running. We will come back to this later to discover other ways to exploit this platform.

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