
Caddy is an open-source webserver for developing and delivering static web applications. The webserver supports HTTP/3 and automatic SSL termination. Caddy also works as a reverse proxy or a load balancer that forwards HTTP client requests to backend services. You can also install Caddy plugins to extend the webserver's functionalities.
This article explains how to install the Caddy webserver on Rocky Linux 9.
app.example.com.Caddy is available in the Extra Packages for Enterprise Linux (EPEL) repository on Rocky Linux 9 but the version might be outdated. Follow the steps below to install the webserver from the Caddy Community Projects (COPR) repository.
Install the DNF Copr Plugin.
Enable the Caddy COPR repository.
Enter Y when prompted to add the repository on your server.
Install Caddy.
View the Caddy version on your server.
Your output should be similar to the one below.
Allow Caddy HTTP port 80 through the firewall.
Reload the firewall to apply changes.
The caddy.service system service manages and controls the Caddy processes on your server. Follow the steps below to enable the service to automatically start at boot and manage the Caddy processes.
Enable the Caddy service to automatically start at boot.
Start the Caddy service.
View the Caddy service status and verify that it's running.
Output:
Open a browser such as Firefox and access your server's public IP address.
Caddy should now display the default web application page.
Caddy stores configurations for virtual hosts under the /etc/caddy directory. It supports virtual host configurations via Caddyfile from any location on your server. Follow the steps below to create a new Caddy virtual host to serve the website from the /var/www/app.example.com/html directory.
Create a new virtual host web root directory. For instance, /var/www/app.example.com/html.
Use a text editor like Nano to create a sample index.html file under the new directory.
Add the following contents to the index.html file.
Save and close the file.
Create a new /etc/caddy/hosts directory to store the Caddy virtual host configuration files.
Open /etc/caddy/Caddyfile, the main Caddy configuration file.
Add the following directive at the end of the /etc/caddy/Caddyfile file.
Save and close the file.
The above import directive instructs Caddy to include additional configuration files from the /etc/caddy/hosts directory.
Create a new virtual host configuration file in the /etc/caddy/hosts/ directory. For example, app.example.com.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a new virtual host that delivers web applications for the app.example.com domain. Within the configuration:
app.example.com: Creates a new virtual host profile that listens for HTTP connections for the app.example.com domain.tls: Sets the email address for validating Let's Encrypt SSL certificates.root: Sets the virtual host web directory.file_server: Enables serving static files.index: Sets the default file to serve when an HTTP client requests your domain.log: Enables and specifies access and error logging.output: Sets the location of the access log file.format: Specifies the log file format.Validate the Caddy configuration for errors.
If you receive the following warning:
Run the following command to correct the configuration file.
Create the Caddy logs directory.
Reload the Caddy webserver to apply the configuration changes.
Caddy runs under the caddy user and group. The user enables the webserver to read and write configuration files on the server. Follow the steps below to restrict access to the Caddy configuration files and the virtual host web applications directory.
Grant Caddy access to the Caddy logs directory.
Grant Caddy access to the /etc/caddy directory.
Grant Caddy access to the virtual host web applications directory.
SELinux is active and blocks specific application operations on Rocky Linux 9 by default. Follow the steps below to configure SELinux and grant the Caddy webserver access to the web applications directory.
Enable the webserver file security context on the virtual host web applications directory.
Apply the SELinux policy.
Allow Caddy to create external network connections.
Caddy delivers web applications using the HTTP port 80 and HTTPS port 443 depending on the virtual host configurations. Follow the steps below to enable the default Firewalld utility and enable network connections to all required ports.
View the firewall status to ensure the firewall is running.
Your output should look like the one below when active.
Allow HTTP port 80 through the firewall.
Allow HTTPS port 443 through the firewall.
Reload the firewall to apply the new changes.
Open a web browser and access your virtual host domain (For example, app.example.com) to run your web application.
You have installed Caddy on Rocky Linux 9 and set up a virtual host configuration to serve web applications. You can also implement Caddy as a reverse proxy or a load balancer. For more information, visit the Caddy documentation.
0 Comments
Be the first to comment and share your perspective with the community.