How to Install Caddy Web Server on AlmaLinux 9

By Raman Kumar

Updated on Jul 24, 2024

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

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 AlmaLinux 9 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 dnf update

Step 2: Install Caddy

This package comes with both of Caddy's systemd service unit files, but does not enable them by default.  We need to enable to and install Caddy:

sudo dnf install 'dnf-command(copr)'
sudo dnf copr enable @caddy/caddy

Note: It will ask if you want to enable it . Type y an enter.

Then, install Caddy:

sudo dnf install caddy -y

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 Firewall

We need to add HTTP and HTTPS ports in firewall.

sudo firewall-cmd --add-port=80/tcp
sudo firewall-cmd --add-port=443/tcp
firewall-cmd --reload

Step 4: 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 caddy:caddy /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 5: 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 6: 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 AlmaLinux 9. This setup ensures that your web server is secure with automatic HTTPS and ready to serve your website.