Caddy server v2 setup

Caddy server download

Debian 10 64bit with ssh

Download url: https://caddyserver.com/docs/download

Step 1: Move caddy file under /usr/bin

sudo mv caddy /usr/bin/

Step 2: Create caddy user and group

## Create caddy group
sudo groupadd --system caddy

## Create caddy user
sudo useradd --system \
    --gid caddy \
    --create-home \
    --home-dir /var/lib/caddy \
    --shell /usr/sbin/nologin \
    --comment "Caddy web server" \
    caddy

## Change permission to caddy exec file

chown caddy:caddy /usr/bin/caddy
chmod +x /usr/bin/caddy

Step 3: Setup systemd service

## Go to systemd dir
cd /etc/systemd/system

## Download service file
wget https://raw.githubusercontent.com/caddyserver/dist/master/init/caddy.service

## Enable and start
sudo systemctl daemon-reload
sudo systemctl enable caddy
sudo systemctl start caddy

## Stop caddy or reload
sudo systemctl stop caddy
sudo systemctl reload caddy

Step 4: Check caddy system status and log

systemctl status caddy
journalctl -u caddy

Step 5: Setup config file for Caddy

cd /etc/caddy

## Config simple reverse proxy

nano Caddyfile

## Paste in this 
## Default https

de11.xxx.com:91, de22.xxx.com:91 {
        reverse_proxy 127.0.0.1:300
}

Act as static file server

## If you don't have an index file but you want to display a file listing, use the browse argument 

server.com {

encode zstd gzip
root * /home/me/mysite
file_server browse

}

Matcher reverse proxy

## In practice, we may want to use the reverse proxy only for API requests, i.e. requests with a base path of /api/. This is easy to do by adding a matcher token

server.com {

file_server
reverse_proxy /api/* 127.0.0.1:9005

}

Work with env

export SITE_ADDRESS=localhost:9055

## Caddy file config

{$SITE_ADDRESS} {
file_server
}

Work with PHP-cgi

example.com

root * /var/www
php_fastcgi /blog/* localhost:9000
file_server

Redirect www to no www

http://1234.xxx.xxx:91 {
    redir http://33.xxx.xxx:91{uri}
}

http://33.xxx.xxx:91 {
        reverse_proxy 127.0.0.1:30001
}