Install Caddy Web Server on Debian 12 – Easy Setup – OrcaCore

Posted on

Install Caddy Web Server on Debian 12 - Easy Setup - OrcaCore

Install Caddy Web Server on Debian 12 – Easy Setup – OrcaCore

In this guide, we will teach you how to Install Caddy Web Server on Debian 12 Bookworm. Caddy is a simple and secure web server with amazing features useful for hosting websites. One standout feature is its ability to automatically obtain and manage TLS certificates from Let’s Encrypt, enabling HTTPS, and its built-in support for HTTP/2.

You can now follow the guide steps provided by the Orcacore team to Install Caddy Web Server on Debian 12.

To Install Caddy Web Server on Debian 12, 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 Web Server on Debian 12.

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 Caddy Web Server on Debian 12:

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 step of Install Caddy Web Server on Debian 12, 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 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.

You may also like the following articles:

Install and Run Apache Web Server on Debian 12

Install Nginx Web Server on AlmaLinux 9

Install Apache Web Server on Rocky Linux 8

Alternative Solutions to Installing Caddy on Debian 12

While the provided guide outlines a standard method for installing and configuring Caddy, alternative approaches exist that might better suit certain use cases or preferences. Here are two alternative solutions:

1. Using Caddy’s Official Debian Repository

Instead of relying solely on the default Debian 12 repository, you can leverage Caddy’s official Debian repository. This method often provides access to the latest Caddy releases and potentially more streamlined updates.

Explanation:

Caddy’s official repository is maintained by the Caddy developers and offers the most up-to-date versions of the server. This approach avoids potential delays in receiving new features or security patches that might occur with the default Debian repository. Adding the repository involves importing the Caddy GPG key and adding the repository URL to your system’s package sources.

Steps:

  1. Install the debian-keyring package: This provides tools for managing GPG keys.

    sudo apt update
    sudo apt install -y debian-keyring
  2. Add Caddy’s package signing key: This verifies the authenticity of the packages you’ll be installing.

    sudo apt install -y debian-archive-keyring
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
  3. Add the Caddy repository to your APT sources: This tells your system where to find Caddy packages.

    echo "deb [signed-by=/usr/share/keyrings/caddy-stable-archive-keyring.gpg] https://dl.cloudsmith.io/public/caddy/stable/deb/debian bookworm main" | sudo tee /etc/apt/sources.list.d/caddy-stable.list
  4. Update your package list and install Caddy:

    sudo apt update
    sudo apt install caddy -y

The remaining steps for configuring Caddy, PHP, and setting up your Caddyfile are largely the same as in the original guide. However, you are now sourcing Caddy from its official repository.

2. Using Docker to Run Caddy

Another robust alternative is to run Caddy within a Docker container. This approach provides isolation, portability, and simplifies dependency management.

Explanation:

Docker containers package applications and their dependencies into isolated units, ensuring consistent behavior across different environments. Using Docker for Caddy means you don’t need to worry about conflicting dependencies with other software on your host system. You also gain the flexibility to easily deploy Caddy to different servers or cloud platforms.

Steps:

  1. Install Docker: If you don’t have Docker installed, follow the official Docker documentation for Debian: https://docs.docker.com/engine/install/debian/

  2. Create a docker-compose.yml file: This file defines the services that make up your application (in this case, Caddy).

    version: "3.8"
    services:
      caddy:
        image: caddy:latest
        restart: unless-stopped
        ports:
          - "80:80"
          - "443:443"
        volumes:
          - ./Caddyfile:/etc/caddy/Caddyfile
          - ./data:/data
          - ./config:/config
        environment:
          - PUID=1000 # Replace with your user ID
          - PGID=1000 # Replace with your group ID
      php:
        image: php:8.2-fpm-alpine
        volumes:
          - ./www:/var/www/html
  3. Create a Caddyfile: This file configures Caddy’s behavior. Place it in the same directory as your docker-compose.yml.

    your-domain-name:80 {
        root * /var/www/html
        encode gzip zstd
        php_fastcgi php:9000
    }
  4. Create a directory for your website files: This is where you’ll put your PHP files (like info.php).

    mkdir www
    cd www
    vi info.php
  5. Add the PHP info code to info.php:

    <?php
    phpinfo();
    ?>
  6. Start the Docker containers:

    docker-compose up -d

Now, access http://your-domain-name/info.php in your browser. Docker will handle the networking between the Caddy and PHP containers. Note that you don’t need to install PHP separately on your host machine when using Docker in this way; it runs entirely within the container. Remember to replace your-domain-name with your actual domain.

Leave a Reply

Your email address will not be published. Required fields are marked *