How to Install Caddy Web Server on Debian 11

Caddy is a free, open-source, and modern web server written in GO language. It is a lightweight and commercially supported web server that supports HTTP/2 and experimental HTTP/3 protocols. It can run anywhere with no external dependencies and is expanded via plugins. It is designed with security in mind and provides a number of features that are useful for hosting websites.

In this tutorial, I will explain how to install the Caddy web server on Debian 11.

Prerequisites

  • A server running Debian 11.
  • A valid domain name pointed with your server IP.
  • A root password is configured on the server.

Install Caddy on Debian 11

By default, the Caddy package is not included in the Debian 11 default repository. So you will need to add the Caddy repository to your system.

First, install all required dependencies using the following command:

apt-get install -y curl debian-keyring debian-archive-keyring apt-transport-https

Next, download and add the GPG key with the following command:

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | apt-key add -

Next, add a Caddy repository to the APT using the following command:

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list

Next, update the repository and install the Caddy web server by running the following command:

apt-get update
apt-get install caddy -y

Once the Caddy has been installed, verify the Caddy version using the following command:

caddy version

You will get the following output:

v2.4.5 h1:P1mRs6V2cMcagSPn+NWpD+OEYUYLIf6ecOa48cFGeUg=

To allow caddy binary to connect to privileged ports like 80 and 443, run the following command:

setcap 'cap_net_bind_service=+ep' /usr/bin/caddy

Manage Caddy Service

You can manage the Caddy service using the systemd.

To start the Caddy service, run the following command:

systemctl start caddy

To stop the Caddy service, run the following command:

systemctl stop caddy

To enable the Caddy service to start at system reboot, run the following command:

systemctl enable caddy

To check the status of the Caddy service, run the following command:

systemctl status caddy

You will get the following output:

? 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

Oct 16 14:34:44 debian11 caddy[2370]: JOURNAL_STREAM=8:16308
Oct 16 14:34:44 debian11 caddy[2370]: {"level":"info","ts":1634394884.658216,"msg":"using provided configuration","config_file":"/etc/caddy/Ca>
Oct 16 14:34:44 debian11 caddy[2370]: {"level":"info","ts":1634394884.6623824,"logger":"admin","msg":"admin endpoint started","address":"tcp/l>
Oct 16 14:34:44 debian11 caddy[2370]: {"level":"info","ts":1634394884.6626618,"logger":"http","msg":"server is listening only on the HTTP port>
Oct 16 14:34:44 debian11 caddy[2370]: {"level":"info","ts":1634394884.663054,"msg":"autosaved config (load with --resume flag)","file":"/var/l>
Oct 16 14:34:44 debian11 systemd[1]: Started Caddy.
Oct 16 14:34:44 debian11 caddy[2370]: {"level":"info","ts":1634394884.6655433,"msg":"serving initial configuration"}
Oct 16 14:34:44 debian11 caddy[2370]: {"level":"info","ts":1634394884.6656897,"logger":"tls","msg":"cleaning storage unit","description":"File>
Oct 16 14:34:44 debian11 caddy[2370]: {"level":"info","ts":1634394884.6657932,"logger":"tls","msg":"finished cleaning storage units"}
Oct 16 14:34:44 debian11 caddy[2370]: {"level":"info","ts":1634394884.6694443,"logger":"tls.cache.maintenance","msg":"started background certi>

Enable PHP Support in Caddy

First, install PHP and other necessary extensions using the following command:

apt-get install php-fpm php-mysql php-curl php-gd php-mbstring php-common php-xml php-xmlrpc -y

After installing PHP, edit the PHP-FPM configuration file and change the default user and group with caddy:

nano /etc/php/7.4/fpm/pool.d/www.conf

Find and replace user and group name from www-data to caddy:

user = caddy
group = caddy
listen.owner = caddy
listen.group = caddy

Save and close the file then restart the PHP-FPM service to apply the changes:

systemctl restart php7.4-fpm

Create Caddy Virtual Host Configuration File

The Caddy default virtual host configuration file is located at /etc/caddy/Caddyfile.

Edit the /etc/caddy/Caddyfile file with the following command:

nano /etc/caddy/Caddyfile

Remove all lines and add the following lines:

caddy.example.com:80 {
    root * /usr/share/caddy/
    encode gzip zstd
    php_fastcgi unix//run/php/php7.4-fpm.sock
}

Save and close the file then restart the Caddy service to apply the changes:

systemctl restart caddy

Next, create a sample PHP file for Caddy using the following command:

nano /usr/share/caddy/info.php

Add the following lines:

<?php

phpinfo();
?>

Save and close the file when you are finished.

Access Caddy Website

Now, open your web browser and access the Caddy website using the URL http://caddy.example.com/info.php. You should see the PHP page on the following screen:

PHP Info page

Conclusion

Congratulations! you have successfully installed the Caddy web server on Debian 11. You can now how your own website using the Caddy web server. Feel free to ask me if you have any questions.

Share this page:

0 Comment(s)