RHEL BasedRocky Linux

How To Install Caddy on Rocky Linux 9

Install Caddy on Rocky Linux 9

In this tutorial, we will show you how to install Caddy on Rocky Linux 9. Caddy is a lightweight, high-performance web server written in Go. It stands out from traditional web servers like Apache and Nginx due to its user-friendly configuration and automatic HTTPS capabilities. For Rocky Linux 9 users, Caddy offers a compelling option for hosting websites and web applications with minimal hassle and maximum security.

Key Features of Caddy

  • Automatic HTTPS: Caddy provisions and renews SSL/TLS certificates automatically using Let’s Encrypt.
  • Simple Configuration: The Caddyfile syntax is intuitive and easy to understand, even for beginners.
  • HTTP/3 Support: Caddy supports the latest HTTP protocols out of the box.
  • Reverse Proxy and Load Balancing: Built-in capabilities for directing traffic to backend services.
  • Extensibility: A wide range of plugins available to extend functionality.

Why Choose Rocky Linux 9?

Rocky Linux 9, a community-driven, enterprise-grade operating system, provides a stable and secure environment for hosting web servers. Its compatibility with Red Hat Enterprise Linux (RHEL) ensures long-term support and a wide range of available software packages.

Prerequisites

Before we dive into the installation process, ensure that you have the following:

  • A Rocky Linux 9 server with root or sudo access
  • A domain name pointed to your server’s IP address (for automatic HTTPS)
  • Basic knowledge of Linux command-line operations

System Requirements

Caddy is lightweight, but for optimal performance, your server should meet these minimum specifications:

  • 1 CPU core
  • 512 MB RAM
  • 1 GB storage

Pre-Installation Setup

Before installing Caddy, it’s crucial to update your system packages. Open a terminal and run:

sudo dnf update -y

This command ensures that your system has the latest security patches and software versions.

Step 1: Adding the Caddy Repository

Caddy is not available in the default Rocky Linux repositories. To install it, we need to add the official Caddy repository using COPR (Cool Other Package Repo).

First, install the COPR plugin for DNF:

sudo dnf install 'dnf-command(copr)' -y

Next, enable the Caddy repository:

sudo dnf copr enable @caddy/caddy -y

This command adds the Caddy repository to your system’s package manager, allowing you to install and update Caddy using DNF.

Troubleshooting Repository Addition

If you encounter any issues adding the repository, try the following:

  1. Ensure your system’s date and time are correct.
  2. Check your internet connection.
  3. Verify that you have sufficient permissions to add repositories.

Step 2: Installing Caddy

With the repository added, installing Caddy is straightforward. Run the following command:

sudo dnf install caddy -y

This command downloads and installs the latest version of Caddy along with its dependencies.

Verifying the Installation

After the installation completes, verify that Caddy was installed correctly by checking its version:

caddy version

You should see output similar to:

v2.x.x

If you receive a “command not found” error, try logging out and back in to refresh your shell’s PATH, or restart your SSH session.

Step 3: Starting and Enabling the Caddy Service

Now that Caddy is installed, we need to start the service and enable it to run on system boot.

To start the Caddy service, run:

sudo systemctl start caddy

To enable Caddy to start automatically on system boot:

sudo systemctl enable caddy

Verifying Service Status

Check the status of the Caddy service to ensure it’s running correctly:

sudo systemctl status caddy

Look for “Active: active (running)” in the output. If you see this, Caddy is running successfully.

Troubleshooting Service Issues

If Caddy fails to start, you can investigate the issue by checking the service logs:

sudo journalctl -u caddy

Common issues include:

  • Port conflicts: Ensure no other service is using port 80 or 443.
  • Permission problems: Verify that Caddy has the necessary permissions to bind to ports and access its configuration files.

Step 4: Configuring a Basic Caddy Server

Caddy’s configuration file, known as the Caddyfile, is located at `/etc/caddy/Caddyfile`. This file defines how Caddy should handle incoming requests and serve your websites.

Let’s create a basic configuration to serve a simple website:

sudo nano /etc/caddy/Caddyfile

Replace the contents with:

example.com {
    root * /var/www/example.com
    file_server
    encode gzip
}

Replace `example.com` with your actual domain name.

Understanding the Caddyfile

Let’s break down this configuration:

  • `example.com`: This is the domain Caddy will respond to.
  • `root * /var/www/example.com`: Sets the root directory for your website files.
  • `file_server`: Enables Caddy’s built-in file server.
  • `encode gzip`: Enables Gzip compression for faster content delivery.

Creating Your Web Content

Create a directory for your website and add a simple HTML file:

sudo mkdir -p /var/www/example.com
sudo nano /var/www/example.com/index.html

Add some basic HTML content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome to My Caddy Server</title>
</head>
<body>
    <h1>Hello, Caddy on Rocky Linux 9!</h1>
    <p>This is a basic web page served by Caddy.</p>
</body>
</html>

Applying Configuration Changes

After making changes to the Caddyfile, reload the Caddy service:

sudo systemctl reload caddy

Step 5: Securing Your Server with HTTPS

One of Caddy’s standout features is its automatic HTTPS provisioning. By default, Caddy will attempt to obtain and renew SSL/TLS certificates from Let’s Encrypt for your domains.

To enable automatic HTTPS, ensure your domain’s DNS A record points to your server’s IP address, and that ports 80 and 443 are open in your firewall.

Configuring Firewall

If you’re using `firewalld`, open the necessary ports:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Verifying HTTPS Setup

Once your configuration is set and Caddy is reloaded, visit your domain using HTTPS:

https://example.com

You should see a valid SSL certificate in your browser’s address bar.

Troubleshooting SSL Issues

If HTTPS isn’t working:

  • Check that your domain’s DNS is correctly configured.
  • Ensure ports 80 and 443 are open and not blocked by your hosting provider.
  • Review Caddy’s logs for any certificate-related errors: `sudo journalctl -u caddy`

Step 6: Advanced Configuration Options

Caddy’s flexibility allows for advanced configurations to suit various needs. Here are some examples:

Reverse Proxy Setup

To use Caddy as a reverse proxy for a backend application:

example.com {
    reverse_proxy localhost:8080
}

This configuration forwards requests to a service running on port 8080.

Load Balancing

Caddy can distribute traffic across multiple backends:

example.com {
    reverse_proxy {
        to backend1:8080 backend2:8080 backend3:8080
        lb_policy round_robin
    }
}

Enabling HTTP/3

To enable HTTP/3 support:

example.com {
    servers {
        protocol {
            experimental_http3
        }
    }
    # ... other directives
}

Adding Plugins

Caddy’s functionality can be extended with plugins. For example, to add basic authentication:

example.com {
    basicauth {
        user JDJhJDEwJC4uLg==  # BCrypt hash of the password
    }
    # ... other directives
}

Remember to use a properly hashed password in your actual configuration.

Step 7: Testing and Troubleshooting

Regular testing ensures your Caddy server is functioning correctly. Here are some methods to test and troubleshoot your setup:

Performance Testing

Use tools like `curl` to test your server’s response:

curl -I https://example.com

This command shows the HTTP headers returned by your server, including status codes and SSL information.

Common Issues and Solutions

  • Service Not Starting: Check system logs (`journalctl -u caddy`) for errors related to configuration or permissions.
  • SSL Certificate Errors: Ensure your domain points to the correct IP and that Caddy has permission to bind to ports 80 and 443.
  • Configuration Syntax Errors: Use `caddy fmt --overwrite /etc/caddy/Caddyfile` to format and check your Caddyfile for syntax errors.

Congratulations! You have successfully installed Caddy. Thanks for using this tutorial for installing the Caddy web server on Rocky Linux 9 system. For additional help or useful information, we recommend you check the official Caddy website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button