Installing and Configuring Caddy Server

From Server rental store
Revision as of 15:57, 12 April 2026 by Admin (talk | contribs) (New server guide)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Installing and Configuring Caddy Server

Caddy is a modern, open-source web server that emphasizes ease of use and automatic HTTPS. It simplifies common web server tasks like TLS certificate management, making it an excellent choice for both beginners and experienced administrators. This guide will walk you through installing and configuring Caddy on a Linux system.

Prerequisites

Before you begin, ensure you have the following:

  • A Linux server with root or sudo privileges. Dedicated servers from PowerVPS provide the necessary root access for full control.
  • Basic understanding of the Linux command line.
  • Internet connectivity on your server.
  • A domain name (e.g., `yourdomain.com`) pointed to your server's public IP address.

Installation

Caddy provides official packages for various Linux distributions, making installation straightforward.

Debian/Ubuntu

On Debian-based systems, you can add the Caddy repository and install it using `apt`:

sudo apt update
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

Fedora/CentOS/RHEL

For RPM-based systems, use `dnf` or `yum`:

sudo dnf install -y 'dnf-command(copr)'
sudo dnf copr enable @caddy/caddy
sudo dnf install caddy

Verifying Installation

After installation, you can check the Caddy version to confirm it was installed correctly:

caddy version

Configuration

Caddy's configuration is managed via a file named `Caddyfile`. By default, this file is located at `/etc/caddy/Caddyfile`.

Basic Caddyfile

A minimal `Caddyfile` to serve a static website looks like this:

yourdomain.com {
    root * /var/www/yourdomain.com
    file_server
}
  • `yourdomain.com`: This is the domain name Caddy will listen for.
  • `root * /var/www/yourdomain.com`: Specifies the document root for all requests.
  • `file_server`: Enables static file serving.

Creating a Website Directory

You need to create the directory specified in your `Caddyfile` and place your website's files there.

sudo mkdir -p /var/www/yourdomain.com
sudo chown caddy:caddy /var/www/yourdomain.com # Or the user Caddy runs as
echo "<h1>Hello from Caddy!</h1>" | sudo tee /var/www/yourdomain.com/index.html

Loading the Configuration

After creating or modifying your `Caddyfile`, you need to reload Caddy for the changes to take effect.

sudo systemctl reload caddy

Starting and Stopping Caddy

You can manage the Caddy service using `systemctl`:

  • Start Caddy:
    sudo systemctl start caddy
    
  • Stop Caddy:
    sudo systemctl stop caddy
    
  • Enable Caddy to start on boot:
    sudo systemctl enable caddy
    
  • Check Caddy's status:
    sudo systemctl status caddy
    

Advanced Configuration =

Reverse Proxy

Caddy excels as a reverse proxy, forwarding requests to backend applications (e.g., Node.js, Python/Flask, Go applications).

yourdomain.com {
    reverse_proxy localhost:3000
}

This configuration directs all traffic for `yourdomain.com` to a backend application running on `localhost:3000`.

Multiple Websites

You can host multiple websites on a single Caddy instance by adding more blocks to your `Caddyfile`:

site1.yourdomain.com {
    root * /var/www/site1
    file_server
}

site2.yourdomain.com {
    reverse_proxy localhost:8080
}

Automatic HTTPS

One of Caddy's killer features is automatic HTTPS. When Caddy starts and is configured with a domain name, it automatically obtains and renews TLS certificates from Let's Encrypt (or ZeroSSL). You don't need to do anything special for this to work, as long as your domain's DNS is correctly configured and ports 80 and 443 are accessible from the internet.

Caddy API

Caddy exposes an API that allows for dynamic configuration changes without restarting the server. This is useful for advanced setups. You can enable the API in your `Caddyfile`:

:2019 # API endpoint
yourdomain.com {
    # ... your site configuration ...
}

You can then interact with the API using `curl` or Caddy's official client.

Troubleshooting

  • Caddy not starting: Check the Caddy service status (`sudo systemctl status caddy`) for error messages. Common issues include syntax errors in the `Caddyfile` or port conflicts.
  • HTTPS not working: Ensure your domain's DNS records are correctly pointing to your server's IP address. Also, verify that ports 80 and 443 are open in your firewall and accessible from the internet. Caddy needs to communicate with Let's Encrypt servers.
  • "Too many redirects" error: This often indicates a misconfiguration in your `Caddyfile`, especially when dealing with reverse proxies or redirects. Check your `Caddyfile` for any conflicting rules.
  • Permissions issues: Ensure the user Caddy runs as (often `caddy`) has read permissions for your website's files and directories.

Related Articles