Installing and Configuring Caddy Server
= 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
}
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 "Hello from Caddy
" 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`:
sudo systemctl start caddy
sudo systemctl stop caddy
sudo systemctl enable caddy
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
Related Articles
Category:Web Server Setup Category:Caddy Category:Server Administration