Using Caddy as a Lightweight Web Server with Automatic HTTPS
Introduction
Caddy is a modern web server designed for simplicity and ease of use. It features automatic HTTPS by default, making it an excellent choice for users who want a lightweight yet powerful web server. This guide will walk you through installing and configuring Caddy to serve a website with automatic HTTPS.
Step 1: Installing Caddy
-
Update your system’s package list:
sudo apt update && sudo apt upgrade -y
-
Install Caddy using the official package repository:
sudo apt install -y caddy
-
Verify that Caddy is installed correctly:
caddy version
Step 2: Configuring Your First Caddy Website
-
Create a new Caddyfile:
sudo nano /etc/caddy/Caddyfile
-
Add the following configuration:
yourdomain.com {
root * /var/www/html
file_server
}
-
Save and exit the file.
-
Ensure the web root directory exists:
sudo mkdir -p /var/www/html
-
Restart Caddy to apply the changes:
sudo systemctl restart caddy
Step 3: Enabling Automatic HTTPS
-
Ensure your domain’s DNS records point to your server’s IP address.
-
Caddy will automatically obtain and renew SSL certificates from Let’s Encrypt.
-
Check the status of your HTTPS setup with:
sudo journalctl -u caddy --no-pager | grep -i "certificate"
Step 4: Serving a Custom Website
-
Create an HTML file to serve:
echo 'Welcome to Caddy.' | sudo tee /var/www/html/index.html
-
Reload Caddy to apply changes:
sudo systemctl reload caddy
-
Open your browser and visit
https://yourdomain.com
to see the webpage.
Step 5: Running Caddy as a Reverse Proxy
-
Modify your Caddyfile to proxy requests to an internal service:
yourdomain.com {
reverse_proxy 127.0.0.1:8080
}
-
Save the changes and reload Caddy:
sudo systemctl reload caddy
-
Now, Caddy will forward incoming requests to your backend service on port 8080.
Conclusion
Caddy is an excellent choice for users looking for an easy-to-configure web server with built-in HTTPS support. Its simplicity and automation make it ideal for both beginners and experienced users.