Install Caddy Web Server on Debian 12

In this guide, we want to teach you to Install and Configure the Caddy Web Server on Debian 12 Bookworm. Caddy is a simple and secure web server that has amazing features that are useful for hosting websites. One of the amazing features is that it can automatically get and manage TLS certificates from Let’s Encrypt to enable HTTPS and includes support for HTTP/2.

Learn To Install and Configure the Caddy Web Server on Debian 12

To complete this guide, you must have access to your server as a non-root user with sudo privileges. For this purpose, you can follow this guide on Initial Server Setup with Debian 12 Bookworm.

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

Now follow the steps below to install Caddy on your server.

Step 1 – Install Caddy on Debian 12 Bookworm

Caddy 2 is available in the default Debian 12 repository. First, run the system update with the following command:

sudo apt update

Then, use the following command to install the Caddy web server:

sudo apt install caddy -y

When your installation is completed, verify the Caddy version:

caddy version
Output
2.6.2

Now you need to allow the 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

Step 2 – Start and Enable Caddy on Debian 12

At this point, you can use the following command to start your Caddy service:

sudo systemctl start caddy

Then, enable your service to start on boot with the command below:

sudo systemctl enable caddy

Verify Caddy web server is active and running on Debian 12:

sudo systemctl status caddy
Ouput
● caddy.service - Caddy
     Loaded: loaded (/lib/systemd/system/caddy.service; enabled; preset: enabl>
     Active: active (running) since Sun 2023-06-18 08:12:09 EDT; 7min ago
       Docs: https://caddyserver.com/docs/
   Main PID: 18850 (caddy)
      Tasks: 7 (limit: 4653)
     Memory: 19.6M
        CPU: 77ms
     CGroup: /system.slice/caddy.service
...

Step 3 – Set up PHP for the Caddy Web Server

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

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

Verify your PHP installation by checking its version:

php --version
Ouput
PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies

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, we use vi:

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

In 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 php8.2-fpm

Step 4 – Configure Caddy on Debian 12

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, 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/php8.2-fpm.sock
}

When you are done, save and close the file.

Restart the Caddy service on Debian 12 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 12.

PHP for Caddy Web Server

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.

For more information, you can visit Caddy Documentation.

Conclusion

At this point, you have learned to Install and Configure the Caddy Web server on Debian 12 Bookworm. It is an easy-to-use web server and has many features that you can use to host your websites. Hope you enjoy using it.

For more guides and articles, you can visit the Orcacore Blog page.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular