How to Install Caddy Web Server on Ubuntu Server 22.04
If you are looking for a flexible, lightweight web host for static websites, web applications or containers, Caddy might be what you want. Jack Wallen shows you how to install this lightning-fast web host.
Caddy is a powerful open source web server, written in Go, that can be used to host web applications in production environments. Caddy has built-in automatic TLS certificate renewal, OSCP pinning, static file serving, reverse proxies, Kubernetes infiltration, and more. Caddy can be run as a standalone web server, an application server or even in containers.
In this tutorial, I will walk you through the steps of installing Caddy on Ubuntu Server 22.04 and then how to create a simple, static website.
SEE: Over 40 Linux and Open Source Terms you need to know (TechRepublic Premium)
What you need
To get Caddy bay up and running, you’ll need an instance of Ubuntu Server 22.04 and a user with sudo privileges. With the two in place, it’s time to install.
How to install Caddy
Login to your Ubuntu Server instance and add the required dependencies with:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https -y
Once the 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
apt update:
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 can now point your browser to http:// SERVER (Where SERVER
is your host’s IP address or domain) and see the default Caddy welcome page (Picture A).
Picture A
How to Create Your First Caddy Website
Also, the Caddy document root directory is /usr/share/caddy but we want to change it to a more standard directory. Before we configure Caddy, let’s create a new document root with:
sudo mkdir -p /var/www/html
Next, let’s create a basic static website 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 config file with:
sudo nano /etc/caddy/Caddyfile
Near the top of that file, you’ll find the following:
: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 config with:
sudo systemctl reload caddy
Point your web browser back to http:// SERVER (where SERVER
is the IP address or domain of the host) and you will see our new welcome message (Figure BUT)
Figure BUT
Caddy has another cool trick for static sites. Let’s create another page that will print the message Hello, TechRepublic 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"],
"roads": [
{
"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 will take place immediately. Once done, point your browser to http://SERVER:2015 (Where SERVER
is your host’s IP address or domain) and you’ll see Welcome, TechRepublic! message is printed (SIZE).
SIZE
Congratulations, you now have the lightning-fast, lightweight Caddy web server up and running. We’ll come back to this later to learn more ways to leverage this platform.
Subscribe to TechRepublic’s How to make technology work on YouTube for all the latest tech advice for business professionals from Jack Wallen.