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:

Adding the Caddy GPG key:

Adding the Caddy repository:

Update the repository and install Caddy:

Displaying the Caddy service status:

Deploy Static Site

Creating a directory for the document root with the domain name aminlabs.my.id:

Creating the index.html file:

Dummy content for the index.html file:

Navigating to the caddy directory and opening the Caddyfile configuration file:

Replace its content, then enter the configuration for the domain aminlabs.my.id:

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:

If there are warning messages:

run the following command:

Restart the Caddy service:

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

Leave a Reply

Your email address will not be published. Required fields are marked *