#!/bin/bash set -e DOMAIN="${1:-192.168.2.175:3000}" export PATH="$HOME/.local/node/bin:$PATH" echo "=== FairReview Deployment Script ===" echo "Target domain: $DOMAIN" echo "" # Check if running as root or with sudo if [ "$EUID" -eq 0 ]; then echo "Warning: Running as root is not recommended. Use a regular user with sudo." echo "" fi # Load database credentials if [ ! -f .postgres-env ]; then echo "Error: .postgres-env file not found!" echo "Please create .postgres-env with your database credentials." exit 1 fi source .postgres-env # Export database credentials for Node.js scripts export DB_HOST DB_PORT DB_USER DB_PASS DB_NAME # Validate credentials if [ -z "$DB_PASS" ] || [ "$DB_PASS" = "your_password_here" ]; then echo "Error: Please update .postgres-env with your actual database password." exit 1 fi echo "Database configuration:" echo " Host: $DB_HOST:$DB_PORT" echo " Database: $DB_NAME" echo " User: $DB_USER" echo "" # Install Node.js 18.x echo "=== Installing Node.js 18.x ===" if ! command -v node &> /dev/null; then curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt install -y nodejs else echo "Node.js already installed: $(node --version)" fi # Install required packages echo "" echo "=== Installing required packages ===" sudo apt update sudo apt install -y curl build-essential git postgresql-client # Install PM2 globally echo "" echo "=== Installing PM2 ===" if ! command -v pm2 &> /dev/null; then sudo npm install -g pm2 else echo "PM2 already installed: $(pm2 --version)" fi # Install npm dependencies echo "" echo "=== Installing npm dependencies ===" npm install npm install next-auth@beta @prisma/client bcryptjs npm install -D prisma @types/bcryptjs # Create .env.local echo "" echo "=== Creating environment configuration ===" NEXTAUTH_SECRET=$(openssl rand -base64 32) cat > .env.local << EOF DATABASE_URL="postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=require" NEXTAUTH_SECRET="${NEXTAUTH_SECRET}" NEXTAUTH_URL="http://${DOMAIN}" STEAM_API_KEY="YOUR_STEAM_WEB_API_KEY" EOF echo "Environment file created: .env.local" echo " NEXTAUTH_URL: http://${DOMAIN}" echo " Database: ${DB_HOST}:${DB_PORT}/${DB_NAME}" echo "" echo "IMPORTANT: Update STEAM_API_KEY in .env.local with your Steam Web API key!" # Generate Prisma client echo "" echo "=== Setting up database ===" npx prisma generate # Create database if it doesn't exist echo "" echo "=== Creating database if not exists ===" node scripts/create-db.js # Push schema to database export DATABASE_URL="postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=require" npx prisma db push # Build for production echo "" echo "=== Building for production ===" npm run build # Configure PM2 echo "" echo "=== Starting application with PM2 ===" pm2 stop fairreview 2>/dev/null || true pm2 delete fairreview 2>/dev/null || true pm2 start npm --name "fairreview" -- start -- -H 0.0.0.0 # Save PM2 config for restart on reboot pm2 save # Setup PM2 startup (if systemd is available) if command -v systemctl &> /dev/null; then sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u $USER --hp $HOME 2>/dev/null || true fi echo "" echo "=== Deployment Complete ===" echo "Application running at: http://${DOMAIN}" echo "" echo "Useful commands:" echo " pm2 status - Check application status" echo " pm2 logs - View application logs" echo " pm2 restart fairreview - Restart application"