Skip to content

How to Set Up Caddy on a Linux VPS – Website and Reverse Proxy with Automatic HTTPS

Set up Caddy on your Linux VPS: use it as a One-Click App for a static website with HTTPS, or install it manually with Docker Compose and your own Caddyfile for a reverse proxy, www redirects, and password protection.

30 November 202612 min read0 Views0 helpful

Introduction

Caddy is an open-source web server and reverse proxy written in Go. Caddy is best known for automatic HTTPS: when a domain points to your server, Caddy obtains a certificate from Let's Encrypt and renews it in time. Its configuration is stored in a short text file called the Caddyfile. You can describe a static website or a reverse proxy for an app in just a few lines.

In this guide, we’ll show you two ways to set it up: the easy way, as a One-Click App, which gets your static website online with HTTPS in just a few minutes, and a manual installation with Docker Compose, which lets you use Caddy as a reverse proxy with your own Caddyfile. At the end, you’ll find some typical Caddyfile examples.

When to Use Caddy on a VPS

Caddy is a good fit for a VPS if:

  • You want to publish a static website, such as a business card site, landing page, or documentation generated with Hugo, Jekyll, Astro, or another generator.
  • You want HTTPS without the manual work. Caddy takes care of certificates and renewals itself, so you don’t need an additional tool like Certbot.
  • You want to make multiple apps on one server available under their own subdomains. One reverse_proxy line per app is all you need.
  • You find nginx or Apache configuration too extensive. A Caddyfile is much shorter and reads almost like plain text.

Caddy is less suitable if you want to run PHP applications like WordPress. Caddy can only pass PHP requests to a separate PHP-FPM service, which you would have to set up yourself. There are more suitable One-Click Apps for this; see Set Up a Website with ZAP Webspace and Set Up WordPress.

One-Click App or Your Own Caddy Installation?

The One-Click App and a custom installation differ more with Caddy than with most apps. Your VPS’s One-Click Apps include their own proxy. It uses ports 80 and 443, obtains an HTTPS certificate for each app, and forwards requests to the app. Caddy therefore runs behind this proxy and only serves your files.

Caddy as a One-Click AppCustom Installation with Docker Compose
Intended forStatic websiteStatic websites and reverse proxy for your own apps
HTTPS certificateObtained by the One-Click Apps proxyObtained by Caddy itself
Custom CaddyfileNo, fixed default configurationYes, freely customizable
Ports 80 and 443Used by the One-Click Apps proxyUsed by Caddy
SetupA few clicks in the dashboardConsole, Docker, Caddyfile

If you only need Caddy to make other One-Click Apps available over HTTPS, you don’t have to do anything: the One-Click Apps proxy already handles that for each app.

Easy Option: Caddy as a One-Click App

The quickest way to get a website running with Caddy is through your VPS’s One-Click Apps. ZapX sets up the domain, certificate, and redirect from HTTP to HTTPS for you. All you need to do is upload your files.

Requirements

  • A Linux VPS (Debian or Ubuntu) with One-Click Apps set up. See Set Up One-Click Apps for instructions.
  • At least 256 MB of free RAM (512 MB recommended) and 1 to 2 GB of storage, plus space for your website. Caddy itself uses very few resources.

Install

  1. Open your VPS in the ZapX dashboard and go to the One-Click Apps tab.
  2. Search for Caddy in the App catalog and click Install app.
  3. Leave Use a custom subdomain or zap.cloud enabled and choose an address, such as a subdomain under zap.cloud or your own domain. Without a domain, the website is only accessible over HTTP.
  4. No other information is required. Caddy has no app configuration or login credentials.
  5. Click Queue installation.

After one or two minutes, the app will appear as “Online” in your list of installed apps.

First Visit

Open the website using the Access link on the app card. You’ll see the “Caddy works!” welcome page. The address is immediately accessible over HTTPS, and the proxy redirects HTTP requests to HTTPS.

Info: The steps on the welcome page don’t apply to the One-Click App

The welcome page describes an installation as a system package (files in /var/www/html, Caddyfile at /etc/caddy, systemctl reload caddy). The One-Click App works differently: the domain and certificate are already set up, and your files belong in the folder described in the next section.

Upload Your Own Website

Caddy serves all files from the /usr/share/caddy folder in the container. This folder is stored as a Docker volume on your VPS. Find the exact path over SSH by running:

Code
docker volume ls | grep caddy-site

The name will look something like this: APP_ID_caddy-site. The files are then located at:

Code
/var/lib/docker/volumes/APP_ID_caddy-site/_data

To publish your website:

  1. Connect to your VPS as root over SFTP and open the folder above.
  2. Replace the provided index.html with your own homepage, then upload your other files and folders.
  3. Reload the website in your browser. You don’t need to restart the app—Caddy serves new files immediately.

Caddy looks for an index.html in each folder. So a file at blog/index.html is available at https://your-domain/blog/. If a file is missing, Caddy returns a 404 error.

Add Your Own Domain and www

Change the app’s primary address in its settings (gear icon), under App actions, by selecting Change next to Custom subdomain. To make your website available under www or a second domain as well, enter it under Additional domains and click Add.

If the domain is registered with ZAP-Hosting or in your connected Cloudflare account, ZapX will create the DNS record for you. If you use another DNS provider, create an A record pointing to the IP address shown in the dashboard. The certificate for the new domain will then be obtained automatically.

Limitations of the One-Click App

Warning: You can’t use your own Caddyfile with the One-Click App

The One-Click App always starts Caddy with the provided configuration at /etc/caddy/Caddyfile: a static website served from /usr/share/caddy. Caddy doesn’t read a Caddyfile that you put in the caddy-config volume (at /config in the container). Caddy only stores its own state there. For redirects, a reverse proxy, password protection, or custom headers, use the manual installation described below.

The One-Click App also doesn’t include PHP, databases, or server-side logic. For a website with PHP, use ZAP Webspace.

Updates and backups

Update Caddy in the app’s settings (gear icon), under App update. Click Check for updates, then install a new version using Apply update. If the page says the app is up to date, there isn’t a newer version in the app catalog.

Your website files are stored in the caddy-site volume and are retained when you update the app or select Reinstall app. Still, back them up regularly—for example, by using SFTP to copy them to your computer or by using Backups in your VPS dashboard. When you uninstall the app, you can also delete its volumes. If you do, your website will be deleted too.

Manual Installation

The rest of this guide describes a manual installation of Caddy with Docker Compose on your VPS. This option is right for you if you need your own Caddyfile—for example, to use Caddy as a reverse proxy for your own apps—and you aren’t using One-Click Apps on the server.

Warning: Don’t use this on a VPS with One-Click Apps set up

If One-Click Apps are set up on your VPS, their proxy is already using ports 80 and 443. Your own Caddy installation won’t start and may report an error such as “port is already allocated” or “address already in use.” In this case, use the One-Click App or a separate VPS for Caddy.

Requirements

  • A Linux VPS with Debian or Ubuntu and SSH access; see First Access via SSH.
  • Docker and Docker Compose (Compose v2); see Install Docker. Check the installation with docker --version and docker compose version.
  • A domain or subdomain whose A record (and AAAA record, if present) points to your VPS’s IP address, for example example.com.
  • Ports 80 and 443 are available. If nginx or Apache is already running, check with ss -ltnp | grep -E ':(80|443) '.

Caddy needs few resources. You’ll only need more RAM and CPU if you have many simultaneous visitors or large files.

HardwareMinimumRecommended
CPU1 vCPU core1 vCPU core
RAM256 MB512 MB
Storage1 GB2 GB plus space for your website

Prepare the Firewall

Caddy needs port 80 for Let's Encrypt certificate validation and redirects to HTTPS, port 443 for HTTPS, and UDP port 443 for HTTP/3. With ufw, allow only SSH and these ports:

Code
ufw allow OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 443/udp
ufw enable
Info: Docker bypasses ufw

Ports published by Docker with ports: are accessible from outside even if ufw blocks them. This is intentional for Caddy. For your other apps, however, make sure to bind them only to 127.0.0.1 or don’t publish any ports if Caddy is in front of them. See Managing the Firewall on Linux for more information.

Install with Docker Compose

Create a directory for Caddy, with one folder for the configuration and another for your website:

Code
mkdir -p /opt/caddy/conf /opt/caddy/site
cd /opt/caddy
nano compose.yaml

Add this content:

Code
services:
  caddy:
    image: caddy:2.11
    restart: unless-stopped
    cap_add:
      - NET_ADMIN
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./conf:/etc/caddy
      - ./site:/srv
      - caddy_data:/data
      - caddy_config:/config
    extra_hosts:
      - "host.docker.internal:host-gateway"

networks:
  default:
    name: caddy

volumes:
  caddy_data:
  caddy_config:

The most important lines:

  • image: caddy:2.11 is the official image, pinned to version 2.11. You’ll automatically get bug fixes for this version whenever you update.
  • ports publishes HTTP, HTTPS, and HTTP/3. cap_add: NET_ADMIN allows Caddy to use larger buffers for HTTP/3.
  • ./conf:/etc/caddy mounts the entire configuration folder, not just the file. This ensures Caddy reliably picks up Caddyfile changes when reloading.
  • ./site:/srv is the folder for your website.
  • caddy_data contains certificates and keys. This volume must never be lost; otherwise, Caddy will have to obtain all certificates again and may quickly hit Let's Encrypt limits.
  • extra_hosts makes the VPS itself accessible from the container as host.docker.internal, while networks gives the Docker network the fixed name caddy. You need both for a reverse proxy.

Now create the Caddyfile:

Code
nano /opt/caddy/conf/Caddyfile

Enter your domain. This block serves the files from /srv and compresses them:

Code
example.com {
	root * /srv
	encode
	file_server
}

Create an initial homepage and start Caddy:

Code
echo '<h1>Hello from Caddy</h1>' > /opt/caddy/site/index.html
docker compose up -d
docker compose logs -f caddy

The logs show Caddy obtaining the certificate for your domain. Once you see certificate obtained successfully, everything is ready. Exit the log view with Strg+C.

First Visit

Open https://example.com in your browser. You’ll see your homepage, and the lock icon indicates a valid certificate. Caddy automatically redirects requests from http:// to HTTPS.

If the certificate can’t be obtained, it’s almost always a DNS or firewall issue: check that the domain points to your VPS’s IP address, that ports 80 and 443 are open, and that an AAAA record isn’t pointing to an incorrect IPv6 address.

HTTPS and Reverse Proxy with Caddy

A custom reverse proxy with nginx requires two tools: nginx for forwarding, as described in Set Up a Reverse Proxy with nginx, and Certbot for the certificate, as described in Create an SSL Certificate (Let's Encrypt). Caddy handles both on its own: as soon as you add a domain name to the Caddyfile, Caddy obtains and renews the certificate and redirects HTTP to HTTPS. There’s no need for a separate TLS block or cron job.

So, use either Caddy or nginx with Certbot on a server, not both on ports 80 and 443 at the same time. You’ll find forwarding examples below under “Caddyfile: Typical Examples.”

After each change to the Caddyfile, reload the configuration without interruption:

Code
cd /opt/caddy
docker compose exec -w /etc/caddy caddy caddy reload

If the Caddyfile contains an error, Caddy keeps the old configuration and shows the line with the error. You can check the file first with docker compose exec caddy caddy validate --config /etc/caddy/Caddyfile.

Find more tips on securing your server in Linux Security Tips.

Back Up Your Data

Back up the Caddyfile, your website, and the volume containing the certificates:

Code
cd /opt/caddy
tar czf /root/caddy-files-$(date +%F).tar.gz conf site compose.yaml
docker run --rm -v caddy_caddy_data:/data -v /root:/backup alpine tar czf /backup/caddy-data-$(date +%F).tar.gz -C /data .

The volume name caddy_caddy_data is based on the folder name caddy. You can find the exact name with docker volume ls. Don’t store the backup only on the VPS itself.

Update Caddy

With the caddy:2.11 tag, you’ll get bug fixes for this version by running:

Code
cd /opt/caddy
docker compose pull
docker compose up -d

To use a new major version, enter the new tag in compose.yaml—for example, caddy:2.12 once it’s available—and run the same commands. Read the release notes first. Check the installed version with docker compose exec caddy caddy version.

Caddyfile: Typical Examples

The following blocks are for the manual installation. Replace the domains with your own, then reload Caddy as described above.

Static Website with www Redirect

This Caddyfile serves the website at example.com and permanently redirects www.example.com there. Caddy obtains a certificate for both names:

Code
example.com {
	root * /srv
	encode
	file_server
}

www.example.com {
	redir https://example.com{uri} permanent
}

Reverse Proxy for an App

If an app also runs in Docker, connect it to the caddy network. Add this to the app’s compose.yaml:

Code
networks:
  default:
    name: caddy
    external: true

Restart the app with docker compose up -d. Caddy can then reach it using its service name and container port, so you no longer need to expose the app’s port externally. For an app with the service name myapp on port 3000:

Code
app.example.com {
	reverse_proxy myapp:3000
}
Info: App Running Directly on the VPS

If the app runs without Docker or uses a published port, you can reach it with reverse_proxy host.docker.internal:8080. This only works if the app isn’t listening exclusively on 127.0.0.1, because that’s a different address from the container’s perspective. In this case, block the app’s port from external access in the firewall.

Caddy sets the X-Forwarded-For, X-Forwarded-Proto, and X-Forwarded-Host headers automatically. Many apps also need to be configured to run behind a proxy and to use their public address. See the documentation for the relevant app.

Protect an Area with a Password

If an app doesn’t have its own login, you can protect it with Basic Auth. First, generate a password hash. Caddy never stores the password itself:

Code
cd /opt/caddy
docker compose exec caddy caddy hash-password

Enter the password twice and copy the output, which starts with $2a$. Add it to the Caddyfile along with a username:

Code
intern.example.com {
	basic_auth {
		admin HASH_AUS_DER_AUSGABE
	}
	reverse_proxy myapp:3000
}

Limitations

  • No PHP out of the box: Caddy serves files and forwards requests. PHP pages need an additional PHP-FPM service, which Caddy connects to using php_fastcgi.
  • Not a replacement for a firewall: Caddy only protects the services you publish through it. Ports published directly by other containers remain accessible.
  • Certificates require accessible domains: Caddy can’t obtain a Let's Encrypt certificate for addresses that don’t publicly point to your VPS.
  • One web server per port: Caddy and another web server, such as nginx or Apache, can’t both use ports 80 and 443 at the same time.

Conclusion

You’ve set up Caddy on your VPS: as a One-Click App for a static website with automatic HTTPS, or manually with your own Caddyfile, which lets you serve websites, publish apps through a reverse proxy, and protect areas with a password.

Want to get a website online without using the console or configuring anything? Choose the easy option: the One-Click App, as described above under “Easy Option: Caddy as a One-Click App.”

Free tools

Related tools for servers and developers

View all

FAQ

Frequently asked questions

For a static website, the One-Click App is the quickest option: Caddy runs under your domain within minutes, ZapX sets up the certificate and HTTPS redirect, and you can install updates in the dashboard. However, you can’t use your own Caddyfile. You need a manual installation with Docker Compose if you want to use Caddy as a reverse proxy for your own apps, with redirects, or with password protection. It only works on a server without One-Click Apps set up, because their proxy uses ports 80 and 443.
No. With the One-Click App, the One-Click Apps proxy obtains the certificate for your domain and redirects HTTP to HTTPS. Caddy receives requests from that proxy and only serves your files. This doesn’t change anything for you: the website is immediately available over HTTPS, including under additional domains you add in App Actions.
Upload them to the caddy-site Docker volume on your VPS. Find the volume name with docker volume ls | grep caddy-site; the files are then located at /var/lib/docker/volumes/APP_ID_caddy-site/_data. Connect as root over SFTP, replace the provided index.html with your homepage, and upload your other files. You don’t need to restart the app.

Feedback

Was this article helpful?

Your feedback helps us prioritize and improve content.

0 of 0 people found this article helpful.

Personal support

Need more help than the guide provides?

Our ZAP-Hosting support team knows your product — briefly describe your issue and we'll help you personally.

Zappy knows this guide and answers you right in the chat.