Caddy Web Server
Caddy offers simple, easy-to-manage configuration with automatic HTTPS, making secure hosting straightforward. It’s lightweight and well-suited for home servers, helping me focus on learning without complex setup.
1. Installing and Configuring Caddy on Ubuntu Server
1.1. Install Caddy
First, you’ll need to install Caddy. Follow the official instructions for your Linux distribution. For Ubuntu, you can use these commands:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /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
1.2. Basic Caddyfile Setup
Now, let’s configure Caddy to serve your Astro site and enable automatic HTTPS. Edit the Caddyfile located at /etc/caddy/Caddyfile
. Make sure to replace pablolebed.dev
with your actual domain and /home/admin/portfolio/dist
with the correct path to your Astro project’s dist
directory.
# Redirect HTTP to HTTPS for your domain
http://pablolebed.dev, http://www.pablolebed.dev {
redir https://pablolebed.dev{uri}
bind 0.0.0.0
}
# Serve your Astro site securely over HTTPS
https://pablolebed.dev, https://www.pablolebed.dev {
root * /home/pablo/portfolio/dist
file_server
encode gzip
tls contact@pablolebed.dev # Important: Use your actual email for TLS certificate registration
bind 0.0.0.0
}
1.3. Reload Caddy
After you’ve made changes to your Caddyfile
, you’ll need to reload the Caddy service for them to take effect:
sudo systemctl reload caddy
2. Network Configuration
2.1. Firewall Setup
To allow Caddy to serve your website, you need to open ports 80 (HTTP) and 443 (HTTPS) on your server’s firewall. If you’re using ufw
, here’s how:
sudo ufw allow 80
sudo ufw allow 443
2.2. Router Port Forwarding
If your server is behind a router (common in home setups), you must configure your router to forward incoming traffic on ports 80 and 443 to your server’s internal IP address. Consult your router’s manual for specific instructions on how to do this.
3. DNS Setup
You need to point your domain’s A record (e.g., pablolebed.dev
and www.pablolebed.dev
) to your server’s public IP address. For instance, if your server’s IP is 2.50.34.108
, your DNS configuration should reflect this.
4. TLS Certificate and HTTPS
One of Caddy’s best features is its automatic handling of TLS certificates. It obtains and renews certificates from Let’s Encrypt automatically, ensuring your site is always served securely over HTTPS. This process works smoothly when:
- Your domain’s A record correctly points to your server’s public IP address.
- Ports 80 (HTTP) and 443 (HTTPS) on your server are publicly accessible.
If you encounter any issues with certificates or connections, you can check Caddy’s logs for insights:
sudo journalctl -u caddy -f
5. Potential problems
Caddy Could Not Serve the Site Files
-
Symptom:
When trying to access the website, the browser showed connection refused or empty responses despite Caddy running. -
Cause:
Caddy was configured to serve the website from a directory (root * /home/admin/portfolio/dist
) that either:- Did not exist yet (no built files),
- Or had incorrect permissions preventing Caddy from reading the files,
- Or the Astro build (
dist
folder) was missing or outdated on the server.
Solution:
-
Build the Astro project locally to generate the
dist/
folder with static files. -
Push the built files (including
dist/
) to GitHub so the server can pull them. -
On the server:
- Pull the latest changes from GitHub.
- Run
npm install
andnpm run build
to regenerate thedist/
folder if needed. - Verify that the
dist/
folder exists and contains the static files. - Check and fix permissions so Caddy can read the files (e.g., owned by the Caddy user or readable by others).
-
Reload Caddy to apply the configuration.
-
Test the site again to confirm Caddy can serve the files properly.
This resolved the issue, enabling Caddy to serve the static Astro site successfully.