From c5b2c2a1ec31282818d6d981ef91a4c294f57104 Mon Sep 17 00:00:00 2001 From: Meghdad <61308304+MeghdadFadaee@users.noreply.github.com> Date: Thu, 13 Aug 2026 03:25:25 +0330 Subject: [PATCH] feat(ops): add automated server installation script --- .github/workflows/ci.yml | 4 + README.md | 32 ++ deploy/nginx/meovv-mail.conf.example | 2 +- docs/nginx-certbot.md | 36 +- scripts/install-server.sh | 515 +++++++++++++++++++++++++++ 5 files changed, 586 insertions(+), 3 deletions(-) create mode 100755 scripts/install-server.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f5c9d37..31c60f6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,6 +44,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Validate installer syntax + run: | + bash -n scripts/install-server.sh + sh -n deploy/certbot/deploy-hook.sh - name: Validate Compose configuration env: MAIL_HOSTNAME: mail.example.com diff --git a/README.md b/README.md index 41abfaa..e88fa08 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,38 @@ Direct delivery also needs working reverse DNS and acceptable IP reputation. If ## Install +For a fresh Ubuntu 24.04 or Debian 13 server, clone this repository and run the +host installer from the repository root. It installs Docker Engine with the +Compose plugin, Nginx, Certbot, and required host utilities; initializes MEOVV; +obtains the first Let's Encrypt certificate; installs renewal and proxy +configuration; and starts the appliance: + +```bash +sudo ./scripts/install-server.sh install \ + --hostname mail.example.com \ + --email admin@example.com +``` + +The hostname's A/AAAA record must already point to the server and inbound port +80 must be reachable so Let's Encrypt can issue the certificate. The installer +does not change DNS, PTR, provider firewall rules, SSH, or UFW. It detects +unmanaged Nginx hostname conflicts and conflicting Docker packages instead of +overwriting or removing them. Use `--yes` only for a reviewed, non-interactive +installation. + +Visit the printed HTTPS URL and complete the browser wizard. After creating and +testing a permanent administrator, finish the security setup: + +```bash +sudo ./scripts/install-server.sh finalize +sudo ./scripts/install-server.sh status +``` + +See [`docs/nginx-certbot.md`](docs/nginx-certbot.md) for the generated routing +and certificate layout, renewal behavior, customization, and manual setup. + +### Manual installation + Build the local operator utility and initialize the bundle: ```bash diff --git a/deploy/nginx/meovv-mail.conf.example b/deploy/nginx/meovv-mail.conf.example index b464546..8e4167d 100644 --- a/deploy/nginx/meovv-mail.conf.example +++ b/deploy/nginx/meovv-mail.conf.example @@ -1,4 +1,4 @@ -# Replace every __MAIL_HOSTNAME__ token before enabling this file. +# Managed by MEOVV Mail installer. Replace every __MAIL_HOSTNAME__ token before enabling this file. # The upstream ports are loopback-only bindings from compose.yaml. server { diff --git a/docs/nginx-certbot.md b/docs/nginx-certbot.md index 29271a6..49f7b7f 100644 --- a/docs/nginx-certbot.md +++ b/docs/nginx-certbot.md @@ -8,6 +8,36 @@ the MEOVV Compose project. Compose publishes two HTTP upstreams on loopback: Mail protocols remain directly exposed by Stalwart on 25, 465, 587, and 993. +## Automated host setup + +On a fresh supported server, the repository installer performs the host steps +documented below without putting Nginx or Certbot inside Compose: + +```bash +sudo ./scripts/install-server.sh install \ + --hostname mail.example.com \ + --email admin@example.com +``` + +Run it from the cloned repository root, or pass the repository's absolute path +with `--bundle-dir`. It supports Ubuntu 24.04 and Debian 13. Before running it, +point the hostname's A/AAAA record at the server and allow inbound port 80. The +script obtains a certificate using Certbot's webroot flow, enables the final +route split, copies the certificate into MEOVV's protected secrets directory, +installs the renewal hook, and starts both containers. + +It deliberately leaves DNS, PTR records, provider firewall rules, SSH, and UFW +to the operator. It also stops when it finds a conflicting unmanaged Nginx site +or container package rather than replacing an existing setup. After the browser +wizard and permanent administrator verification, run: + +```bash +sudo ./scripts/install-server.sh finalize +``` + +The rest of this document describes the same integration for manual installs +and explains the files managed by the installer. + ## Nginx Copy `deploy/nginx/meovv-mail.conf.example` into the host's Nginx configuration, @@ -82,8 +112,10 @@ sudo install -m 0755 \ The default hook expects the bundle at `/opt/meovv-mail`; set `MEOVV_BUNDLE_DIR` if it lives elsewhere. Certbot supplies `RENEWED_LINEAGE`. The hook copies only the certificate chain and private key, applies restrictive -permissions, and restarts Stalwart if it is running. Nginx renewal/reload remains -owned by your existing Certbot integration. +permissions, and restarts Stalwart if it is running. When installed by +`install-server.sh`, a managed wrapper also validates and reloads Nginx after a +successful copy. Manual installations should retain their existing Nginx reload +hook or add an equivalent one. Test the entire renewal path before production: diff --git a/scripts/install-server.sh b/scripts/install-server.sh new file mode 100755 index 0000000..5db33a3 --- /dev/null +++ b/scripts/install-server.sh @@ -0,0 +1,515 @@ +#!/usr/bin/env bash +set -Eeuo pipefail +IFS=$'\n\t' + +readonly SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" +readonly REPOSITORY_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd -P)" +readonly NGINX_AVAILABLE="/etc/nginx/sites-available/meovv-mail" +readonly NGINX_ENABLED="/etc/nginx/sites-enabled/meovv-mail" +readonly CERTBOT_WEBROOT="/var/www/certbot" +readonly CERTBOT_HOOK="/etc/letsencrypt/renewal-hooks/deploy/meovv-mail" +readonly MAILCTL_BIN="/usr/local/bin/mailctl" +readonly MANAGED_MARKER="Managed by MEOVV Mail installer" + +COMMAND="" +MAIL_HOSTNAME="" +ADMIN_EMAIL="" +BUNDLE_DIR="$REPOSITORY_DIR" +ASSUME_YES=false +TEMP_CONTAINER="" +TEMP_FILES=() + +log() { + printf '\n\033[1;35m==>\033[0m %s\n' "$*" +} + +warn() { + printf '\033[1;33mWARNING:\033[0m %s\n' "$*" >&2 +} + +die() { + printf '\033[1;31mERROR:\033[0m %s\n' "$*" >&2 + exit 1 +} + +cleanup() { + if [[ -n "$TEMP_CONTAINER" ]]; then + docker rm -f "$TEMP_CONTAINER" >/dev/null 2>&1 || true + fi + local temporary_file + for temporary_file in "${TEMP_FILES[@]}"; do + rm -f -- "$temporary_file" + done +} +trap cleanup EXIT + +usage() { + cat <<'EOF' +Install and operate MEOVV Mail prerequisites on Ubuntu 24.04 or Debian 13. + +Usage: + sudo ./scripts/install-server.sh install \ + --hostname mail.example.com \ + --email admin@example.com \ + [--bundle-dir /opt/meovv-mail] [--yes] + + sudo ./scripts/install-server.sh finalize \ + [--bundle-dir /opt/meovv-mail] [--yes] + + sudo ./scripts/install-server.sh status \ + [--bundle-dir /opt/meovv-mail] + +Commands: + install Install Docker, Nginx, and Certbot; initialize the bundle; obtain + TLS; install proxy/renewal configuration; and start the appliance. + finalize After completing the browser wizard and creating a permanent + administrator, register TLS with Stalwart and remove recovery access. + status Show service, certificate, and local endpoint status without changes. + +This script does not modify DNS, PTR records, provider firewalls, SSH, or UFW. +EOF +} + +parse_arguments() { + [[ $# -gt 0 ]] || { usage; exit 2; } + COMMAND="$1" + shift + + while [[ $# -gt 0 ]]; do + case "$1" in + --hostname) + [[ $# -ge 2 ]] || die "--hostname requires a value" + MAIL_HOSTNAME="$2" + shift 2 + ;; + --email) + [[ $# -ge 2 ]] || die "--email requires a value" + ADMIN_EMAIL="$2" + shift 2 + ;; + --bundle-dir) + [[ $# -ge 2 ]] || die "--bundle-dir requires a value" + BUNDLE_DIR="$2" + shift 2 + ;; + --yes|-y) + ASSUME_YES=true + shift + ;; + --help|-h) + usage + exit 0 + ;; + *) + die "unknown argument: $1" + ;; + esac + done + + case "$COMMAND" in + install|finalize|status) ;; + *) die "unknown command: $COMMAND" ;; + esac + + [[ "$BUNDLE_DIR" = /* ]] || die "--bundle-dir must be an absolute path" + [[ "$BUNDLE_DIR" != *$'\n'* ]] || die "--bundle-dir contains a newline" +} + +require_root() { + [[ ${EUID:-$(id -u)} -eq 0 ]] || die "run this command with sudo or as root" +} + +validate_hostname() { + [[ "$MAIL_HOSTNAME" =~ ^[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$ ]] || \ + die "invalid fully-qualified hostname: $MAIL_HOSTNAME" + [[ ${#MAIL_HOSTNAME} -le 253 ]] || die "hostname is too long" + MAIL_HOSTNAME="${MAIL_HOSTNAME,,}" +} + +validate_email() { + [[ "$ADMIN_EMAIL" =~ ^[^[:space:]@]+@[^[:space:]@]+\.[^[:space:]@]+$ ]] || \ + die "invalid email address: $ADMIN_EMAIL" +} + +require_bundle() { + [[ -f "$BUNDLE_DIR/compose.yaml" ]] || die "compose.yaml not found in $BUNDLE_DIR" + [[ -f "$BUNDLE_DIR/Dockerfile" ]] || die "Dockerfile not found in $BUNDLE_DIR" + [[ -f "$BUNDLE_DIR/deploy/nginx/meovv-mail.conf.example" ]] || die "Nginx template is missing" + [[ -x "$BUNDLE_DIR/deploy/certbot/deploy-hook.sh" ]] || die "Certbot hook is missing or not executable" +} + +confirm_install() { + $ASSUME_YES && return + [[ -t 0 ]] || die "non-interactive use requires --yes" + + cat </dev/null 2>&1 && docker compose version >/dev/null 2>&1; then + log "Docker Engine and Compose are already installed" + systemctl enable --now docker + return + fi + + if command -v docker >/dev/null 2>&1; then + die "Docker is installed without a working Compose plugin; resolve that installation before continuing" + fi + + local conflicting_packages=() package + for package in docker.io docker-compose docker-compose-v2 podman-docker containerd runc; do + if dpkg-query -W -f='${Status}' "$package" 2>/dev/null | grep -Fq 'install ok installed'; then + conflicting_packages+=("$package") + fi + done + if (( ${#conflicting_packages[@]} > 0 )); then + die "conflicting container packages are installed (${conflicting_packages[*]}); review them manually before installing Docker CE" + fi + + log "Installing Docker Engine from Docker's official repository" + install -m 0755 -d /etc/apt/keyrings + local docker_key + docker_key="$(mktemp /tmp/meovv-docker.XXXXXX.asc)" + TEMP_FILES+=("$docker_key") + curl -fsSL "https://download.docker.com/linux/$OS_ID/gpg" -o "$docker_key" + gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg "$docker_key" + chmod a+r /etc/apt/keyrings/docker.gpg + + local architecture + architecture="$(dpkg --print-architecture)" + printf 'deb [arch=%s signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/%s %s stable\n' \ + "$architecture" "$OS_ID" "$OS_CODENAME" \ + > /etc/apt/sources.list.d/docker.list + + apt-get update + apt-get install -y --no-install-recommends \ + containerd.io \ + docker-buildx-plugin \ + docker-ce \ + docker-ce-cli \ + docker-compose-plugin + systemctl enable --now docker + docker compose version >/dev/null +} + +assert_hostname_is_available() { + local conflict="" + if [[ -d /etc/nginx/sites-enabled ]]; then + while IFS= read -r candidate; do + [[ -e "$candidate" ]] || continue + [[ "$(readlink -f "$candidate")" == "$(readlink -f "$NGINX_AVAILABLE" 2>/dev/null || printf '%s' "$NGINX_AVAILABLE")" ]] && continue + if grep -Eq "^[[:space:]]*server_name[[:space:]][^;]*\b${MAIL_HOSTNAME//./\.}\b" "$candidate"; then + conflict="$candidate" + break + fi + done < <(find /etc/nginx/sites-enabled -maxdepth 1 \( -type f -o -type l \) 2>/dev/null) + fi + [[ -z "$conflict" ]] || die "$MAIL_HOSTNAME is already configured by $conflict; merge or disable that site manually" + + if [[ -e "$NGINX_AVAILABLE" ]] && ! grep -Fq "$MANAGED_MARKER" "$NGINX_AVAILABLE"; then + die "$NGINX_AVAILABLE exists but is not managed by this installer" + fi +} + +install_challenge_site() { + install -d -m 0755 "$CERTBOT_WEBROOT" + install -d -m 0755 /etc/nginx/sites-available /etc/nginx/sites-enabled + + if [[ -r "/etc/letsencrypt/live/$MAIL_HOSTNAME/fullchain.pem" ]] && \ + [[ -r "/etc/letsencrypt/live/$MAIL_HOSTNAME/privkey.pem" ]] && \ + [[ -f "$NGINX_AVAILABLE" ]] && grep -Fq "listen 443 ssl" "$NGINX_AVAILABLE"; then + log "Keeping the existing managed HTTPS site during certificate renewal" + ln -sfn "$NGINX_AVAILABLE" "$NGINX_ENABLED" + nginx -t + systemctl enable --now nginx + systemctl reload nginx + return + fi + + log "Preparing the initial ACME challenge site" + cat > "$NGINX_AVAILABLE" < "$NGINX_AVAILABLE" + ln -sfn "$NGINX_AVAILABLE" "$NGINX_ENABLED" + nginx -t + systemctl reload nginx +} + +compose() { + docker compose --project-directory "$BUNDLE_DIR" -f "$BUNDLE_DIR/compose.yaml" "$@" +} + +build_and_install_mailctl() { + log "Building the pinned MEOVV application image" + MAIL_HOSTNAME="$MAIL_HOSTNAME" MEOVV_VERSION=0.1.0 compose build meovv + + local image_id temporary_binary + image_id="$(MAIL_HOSTNAME="$MAIL_HOSTNAME" MEOVV_VERSION=0.1.0 compose images -q meovv)" + [[ -n "$image_id" ]] || die "the MEOVV image was not created" + + TEMP_CONTAINER="$(docker create "$image_id")" + temporary_binary="$(mktemp /tmp/meovv-mailctl.XXXXXX)" + TEMP_FILES+=("$temporary_binary") + docker cp "$TEMP_CONTAINER:/usr/local/bin/mailctl" "$temporary_binary" + install -m 0755 "$temporary_binary" "$MAILCTL_BIN" + rm -f "$temporary_binary" + docker rm "$TEMP_CONTAINER" >/dev/null + TEMP_CONTAINER="" +} + +initialize_bundle() { + if [[ -f "$BUNDLE_DIR/.env" ]]; then + local configured_hostname + configured_hostname="$(sed -n 's/^MAIL_HOSTNAME=//p' "$BUNDLE_DIR/.env" | tail -n 1)" + [[ "$configured_hostname" == "$MAIL_HOSTNAME" ]] || \ + die "$BUNDLE_DIR/.env belongs to $configured_hostname, not $MAIL_HOSTNAME" + log "The appliance is already initialized" + else + log "Initializing appliance secrets" + "$MAILCTL_BIN" init --directory "$BUNDLE_DIR" --hostname "$MAIL_HOSTNAME" + fi + + # The MEOVV image runs with group 2001. Keep secrets unreadable to other + # host users while allowing the application process to read its inputs. + chown -R root:2001 "$BUNDLE_DIR/secrets" + find "$BUNDLE_DIR/secrets" -type d -exec chmod 0750 {} + + find "$BUNDLE_DIR/secrets" -type f -exec chmod 0640 {} + + chmod 0600 "$BUNDLE_DIR/.env" +} + +copy_certificate_to_stalwart() { + log "Copying the Certbot certificate into the protected appliance directory" + MEOVV_BUNDLE_DIR="$BUNDLE_DIR" \ + RENEWED_LINEAGE="/etc/letsencrypt/live/$MAIL_HOSTNAME" \ + "$BUNDLE_DIR/deploy/certbot/deploy-hook.sh" + + chown root:2001 "$BUNDLE_DIR/secrets/tls/fullchain.pem" "$BUNDLE_DIR/secrets/tls/privkey.pem" + chmod 0644 "$BUNDLE_DIR/secrets/tls/fullchain.pem" + chmod 0640 "$BUNDLE_DIR/secrets/tls/privkey.pem" +} + +install_certbot_hook() { + log "Installing the Certbot deploy hook" + install -d -m 0755 "$(dirname "$CERTBOT_HOOK")" + local escaped_bundle escaped_hook + printf -v escaped_bundle '%q' "$BUNDLE_DIR" + printf -v escaped_hook '%q' "$BUNDLE_DIR/deploy/certbot/deploy-hook.sh" + cat > "$CERTBOT_HOOK" </dev/null; then + return + fi + sleep 2 + done + compose ps + die "MEOVV did not become live within 60 seconds; inspect 'docker compose logs'" +} + +install_command() { + require_root + [[ -n "$MAIL_HOSTNAME" ]] || die "install requires --hostname" + [[ -n "$ADMIN_EMAIL" ]] || die "install requires --email" + validate_hostname + validate_email + require_bundle + confirm_install + detect_platform + install_base_packages + assert_hostname_is_available + install_docker + build_and_install_mailctl + initialize_bundle + install_challenge_site + obtain_certificate + copy_certificate_to_stalwart + install_certbot_hook + install_final_nginx_site + start_appliance + + cat <