How to Install and Configure the Caddy Web Server on CentOS 8

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

Caddy is a fast, open-source, and security-focused web server written in Go. Caddy includes modern features such as support for virtual hosts, minification of static files, and HTTP/2. Caddy is also the first web-server that can obtain and renew SSL/TLS certificates automatically using Let’s Encrypt.

Before You Begin

  1. Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.

  2. Complete the sections of our Securing Your Server guide to create a standard user account, harden SSH access, and remove unnecessary network services.

  3. Register (purchase) your site’s domain name and follow our DNS Manager Overview guide to point the domain to your Linode.

  4. Update your system:

    sudo yum update
    
  5. Install the SELinux core policy Python utilities. This gives you the ability to manage SELinux settings in a fine-grained way.

     sudo yum install -y policycoreutils-python-utils
    

Install Caddy

  1. Install the dnf-command(cpor) plugin and enable caddy:

    sudo dnf install 'dnf-command(copr)'
    sudo dnf copr enable @caddy/caddy
    
  2. Install Caddy:

    sudo dnf install caddy
    
  3. To verify the installation of caddy type: caddy version An output similar to the following appears:

     v2.4.3 h1:Y1FaV2N4WO3rBqxSYA8UZsZTQdN+PwcoOcAiZTM8C0I=
    

Allow HTTP and HTTPS Connections

Caddy serves websites using HTTP and HTTPS protocols, so you need to allow access to the ports 80, and 443.

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

Add Web Content

  1. Set up a home directory, web root, for your website:

    sudo mkdir -p /var/www/html/example.com
    
  2. Use SELinux’s chcon command to change the file security context for web content:

     sudo chcon -t httpd_sys_content_t /var/www/example.com -R
     sudo chcon -t httpd_sys_rw_content_t /var/www/example.com -R
    
  3. Create a test page:

    echo '<!doctype html><head><title>Caddy Test Page</title></head><body><h1>Hello, World!</h1></body></html>' > /var/www/html/example.com/index.html
    

Configure the Caddyfile

Add your hostname and web root to the Caddy configuration. Use an editor of your choice and replace :80 with your domain name. Set the root directory of the site to /var/www/html/example.com Replace example.com with your site’s domain name:

File: /etc/caddy/Caddyfile
1
2
3
4
example.com {
    root * /var/www/html/example.com
    file_server
}

Start and Enable the Caddy Service

  1. Temporarily set SELinux to permissive mode in order to start the Caddy service.

     sudo setenforce 0
    
  2. Enable the Caddy service:

    sudo systemctl start caddy
    
  3. Verify that the service is active:

     sudo systemctl status caddy
    

    An output similar to the following appears:

    ● caddy.service - Caddy
       Loaded: loaded (/usr/lib/systemd/system/caddy.service; disabled; vendor preset: disabled)
       Active: active (running) since Thu 2021-09-02 18:25:29 IST; 4s ago
         Docs: https://caddyserver.com/docs/
     Main PID: 19314 (caddy)
       CGroup: /system.slice/caddy.service
               └─19314 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile...
    
    Sep 02 18:25:29 caddy caddy[19314]: SHELL=/sbin/nologin
    Sep 02 18:25:29 caddy caddy[19314]: {"level":"info","ts":1630587329.1270738..."}
    Sep 02 18:25:29 caddy systemd[1]: Started Caddy.
    Sep 02 18:25:29 caddy caddy[19314]: {"level":"info","ts":1630587329.1316314...]}
    Sep 02 18:25:29 caddy caddy[19314]: {"level":"info","ts":1630587329.1317837...0}
    Sep 02 18:25:29 caddy caddy[19314]: {"level":"info","ts":1630587329.1324193..."}
    Sep 02 18:25:29 caddy caddy[19314]: {"level":"info","ts":1630587329.1324632..."}
    Sep 02 18:25:29 caddy caddy[19314]: {"level":"info","ts":1630587329.1325648..."}
    Sep 02 18:25:29 caddy caddy[19314]: {"level":"info","ts":1630587329.1326034..."}
    Sep 02 18:25:29 caddy caddy[19314]: {"level":"info","ts":1630587329.1326299..."}
    Hint: Some lines were ellipsized, use -l to show in full.
        

To check the latest logs without truncation use sudo journalctl -u caddy --no-pager | less +G.

  1. Set SELinux back to enforcing mode once you have successfully started the Caddy service.

     sudo setenforce 1
    
  2. Type your domain in the browser on your local machine and you should see the test page. If everything is configured correctly, you should see a green lock symbol in the URL bar, indicating that your connection is secure.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.