How to Install Caddy Web Server on Ubuntu 24.04
Caddy is an open-source web server designed for simplicity and automation. Built with Go, Caddy offers attractive features, including built-in support for HTTPS via Let’s Encrypt, automatic certificate management, and straightforward configuration. Thanks to these features, Caddy is an ideal choice for developers who need a fast and reliable solution for serving websites or applications.
Install Caddy
Here are the steps to install and configure Caddy Web Server on Ubuntu 24.04.
Install the dependencies to add the Caddy GPG key and apt:
1 | sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl |
Adding the Caddy GPG key:
1 | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg |
Adding the Caddy repository:
1 | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list |
Update the repository and install Caddy:
1 2 | sudo apt update sudo apt install caddy |
Displaying the Caddy service status:
1 | sudo systemctl status caddy |
Deploy Static Site
Creating a directory for the document root with the domain name aminlabs.my.id
:
1 | sudo mkdir -p /var/www/aminlabs.my.id |
Creating the index.html
file:
1 | sudo nano /var/www/aminlabs.my.id/index.html |
Dummy content for the index.html
file:
1 2 3 4 5 6 7 8 9 | <!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome. This page is served by the Caddy web server.</h1> </body> </html> |
Navigating to the caddy
directory and opening the Caddyfile
configuration file:
1 2 | cd /etc/caddy sudo nano Caddyfile |
Replace its content, then enter the configuration for the domain aminlabs.my.id
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | www.aminlabs.my.id { tls admin@aminlabs.my.id root * /var/www/aminlabs.my.id file_server log { output file /var/log/caddy/aminlabs.my.id.log } } aminlabs.my.id { redir https://www.aminlabs.my.id{uri} permanent } |
Explanation of the above Caddyfile
configuration lines:
- www.aminlabs.my.id {: defines the domain or subdomain to be served by Caddy, in this case www.aminlabs.my.id.
- tls [email protected]: enables TLS (HTTPS) for the domain www.aminlabs.my.id. The SSL certificate will be automatically managed by Let’s Encrypt with email notifications to [email protected], such as renewal warnings or certificate issues.
- root * /var/www/aminlabs.my.id: specifies the root directory to serve static files.
- file_server: enables the file server feature, allowing Caddy to serve static files from the defined root directory.
- index index.html: specifies the index file that Caddy will look for when accessing a directory.
- log {: configures logging.
- output file /var/log/caddy/aminlabs.my.id.log: logs for the site www.aminlabs.my.id will be saved in the file /var/log/caddy/aminlabs.my.id.log
- aminlabs.my.id {: handles the main domain aminlabs.my.id (without www.).
- redir https://www.aminlabs.my.id{uri} permanent: instructs Caddy to perform a permanent redirect (HTTP status code 301) from the domain aminlabs.my.id to the www.aminlabs.my.id version. {uri} ensures that all paths requested from aminlabs.my.id are forwarded to www.aminlabs.my.id with the same path. For example, if a user visits aminlabs.my.id/about, they will be redirected to www.aminlabs.my.id/about.
Verifying the Caddyfile
configuration for errors:
1 | sudo caddy validate |
If there are warning messages:
1 | WARN Caddyfile input is not formatted; run 'caddy fmt --overwrite' to fix inconsistencies |
run the following command:
1 | sudo caddy fmt --overwrite |
Restart the Caddy service:
1 | sudo systemctl restart caddy |
Finally, test the Caddy web server configuration by browsing the domain name. If successful, the domain should be accessible, displaying the index.html page, and the HTTPS protocol should be active.
If you found this article helpful and would like to support my work, consider making a donation through PayPal. Your support helps me continue creating useful content and tutorials. Thank you!
Donate via PayPal: https://paypal.me/musaamin