115 lines
2.8 KiB
Bash
Executable File
115 lines
2.8 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
# Install Caddy web server on Debian/Ubuntu/Raspbian using the official
|
|
# Cloudsmith apt repository.
|
|
# Follows the official docs:
|
|
# https://caddyserver.com/docs/install#debian-ubuntu-raspbian
|
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo "Please run as root (sudo)." >&2
|
|
exit 1
|
|
fi
|
|
|
|
. /etc/os-release
|
|
|
|
case "$ID" in
|
|
debian|ubuntu|raspbian) ;;
|
|
*)
|
|
echo "Unsupported distribution: $ID (only Debian, Ubuntu and Raspbian are supported)." >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
ARCH="$(dpkg --print-architecture)"
|
|
case "$ARCH" in
|
|
amd64|arm64|armhf) ;;
|
|
*)
|
|
echo "Unsupported architecture: $ARCH" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
|
|
echo "==> Installing prerequisites"
|
|
apt-get update
|
|
apt-get install -y debian-keyring debian-archive-keyring apt-transport-https curl
|
|
|
|
echo "==> Installing Caddy"
|
|
apt-get update
|
|
apt-get install -y caddy
|
|
|
|
echo "==> Creating site directories"
|
|
mkdir -p /etc/caddy/sites-available /etc/caddy/sites-enabled /var/log/caddy
|
|
if id caddy >/dev/null 2>&1; then
|
|
chown -R caddy:caddy /var/log/caddy
|
|
fi
|
|
|
|
echo "==> Writing global config with distinguished logging"
|
|
cat > /etc/caddy/Caddyfile <<'EOF'
|
|
# Global options
|
|
{
|
|
# Distinguished logging:
|
|
# - global log: process/error-level events for all sites
|
|
# - per-site access logs: written by each site block in sites-enabled/
|
|
log {
|
|
output file /var/log/caddy/caddy.log
|
|
format json
|
|
level INFO
|
|
}
|
|
}
|
|
|
|
# Load enabled sites (symlinks into sites-enabled/)
|
|
import /etc/caddy/sites-enabled/*.conf
|
|
EOF
|
|
|
|
echo "==> Writing default site config"
|
|
cat > /etc/caddy/sites-available/00-default.conf <<'EOF'
|
|
:80 {
|
|
root * /usr/share/caddy
|
|
encode gzip
|
|
file_server
|
|
|
|
# Per-site access log (distinguished from the global log)
|
|
log {
|
|
output file /var/log/caddy/default.access.log
|
|
format json
|
|
level INFO
|
|
}
|
|
}
|
|
EOF
|
|
|
|
ln -sf /etc/caddy/sites-available/00-default.conf /etc/caddy/sites-enabled/00-default.conf
|
|
|
|
echo "==> Writing template site config"
|
|
cat > /etc/caddy/sites-available/example.com.conf <<'EOF'
|
|
# Template site config.
|
|
# To create a real site:
|
|
# 1. cp /etc/caddy/sites-available/example.com.conf /etc/caddy/sites-available/mysite.com.conf
|
|
# 2. Replace example.com, /var/www/example.com and the log path.
|
|
# 3. Enable it:
|
|
# ln -s /etc/caddy/sites-available/mysite.com.conf /etc/caddy/sites-enabled/mysite.com.conf
|
|
# 4. Reload: systemctl reload caddy
|
|
#
|
|
# example.com {
|
|
# root * /var/www/example.com
|
|
# encode gzip
|
|
# file_server
|
|
#
|
|
# # Per-site access log (distinguished from the global log)
|
|
# log {
|
|
# output file /var/log/caddy/example.com.access.log
|
|
# format json
|
|
# level INFO
|
|
# }
|
|
# }
|
|
EOF
|
|
|
|
echo "==> Restarting Caddy with new config"
|
|
systemctl restart caddy
|
|
|
|
echo "Caddy installed. Service status: systemctl status caddy"
|
|
echo "Sites: /etc/caddy/sites-available (template: example.com.conf)"
|
|
echo "Enabled: /etc/caddy/sites-enabled"
|