Installing & using Caddy:

Stable releases:


sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

Testing releases:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/testing/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-testing-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/testing/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-testing.list
sudo apt update
sudo apt install caddy

Snippets

Go to /etc/caddy and create a directory (let's call it snippet)

Inside the snipper directory, write the following file

# /etc/caddy/snippets/laravel-app.conf

# snippets/laravel-app
# {args[0]} represents the root url of the app. Example: "exmaple.com".
# {args[1]} represents the root path to the app. Example: "/var/www/html/laravel-app"

(laravel-app) {
    {args[0]} {
        # apply security header
        import ../security.conf

        # Resolve the root directory for the app
        root * {args[1]}/public

        # Provide Zstd and Gzip compression
        encode zstd gzip

        # Enable PHP-FPM 
        # Change this based on installed php version
        php_fastcgi unix//run/php/php8.2-fpm.sock 

        # Allow caddy to serve static files
        file_server
    }
}

Next we will use these snippets in order to create virtual hosts.

Using the snippets

Now we will use the snippets that we made earlier.

# /etc/caddy/Caddyfile

import snippets/*

# Use the "laravel-app" snippet for our site:
import laravel-app exmaple.com /var/www/html/laravel-app

Reverse proxy

example.com {
        reverse_proxy 127.0.0.1:5000
}

Last Updated: