#!/bin/sh set -eu scratch=$(mktemp -d) tar --extract --file "$1" --directory "$scratch" --no-same-owner --no-same-permissions account_id=$(jq -er '.account_id' "$scratch/site.json") domain=$(jq -er '.domain' "$scratch/site.json") case "$account_id" in ''|*[!0-9]*) echo 'Invalid hosting account ID.' >&2; exit 1 ;; esac case "$domain" in ''|*[!a-z0-9.-]*) echo 'Invalid primary domain.' >&2; exit 1 ;; esac account_dir="/srv/coulobre/web/$account_id" site_user="coulobre-$account_id" site_conf="$site_user.conf" useradd --system --user-group --no-create-home --home-dir "$account_dir" \ --shell /usr/sbin/nologin "$site_user" install -d -m 0755 "$account_dir" /run/php install -d -m 0750 -o "$site_user" -g www-data "$account_dir/public" install -m 0640 -o "$site_user" -g www-data \ "$scratch/public/index.php" "$account_dir/public/index.php" install -m 0644 "$scratch/apache.conf" "/etc/apache2/sites-available/$site_conf" a2enmod proxy_fcgi >/dev/null a2dissite 000-default >/dev/null a2ensite "$site_conf" >/dev/null printf 'ServerName localhost\n' > /etc/apache2/conf-enabled/coulobre-smoke.conf printf '[global]\nerror_log = /proc/self/fd/2\ninclude = %s\n' \ "$scratch/php-fpm.conf" > "$scratch/fpm.conf" php-fpm8.5 --test --fpm-config "$scratch/fpm.conf" apache2ctl configtest php-fpm8.5 --daemonize --fpm-config "$scratch/fpm.conf" apache2ctl start body=$(curl --fail --silent --show-error --noproxy '*' --max-time 5 \ --retry 5 --retry-connrefused --header "Host: $domain" http://127.0.0.1/index.php) if [ "$body" != 'Coulobre PHP-FPM smoke check passed.' ]; then echo 'The test site did not return the expected PHP response.' >&2 exit 1 fi printf '%s via %s\n' "$body" "$domain"