#!/usr/bin/env bash # # Provision an nginx vhost for one of Ladill's own service subdomains # (auth./account./pay./qr./... .ladill.com), reusing the existing # *.ladill.com wildcard TLS certificate. # # The monolith phase needs nothing here — the ladill.com vhost already # serves *.ladill.com over the wildcard cert. Use this when a service gets # its OWN app (separate Laravel app dir, or a backend on another host/port), # so its subdomain stops falling through to the catch-all and routes to the # real service instead. # # Idempotent + safe: writes to sites-available, validates with `nginx -t`, # and only reloads on success (restores the previous config on failure). # # Usage: # setup-service-subdomain-nginx.sh --app [--fpm ] # setup-service-subdomain-nginx.sh --proxy # # Options: # --app DIR Serve a Laravel app from DIR/public via PHP-FPM. # --fpm SOCK PHP-FPM socket for --app (default: /run/php/php8.4-fpm-ladill.sock). # --proxy URL Reverse-proxy the subdomain to URL (e.g. http://127.0.0.1:9001). # --zone ZONE Apex zone (default: ladill.com). # --cert NAME Let's Encrypt lineage under /etc/letsencrypt/live (default: ladill-wildcard). # --dry-run Print the rendered vhost and exit without writing anything. # # Examples: # setup-service-subdomain-nginx.sh auth --app /var/www/auth.ladill.com/current # setup-service-subdomain-nginx.sh pay --proxy http://127.0.0.1:9002 set -euo pipefail SUB="${1:-}"; shift || true [ -n "$SUB" ] || { echo "ERROR: missing (e.g. 'auth')"; exit 2; } MODE=""; APP_DIR=""; PROXY_URL="" FPM_SOCK="/run/php/php8.4-fpm-ladill.sock" ZONE="ladill.com" CERT="ladill-wildcard" DRY_RUN=0 while [ $# -gt 0 ]; do case "$1" in --app) MODE="app"; APP_DIR="${2:?--app needs a dir}"; shift 2;; --proxy) MODE="proxy"; PROXY_URL="${2:?--proxy needs a url}"; shift 2;; --fpm) FPM_SOCK="${2:?}"; shift 2;; --zone) ZONE="${2:?}"; shift 2;; --cert) CERT="${2:?}"; shift 2;; --dry-run) DRY_RUN=1; shift;; *) echo "ERROR: unknown arg '$1'"; exit 2;; esac done [ -n "$MODE" ] || { echo "ERROR: choose a backend: --app or --proxy "; exit 2; } FQDN="${SUB}.${ZONE}" CERT_DIR="/etc/letsencrypt/live/${CERT}" WEBROOT="${APP_DIR:-/var/www/${ZONE}/current}/public" if [ "$MODE" = "app" ]; then BODY=$(cat <> Backed up existing vhost to $BK" fi printf '%s\n' "$VHOST" > "$AVAIL" ln -sfn "$AVAIL" "$ENABLED" if nginx -t; then systemctl reload nginx echo ">> ${FQDN} vhost active (${MODE})." else echo "ERROR: nginx config test failed — rolling back." if [ -n "${BK:-}" ] && [ -f "${BK:-}" ]; then cp -a "$BK" "$AVAIL" else rm -f "$AVAIL" "$ENABLED" fi nginx -t && systemctl reload nginx || true exit 1 fi