Install Caddy Web Server on Rocky Linux 8

This tutorial intends to teach you How To Install Caddy Web Server on Rocky Linux 8. Also, you learn to create a new site on your Caddy web server.

Caddy is an open-source web server platform designed to be simple, easy to use, and secure. Written in Go with zero dependencies, Caddy is easy to download and runs on almost every platform that Go compiles.

By default, Caddy comes with support for automatic HTTPS by provisioning and renewing certificates through Let’s Encrypt.

Caddy is the only one to provide these features out of the box, and it also comes with automatic redirection of HTTP traffic to HTTPS.

Compared to Apache and Nginx, Caddy’s configuration files are much smaller. Additionally, Caddy runs on TLS 1.3, the newest standard in transport security.

Installing Caddy is straightforward. Now follow the steps below to complete this guide.

How To Install Caddy Web Server on Rocky Linux 8

To install the Caddy web server, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide the Initial Server Setup with Rocky Linux 8.

Also, you need a domain name that pointed to your server’s IP address.

Install and Configure Caddy on Rocky Linux 8

First of all, you must update your local package index with the command below:

sudo dnf update -y

By default, Caddy is not available in the default Rocky Linux repository. In this guide, we use the Copr repository to install Caddy. The Copr (Cool Other Package Repo) is a Fedora project to help make building and managing third-party package repositories easy.

To install Copr, run the following command:

sudo dnf install 'dnf-command(copr)'

Then, you need to enable the Caddy repository by running the command below:

sudo dnf copr enable @caddy/caddy
Output
Do you really want to enable copr.fedorainfracloud.org/@caddy/caddy? [y/N]: y
Repository successfully enabled.

Run the system update again:

sudo dnf update -y

At this point, you can use the following command to install the Caddy web server:

sudo dnf install caddy -y

When your installation is completed, start and enable your service to start on boot:

$ sudo systemctl start caddy
$ sudo systemctl enable caddy

Verify that your Caddy service is active and running on Rocky Linux 8:

sudo systemctl status caddy

You should get the following output:

Output
● caddy.service - Caddy
Loaded: loaded (/usr/lib/systemd/system/caddy.service; enabled; vendor prese>
Active: active (running) since Tue 2022-08-09 06:02:02 EDT; 11s ago
Docs: https://caddyserver.com/docs/
Main PID: 88881 (caddy)
Tasks: 7 (limit: 11413)
Memory: 16.3M
CGroup: /system.slice/caddy.service
└─88881 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

To see your Caddy web server version, you can run:

caddy version
Output
v2.5.2 h1:eCJdLyEyAGzuQTa5Mh3gETnYWDClo1LjtQm2q9RNZrs=

Now that you have Caddy installed and running on your Rocky Linux 8, let’s see how to create a new site on the Caddy web server.

Create a new site with the Caddy webserver on Rocky Linux 8

This step is similar to the Apache virtual hosts or Nginx server blocks.

First, create a directory for your site with the command below:

sudo mkdir -p /var/www/your-domain/html

Then, you need to create a directory for the logs:

sudo mkdir /var/log/caddy

Set the correct ownership for both directories with the commands below:

$ sudo chown caddy:caddy /var/www/your-domain/html -R
$ sudo chown caddy:caddy /var/log/caddy

Now you need to create an index.html file in your new site directory with your favorite text editor, here we use vi:

sudo vi /var/www/your-domain/html/index.html

Add the following content to the file:

<!DOCTYPE html>
<html>
<head>
<title>Hi</title>
</head>
<body>
<h1>Welcome to orcacore</h1>
</body>
</html>

When you are done, save and close the file.

Here you need to edit the Caddy configuration file. Open the file with your favorite text editor, here we use vi:

sudo vi /etc/caddy/Caddyfile

Remove all the contents or comment them on the file and add the following contents to the file:

your-domain {
    root * /var/www/your-domain/html
    file_server
    encode gzip

    log {
        output file /var/log/caddy/your-domain.log
    }

    @static {
        file
        path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.pdf *.webp
    }
    header @static Cache-Control max-age=5184000

    tls [email protected]your-domain
}

When you are done, save and close the file.

Then, you need to validate your configuration with the command below:

caddy validate --adapter caddyfile --config /etc/caddy/Caddyfile

Note: If the on-screen output results in a formatted warning, you can fix it by running the command below:

caddy fmt --overwrite /etc/caddy/Caddyfile

Next, restart the Caddy service on Rocky Linux 8 to apply the changes:

sudo systemctl restart caddy

Here we assumed that you have enabled the firewalld. Now you need to allow traffic on HTTP and HTTPS through the firewall with the following commands:

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

Reload the firewall to apply the new rules:

sudo systemctl reload firewalld

Access Caddy Web Server

At this point, you can access the Caddy web server by typing your domain name or server’s IP address in your web browser:

http://server-ip-or-domain

You will see the Welcome test page.

Caddy test page

That’s It, you are done.

For more information, you can visit the Caddy Documentation page.

Conclusion

At this point, you learn to Install the Caddy Web server on Rocky Linux 8 by using the Copr repository and also you learn to create a new site with caddy.

Hope you enjoy it.

You may be interested in these articles:

How To Install Apache Cassandra on Rocky Linux 8

Install and Use aaPanel on Rocky Linux 8

How To Install and Run Xfce Desktop on Rocky Linux 8

How To Generate SSH Key Pairs on Rocky Linux 8

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular