#!/bin/bash # One-time setup of the sync server on a Debian/Ubuntu machine with Apache + PHP + certbot, run as # root from the folder the server was copied to (e.g. /srv/dash-sync — anywhere outside other sites): # # sudo bash /srv/dash-sync/bin/setup-server.sh [ …] # sudo bash /srv/dash-sync/bin/setup-server.sh sync.example.com me # # It makes data/ (the web server's, 700), the Apache site (own file, nothing existing is edited), # the Let's Encrypt certificate, the account and its first device (default: dashboard) — whose # token it prints once, here in your terminal only. Phones and other dashboards get theirs from the # dashboard: Settings ▸ General ▸ Devices (a QR code). Safe to run again: existing pieces are left alone. set -euo pipefail [ $# -ge 2 ] || { sed -n '2,10p' "$0"; exit 2; } HOST=$1; ACCOUNT=$2; shift 2 if [ $# -gt 0 ]; then DEVICES=("$@"); else DEVICES=(dashboard); fi DIR=$(cd "$(dirname "$0")/.." && pwd) SITE=/etc/apache2/sites-available/dash-sync-$HOST.conf LOGS='${APACHE_LOG_DIR}' [ "$(id -u)" = 0 ] || { echo "run it with sudo"; exit 1; } case "$DIR" in /var/www/html|/var/www/html/*) echo "copy it outside another site's folder (e.g. /srv/dash-sync)"; exit 1 ;; esac cd "$DIR" [ -f config.php ] || cp config.example.php config.php mkdir -p data chown -R www-data:www-data data chmod 700 data if [ ! -f "$SITE" ]; then cat > "$SITE" < ServerName $HOST DocumentRoot $DIR/public AllowOverride All Require all granted LimitRequestBody 9437184 ErrorLog $LOGS/dash-sync.error.log CustomLog $LOGS/dash-sync.access.log combined EOF echo "site written: $SITE" fi a2enmod -q rewrite >/dev/null # public/.htaccess: every request → index.php a2ensite -q "dash-sync-$HOST" >/dev/null apache2ctl configtest systemctl reload apache2 if [ ! -d /etc/letsencrypt/live/$HOST ]; then certbot --apache -n --redirect -d $HOST fi admin() { sudo -u www-data php "$DIR/bin/admin.php" "$@"; } admin add-account "$ACCOUNT" >/dev/null 2>&1 || true # already there on a second run for dev in "${DEVICES[@]}"; do if ! admin list 2>/dev/null | grep -qE "^$ACCOUNT +$dev "; then echo; echo "== token for $dev (shown once — keep it for its setup):" admin add-device "$ACCOUNT" "$dev" fi done echo; echo "== check (should say 401 without a token):" curl -s -o /dev/null -w "%{http_code}\n" https://$HOST/v1/hello || echo "not reachable yet (DNS / certificate?)"