How To Install and Configure Caddy on Debian 11

In this article, we want to teach you How To Install and Configure Caddy on Debian 11.

Caddy is a web server designed around simplicity and security that comes with a number of features that are useful for hosting websites. For example, it can automatically obtain and manage TLS certificates from Let’s Encrypt to enable HTTPS and includes support for HTTP/2.

How To Install and Configure Caddy on Debian 11

To install Caddy on Debian 11, you need some requirements first.

Requirements

You need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article The Initial Server Setup with Debian 11.

Also, you need a domain name that pointed to your server’s IP address.

Now follow the steps below to install Caddy on Debian 11.

Set up Caddy on Debian 11

By default Caddy is not available in the default Debian repository. S you need to add the Caddy repository to your server.

First, update your local package index with the command below:

sudo apt update

Then, you need to install the required packages on Debian 11 with the following command:

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

Next, download and add the GPG key:

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

Now you can add the Caddy repository with the following command:

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

At this point, you can install Caddy on your Debian 11:

sudo apt update
sudo apt install caddy -y

When your installation is completed, you can verify your Caddy version on Debian 11:

caddy version
Output
v2.4.6 h1:HGkGICFGvyrodcqOOclHKfvJC0qTU7vny/7FhYp9hNw=

Now you need to allow caddy binary to connect to privileged ports like 80 and 443 with the following command:

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

Start and enable your Caddy service to start on boot with the commands below:

$ sudo systemctl start caddy
$ sudo systemctl enable caddy

Check your Caddy service is active and running on Debian 11 with the command below:

sudo systemctl status caddy

In your output you will see:

Output
caddy.service - Caddy
Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset:>
Active: active (running) since Wed 2022-02-02 04:27:13 EST; 3min 19s ago
Docs: https://caddyserver.com/docs/
Main PID: 1874 (caddy)
Tasks: 8 (limit: 2340)
Memory: 18.0M
CPU: 90ms
CGroup: /system.slice/caddy.service
└─1874 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

Install and Configure PHP for Caddy

At this point, you need to install PHP and its extensions on Debian 11 with the following command:

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

Then, you need to edit the PHP-FPM configuration file and change the default user and group with caddy.

Open the file with your favorite text editor, here we use vi:

sudo vi /etc/php/7.4/fpm/pool.d/www.conf

At the file, find the lines below and change their value to Caddy:

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

When you are done, save and close the file.

Restart the PHP-FPM to apply the changes:

sudo systemctl restart php7.4-fpm

Configure Caddy on Debian 11

At this point, you need to edit the Caddy virtual host configuration file.

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

Open the file with your favorite text editor, here we use vi:

sudo vi /etc/caddy/Caddyfile

Remove the lines at the file and add the following lines to the file with your domain name:

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

When you are done, save and close the file.

Restart the Caddy service on Debian 11 to apply the changes:

sudo systemctl restart caddy

Then, create a sample PHP file for Caddy with the following command:

sudo vi /usr/share/caddy/info.php

Add the following content to the file:

<?php

phpinfo();
?>

When you are done, save and close the file.

Here you can access the Caddy website by typing your domain name in your web browser followed by info.php:

http://your-domain-name/info.php

You should see your PHP info on Debian 11.

PHP info for Caddy on Debian 11

When you have finished reading your PHP info, it’s better to remove your PHP file for more security.

sudo rm -rf /usr/share/caddy/info.php

You can always rebuild your file every time you need it.

Conclusion

At this point, you learn to install and configure Caddy on Debian 11.

Hope you enjoy it.

May this article about Install and Configure Caddy on Centos 7 be useful for you.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular