DEV Community

Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

Serve Static Files with Caddy: Minimal Setup Guide

Hi there! I'm Maneshwar. Right now, I’m building LiveAPI, a first-of-its-kind tool that helps you automatically index API endpoints across all your repositories. LiveAPI makes it easier to discover, understand, and interact with APIs in large infrastructures.


Caddy is a lightweight, modern web server with automatic HTTPS, easy config, and a powerful plugin ecosystem. Here's how to quickly serve static files with it.

1. Install Caddy

On Debian/Ubuntu:

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
Enter fullscreen mode Exit fullscreen mode

2. Create Your Static Site Directory

mkdir -p /var/www/mysite
cd /var/www/mysite
echo "Hello from Caddy!" > index.html
Enter fullscreen mode Exit fullscreen mode

You can place any static files here (HTML, CSS, JS, images, etc.).

3. Write the Caddyfile

Create a file named Caddyfile (default location: /etc/caddy/Caddyfile)

:80 {
    root * /var/www/mysite
    file_server
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • :80 → Serve on HTTP port 80 (use yourdomain.com for HTTPS).
  • root * /var/www/mysite → Set the root directory.
  • file_server → Enable static file serving.

4. Reload Caddy

After saving the Caddyfile:

sudo systemctl reload caddy
Enter fullscreen mode Exit fullscreen mode

Or if running manually:

caddy run --config /etc/caddy/Caddyfile
Enter fullscreen mode Exit fullscreen mode

5. Test It

Visit:

http://localhost/
Enter fullscreen mode Exit fullscreen mode

You should see: Hello from Caddy!

Bonus: Enable HTTPS with a Domain

Update your Caddyfile:

yourdomain.com {
    root * /var/www/mysite
    file_server
}
Enter fullscreen mode Exit fullscreen mode

Caddy will automatically fetch and renew the SSL cert via Let's Encrypt.

Make sure port 80 and 443 are open, and DNS is correctly set.

Optional: Directory Browsing

:80 {
    root * /var/www/mysite
    file_server browse
}
Enter fullscreen mode Exit fullscreen mode

Permissions

Make sure the Caddy user can read the static files:

sudo chown -R www-data:www-data /var/www/mysite
Enter fullscreen mode Exit fullscreen mode

Run Caddy Without Systemd (Manual Dev Mode)

caddy file-server --root /var/www/mysite --listen :8080
Enter fullscreen mode Exit fullscreen mode

That's it. Static hosting done right with minimal config.
Use it to serve SPAs, HTML dumps, or just quick previews.


LiveAPI helps you get all your backend APIs documented in a few minutes.

With LiveAPI, you can generate interactive API docs that allow users to search and execute endpoints directly from the browser.

LiveAPI Demo

If you're tired of updating Swagger manually or syncing Postman collections, give it a shot.

Top comments (0)