nomadro/apps/ebook/deploy/install_backend.sh
eric d93a355fd5 Migrate navmini/ebook/nomadweb into apps/ as same-origin portal subpaths.
Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 03:30:34 -05:00

125 lines
3.7 KiB
Bash

#!/usr/bin/env bash
# Install PocketBase + FastAPI backend on VPS
set -euo pipefail
APP_DIR="/var/www/ebook"
BACKEND_DIR="$APP_DIR/backend"
PB_DIR="/opt/pocketbase"
PB_VERSION="0.25.9"
# ── PocketBase ──────────────────────────────────────────────
echo "==> PocketBase setup"
if [ ! -f "$PB_DIR/pocketbase" ]; then
mkdir -p "$PB_DIR"
cd /tmp
wget -q "https://github.com/pocketbase/pocketbase/releases/download/v${PB_VERSION}/pocketbase_${PB_VERSION}_linux_amd64.zip" -O pb.zip
unzip -o pb.zip -d "$PB_DIR"
rm pb.zip
echo " installed PocketBase $PB_VERSION"
else
echo " PocketBase already installed"
fi
# systemd service for PocketBase
cat > /etc/systemd/system/pocketbase.service <<'SVC'
[Unit]
Description=PocketBase
After=network.target
[Service]
Type=simple
ExecStart=/opt/pocketbase/pocketbase serve --http=127.0.0.1:8090
WorkingDirectory=/opt/pocketbase
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
SVC
systemctl daemon-reload
systemctl enable pocketbase
systemctl restart pocketbase
sleep 2
echo " PocketBase status: $(systemctl is-active pocketbase)"
# ── Ensure superuser exists (for creating collections / admin APIs) ──
echo "==> PocketBase superuser upsert"
BACKEND_ENV_FILE="$BACKEND_DIR/.env"
PB_ADMIN_EMAIL=""
PB_ADMIN_PASSWORD=""
if [ -f "$BACKEND_ENV_FILE" ]; then
while IFS='=' read -r k v; do
k="$(echo "$k" | tr -d '\r')"
v="$(echo "$v" | tr -d '\r')"
if [ "$k" = "PB_ADMIN_EMAIL" ]; then PB_ADMIN_EMAIL="$v"; fi
if [ "$k" = "PB_ADMIN_PASSWORD" ]; then PB_ADMIN_PASSWORD="$v"; fi
done < "$BACKEND_ENV_FILE"
fi
if [ -n "$PB_ADMIN_EMAIL" ] && [ -n "$PB_ADMIN_PASSWORD" ]; then
/opt/pocketbase/pocketbase superuser upsert "$PB_ADMIN_EMAIL" "$PB_ADMIN_PASSWORD" >/dev/null 2>&1 || true
echo " superuser ensured: $PB_ADMIN_EMAIL"
else
echo " WARN: PB_ADMIN_EMAIL/PB_ADMIN_PASSWORD missing in backend/.env"
fi
# ── Python + FastAPI ────────────────────────────────────────
echo "==> Python + FastAPI setup"
if ! command -v python3 >/dev/null 2>&1; then
apt-get update -qq
apt-get install -y -qq python3 python3-pip python3-venv >/dev/null 2>&1
fi
cd "$BACKEND_DIR"
if [ ! -d "venv" ]; then
python3 -m venv venv
fi
source venv/bin/activate
pip install -q --upgrade pip
pip install -q -r requirements.txt
# systemd service for FastAPI
cat > /etc/systemd/system/ebook-api.service <<SVC
[Unit]
Description=Ebook FastAPI Backend
After=network.target pocketbase.service
[Service]
Type=simple
WorkingDirectory=$BACKEND_DIR
ExecStart=$BACKEND_DIR/venv/bin/uvicorn app.main:app --host 127.0.0.1 --port 8010
EnvironmentFile=$BACKEND_DIR/.env
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
SVC
fuser -k 8010/tcp 2>/dev/null || true
fuser -k 8090/tcp 2>/dev/null || true
systemctl daemon-reload
systemctl enable ebook-api
systemctl restart pocketbase
systemctl restart ebook-api
sleep 2
echo " FastAPI status: $(systemctl is-active ebook-api)"
# ── PocketBase schema + seed payment providers ───────────────
echo "==> PocketBase schema"
cd "$BACKEND_DIR"
source venv/bin/activate
python -m app.pb_schema || echo " WARN: pb_schema failed"
# ── Health check ────────────────────────────────────────────
echo "==> Health checks"
curl -sf http://127.0.0.1:8090/api/health || echo " WARN: PocketBase not responding"
curl -sf http://127.0.0.1:8010/api/health || echo " WARN: FastAPI not responding"
echo ""
echo "==> Backend deploy OK"