How to install Caddy web server on Rocky Linux 8.6

To install Caddy Web Server on Rocky Linux 8.6

Introduction :

Caddy is an open-source HTTP Web server that is used for various platforms including Windows, Mac, Linux, FreeBSD, OpenBSD, etc. It is a full-featured HTTP/2 web server that can instantly enable HTTPS. It permits designers to create amazing things with high flexibility and low overhead.

Installation Procedure:

Step 1: Check the installed version of OS by using the below command

[root@linuxhelp ~]# cat /etc/os-release 
NAME="Rocky Linux"
VERSION="8.6 (Green Obsidian)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="8.6"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Rocky Linux 8.6 (Green Obsidian)"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:rocky:rocky:8:GA"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
ROCKY_SUPPORT_PRODUCT="Rocky Linux"
ROCKY_SUPPORT_PRODUCT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

Step 2: Install the SELinux core policy Python utilities by using the below command

[root@linuxhelp ~]# yum install policycoreutils-python-utils
Rocky Linux 8 - AppStream                              4.4 MB/s | 9.6 MB     00:02    
Rocky Linux 8 - BaseOS                                 3.5 MB/s | 6.7 MB     00:01    
Rocky Linux 8 - Extras                                 9.8 kB/s |  12 kB     00:01    
Package policycoreutils-python-utils-2.9-19.el8.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!

Step 3: Enable caddy repository by using the below command

[root@linuxhelp ~]# dnf copr enable @caddy/caddy
Enabling a Copr repository. Please note that this repository is not part
of the main distribution, and quality may vary.

The Fedora Project does not exercise any power over the contents of
this repository beyond the rules outlined in the Copr FAQ at
<https://docs.pagure.org/copr.copr/user_documentation.html#what-i-can-build-in-copr>,
and packages are not held to any quality or security level.

Please do not file bug reports about these packages in Fedora
Bugzilla. In case of problems, contact the owner of this repository.

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

Step 4: Install Caddy by using the below command

[root@linuxhelp ~]# dnf install caddy
Copr repo for caddy owned by @caddy                    1.1 kB/s | 1.5 kB     00:01    
Dependencies resolved.
=======================================================================================

Installed size: 45 M
Is this ok [y/N]: y
"
 Fingerprint: 4A76 F92F F6D4 0440 F8FC 4F36 C521 91B1 D605 147E
 From       : https://download.copr.fedorainfracloud.org/results/@caddy/caddy/pubkey.gpg
Is this ok [y/N]: y

Running transaction
  Preparing        :                                                               1/1 
  Running scriptlet: caddy-2.5.2-1.el8.x86_64                                      1/1 
  Installing       : caddy-2.5.2-1.el8.x86_64                                      1/1 
  Running scriptlet: caddy-2.5.2-1.el8.x86_64                                      1/1 
  Verifying        : caddy-2.5.2-1.el8.x86_64                                      1/1 

Installed:
  caddy-2.5.2-1.el8.x86_64                                                             

Complete!

Step 5: Add HTTP and HTTPS on firewall by using the below command

[root@linuxhelp ~]# firewall-cmd --add-service=http --permanent 
success
[root@linuxhelp ~]# firewall-cmd --add-service=https --permanent 
success

Step 6: Reload firewall by using the below command

[root@linuxhelp ~]# firewall-cmd --reload
success

Step 7: Set up a home directory, web root, for your website by using the below command

[root@linuxhelp ~]# mkdir -p /var/www/html/example.com

Step 8: Now use SELinux’s chcon command to change the file security context for web content by using the below command

[root@linuxhelp ~]# chcon -t httpd_sys_content_t /var/www/html/example.com/
[root@linuxhelp ~]# chcon -t httpd_sys_rw_content_t /var/www/html/example.com/ -R

Step 9: Create a test page by using the below command

[root@linuxhelp ~]# echo '<!doctype html><head><title>Caddy Test Page</title></head><body><h1>Hello, World!</h1></body></html>' > /var/www/html/example.com/index.html

Step 10: Configure the Caddyfile by using the below command

[root@linuxhelp ~]# vim /etc/caddy/Caddyfile 
example.com {
    root * /var/www/html/example.com
    file_server
}

Step 11: Set SELinux to permissive mode by using the below command

[root@linuxhelp ~]# setenforce 0

Step 12: Start the Caddy service by using the below command

[root@linuxhelp ~]# systemctl start caddy

Step 13: Enable the Caddy service by using the below command

[root@linuxhelp ~]# systemctl enable caddy
Created symlink /etc/systemd/system/multi-user.target.wants/caddy.service → /usr/lib/systemd/system/caddy.service.

Step 14: Go to the browser and ping local host to get caddy test page as shown in the below image

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to install Caddy webserver on rocky Linux 8.6. Your feedback is much welcome.

FAQ
Q
Is Caddy Opensource?
A
Caddy is open-source and free software.
Q
What is Caddy software?
A
Caddy is an open-source HTTP Web server that is used for various platforms including Windows, Mac, Linux, FreeBSD, OpenBSD, etc. It is a full-featured HTTP/2 web server that can automatically enable HTTPS.
Q
./caddy script got executed successfully, but the web page is not getting accessed.
A
The custom port access for Caddy should be added and enabled in the firewall.
Q
2. Does Caddy have redirects?
A
Yes, Caddy does provide Rewrites & Redirects
Q
1. Does Caddy has a WebSocket availability function?
A
Caddy does provide WebSocket connectivity.