The Caddy web server is a lightweight, modern, and open-source solution written in the GO language. It offers support for HTTP/2 and experimental HTTP/3 protocols and is designed with security in mind. In this tutorial, we will guide you through the process of installing and configuring the Caddy web server on Debian 11.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  • A server running Debian 11.
  • A valid domain name pointed to your server’s IP address.
  • Root access to the server.

Step 1: Installing Caddy on Debian 11

By default, the Caddy package is not included in the Debian 11 default repository. Therefore, we need to add the Caddy repository to our system.

  1. Start by installing all the required dependencies:
   apt-get install -y curl debian-keyring debian-archive-keyring apt-transport-https
  1. Next, download and add the GPG key:
   curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | apt-key add -
  1. Add the Caddy repository to the APT:
   curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
  1. Update the repository and install the Caddy web server:
   apt-get update
   apt-get install caddy -y
  1. Verify the Caddy version:
   caddy version

You should see an output similar to this:

   v2.4.5 h1:P1mRs6V2cMcagSPn+NWpD+OEYUYLIf6ecOa48cFGeUg=
  1. To allow the Caddy binary to connect to privileged ports like 80 and 443, run the following command:
   setcap 'cap_net_bind_service=+ep' /usr/bin/caddy

Step 2: Managing the Caddy Service

Now that Caddy is installed, let’s learn how to manage the Caddy service using systemd.

  • To start the Caddy service, use the following command:
  systemctl start caddy
  • To stop the Caddy service, use the following command:
  systemctl stop caddy
  • To enable the Caddy service to start at system reboot, use the following command:
  systemctl enable caddy
  • To check the status of the Caddy service, use the following command:
  systemctl status caddy

You should see an output similar to this:

  ● caddy.service - Caddy
Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-10-16 14:34:44 UTC; 2min 30s ago
Docs: https://caddyserver.com/docs/
Main PID: 2370 (caddy)
Tasks: 7 (limit: 2341)
Memory: 17.5M
CPU: 30ms
CGroup: /system.slice/caddy.service
├─2370 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
...

Step 3: Enabling PHP Support in Caddy

To enable PHP support in Caddy, you need to install PHP and other necessary extensions.

  1. Install PHP and the required extensions:
   apt-get install php-fpm php-mysql php-curl php-gd php-mbstring php-common php-xml php-xmlrpc -y
  1. Edit the PHP-FPM configuration file:
   nano /etc/php/7.4/fpm/pool.d/www.conf
  1. Find the following lines and replace the user and group names from www-data to caddy:
   user = caddy
   group = caddy
   listen.owner = caddy
   listen.group = caddy
  1. Save and close the file, then restart the PHP-FPM service:
   systemctl restart php7.4-fpm

Step 4: Creating a Caddy Virtual Host Configuration

Next, we’ll create a virtual host configuration file for Caddy.

  1. Edit the Caddyfile:
   nano /etc/caddy/Caddyfile
  1. Remove all existing lines and add the following configuration:
caddy.example.com:80 {
root * /usr/share/caddy/
encode gzip zstd
php_fastcgi unix//run/php/php7.4-fpm.sock
}

Note: Replace caddy.example.com with your own domain name.

  1. Save and close the file, then restart the Caddy service:
   systemctl restart caddy

Step 5: Accessing Your Caddy Website

To access your Caddy website, follow these steps:

  1. Open your web browser.
  2. Enter the URL http://caddy.example.com/info.php in the address bar.Note: Replace caddy.example.com with your own domain name.
  3. You should now see the PHP information page.

Conclusion

Congratulations! You have successfully installed and configured the Caddy web server on Debian 11. You can now host your own website using the powerful features of Caddy. If you have any questions or need further assistance, feel free to contact our team at Shape.host. We provide reliable and scalable cloud hosting solutions, including Cloud VPS, to support your business needs.