#!/bin/bash # StatSphere - Ubuntu 24.04 LXC Install Script # Run as root or with sudo set -e APP_DIR="/opt/statsphere" APP_USER="statsphere" NODE_VERSION="20" echo "============================================" echo " StatSphere Athlete Stats Platform Installer" echo "============================================" # 1. System update echo "[1/8] Updating system packages..." apt-get update -qq apt-get install -y -qq curl git nginx # 2. Install Node.js 20 LTS via NodeSource echo "[2/8] Installing Node.js ${NODE_VERSION}..." curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash - apt-get install -y -qq nodejs echo " Node: $(node --version)" echo " NPM: $(npm --version)" # 3. Create app user echo "[3/8] Creating app user..." id -u $APP_USER &>/dev/null || useradd -r -s /bin/false -d $APP_DIR $APP_USER # 4. Copy app files echo "[4/8] Setting up application directory..." mkdir -p $APP_DIR cp -r . $APP_DIR/ chown -R $APP_USER:$APP_USER $APP_DIR # 5. Install dependencies and build echo "[5/8] Installing Node dependencies (this may take 2-3 minutes)..." cd $APP_DIR sudo -u $APP_USER npm install echo "[6/8] Building production bundle..." sudo -u $APP_USER npm run build # 6. Configure Nginx echo "[7/8] Configuring Nginx..." cat > /etc/nginx/sites-available/statsphere <<'NGINX' server { listen 80; listen [::]:80; server_name _; root /opt/statsphere/dist; index index.html; # Gzip gzip on; gzip_vary on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml; # Cache static assets location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2|woff)$ { expires 1y; add_header Cache-Control "public, immutable"; } # Service worker - no cache location = /sw.js { add_header Cache-Control "no-cache"; } # SPA fallback - all routes serve index.html location / { try_files $uri $uri/ /index.html; } } NGINX # Enable site ln -sf /etc/nginx/sites-available/statsphere /etc/nginx/sites-enabled/ rm -f /etc/nginx/sites-enabled/default nginx -t systemctl restart nginx systemctl enable nginx # 7. Setup systemd service for dev server (optional) echo "[8/8] Setting up systemd service (dev preview mode)..." cat > /etc/systemd/system/statsphere-dev.service <<'SERVICE' [Unit] Description=StatSphere Development Server After=network.target [Service] Type=simple User=statsphere WorkingDirectory=/opt/statsphere ExecStart=/usr/bin/npm run preview Restart=on-failure RestartSec=5 Environment=NODE_ENV=production [Install] WantedBy=multi-user.target SERVICE systemctl daemon-reload echo "" echo "============================================" echo " Installation Complete!" echo "============================================" echo "" echo " Production (Nginx): http://$(hostname -I | awk '{print $1}')" echo "" echo " To rebuild after changes:" echo " cd $APP_DIR && npm run build" echo "" echo " To update the app:" echo " cd $APP_DIR" echo " git pull (if using git)" echo " npm install" echo " npm run build" echo " systemctl restart nginx" echo "" echo " Dev server (optional, port 4173):" echo " systemctl start statsphere-dev" echo " systemctl enable statsphere-dev" echo ""