Setting Up Caddy Web Server on Ubuntu 24.04

By Raman Kumar

Updated on Jul 23, 2024

In this tutorial, we'll discuss about setting up Caddy Web Server on Ubuntu 24.04.

Caddy is a powerful, enterprise-ready, open-source web server with automatic HTTPS by default, making it an excellent choice for both novice and experienced users. It is known for its simplicity, automatic HTTPS configuration, and robust feature set.

Prerequisites

  • A server running Ubuntu 24.04 dedicated server or KVM VPS
  • A user with sudo privileges
  • A domain name pointing to your server's IP address

Step 1: Update System Packages

First, ensure your package list is up-to-date:

sudo apt update
sudo apt upgrade -y

Step 2: Install Caddy

The simplest way to install Caddy is via the official package repository. Start by adding the repository and the GPG key:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
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

Then, install Caddy:

sudo apt update
sudo apt install caddy

Start and enable the Caddy service

sudo systemctl start caddy
sudo systemctl enable caddy

Check that Caddy was installed correctly by verifying its version:

caddy version

Step 3: Configure Caddy

Caddy's configuration file is called Caddyfile and is located at /etc/caddy/Caddyfile. Open this file in your preferred text editor:

sudo nano /etc/caddy/Caddyfile

A basic configuration for serving a static site might look like this:

example.com {
    root * /var/www/html
    file_server
}
Replace example.com with your domain and /var/www/html with the path to your website files.

Set up directory, permissions, and create index file.

Create the directory for your website if it doesn't already exist and set appropriate permissions:

sudo mkdir -p /var/www/html
sudo chown -R www-data:www-data /var/www/html

Change current working directory to /var/www/html and create index.html file.

sudo cd /var/www/html && nano index.html

Add following content:

<html>
    <head>
        <title>This is test file</title>
    </head>
    <body>
        <h1>It works!</h1>
    </body>
</html>

Save and exit

Step 4: Test and Restart Caddy Configuration

Test the configuration to ensure there are no errors:

sudo caddy validate --config /etc/caddy/Caddyfile

After configuring Caddy, restart the service to apply the changes:

sudo systemctl restart caddy

Step 5: Access Your Website

Caddy automatically requests an SSL certificate from Let’s Encrypt for your domain. You can verify that your site is using HTTPS.

Open a web browser and navigate to your domain name. You should see your website served by Caddy.

Additional Configuration

Caddy offers many features such as reverse proxying, load balancing, and more. You can explore further configurations in the official Caddy documentation.

Conclusion

You have successfully installed and configured the Caddy web server on Ubuntu 24.04. This setup ensures that your web server is secure with automatic HTTPS and ready to serve your website.