#!/bin/bash # University Portal Production Deployment Script # This script sets up the application for production deployment set -e echo "🚀 Starting University Portal Production Deployment..." # Check if running as root if [ "$EUID" -eq 0 ]; then echo "❌ Please don't run this script as root" exit 1 fi # Check if Node.js is installed if ! command -v node &> /dev/null; then echo "❌ Node.js is not installed. Please install Node.js 18+ first." exit 1 fi # Check if npm is installed if ! command -v npm &> /dev/null; then echo "❌ npm is not installed. Please install npm first." exit 1 fi echo "✅ Node.js and npm are installed" # Install dependencies echo "📦 Installing dependencies..." npm ci --only=production # Build the application echo "🔨 Building the application..." npm run build # Set up environment variables echo "⚙️ Setting up environment variables..." if [ ! -f .env.local ]; then cat > .env.local << EOF # Production Environment Configuration NODE_ENV=production DATABASE_URL=file:./prod.db JWT_SECRET=$(openssl rand -base64 32) OLLAMA_HOST=http://localhost:11434 OLLAMA_MODEL=llama2 NEXT_PUBLIC_APP_URL=https://your-domain.com EOF echo "✅ Created .env.local file" else echo "⚠️ .env.local already exists, skipping creation" fi # Set up database echo "🗄️ Setting up database..." npx prisma migrate deploy npx prisma generate # Seed database if needed read -p "Do you want to seed the database with sample data? (y/N): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then echo "🌱 Seeding database..." npx prisma db seed fi # Set up PM2 for process management (if available) if command -v pm2 &> /dev/null; then echo "📋 Setting up PM2 process manager..." cat > ecosystem.config.js << EOF module.exports = { apps: [{ name: 'university-portal', script: 'npm', args: 'start', instances: 'max', exec_mode: 'cluster', env: { NODE_ENV: 'production', PORT: 3000 }, error_file: './logs/err.log', out_file: './logs/out.log', log_file: './logs/combined.log', time: true }] } EOF mkdir -p logs echo "✅ PM2 configuration created" else echo "⚠️ PM2 not found. Consider installing it for production process management:" echo " npm install -g pm2" fi # Create systemd service (if running as root or with sudo) if [ "$EUID" -eq 0 ]; then echo "🔧 Creating systemd service..." cat > /etc/systemd/system/university-portal.service << EOF [Unit] Description=University Portal After=network.target [Service] Type=simple User=$SUDO_USER WorkingDirectory=$(pwd) ExecStart=/usr/bin/npm start Restart=always RestartSec=10 Environment=NODE_ENV=production [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable university-portal echo "✅ Systemd service created and enabled" fi # Set up Nginx configuration (if available) if command -v nginx &> /dev/null; then echo "🌐 Setting up Nginx configuration..." cat > nginx.conf << EOF server { listen 80; server_name your-domain.com www.your-domain.com; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade \$http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; proxy_cache_bypass \$http_upgrade; } # Static files location /_next/static { alias $(pwd)/.next/static; expires 1y; add_header Cache-Control "public, immutable"; } # Security headers add_header X-Frame-Options "SAMEORIGIN" always; add_header X-XSS-Protection "1; mode=block" always; add_header X-Content-Type-Options "nosniff" always; add_header Referrer-Policy "no-referrer-when-downgrade" always; add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always; } EOF echo "✅ Nginx configuration created" echo "📝 Copy nginx.conf to /etc/nginx/sites-available/ and enable the site" fi # Set up SSL with Let's Encrypt (if certbot is available) if command -v certbot &> /dev/null; then echo "🔒 Setting up SSL certificate..." echo "📝 Run the following command to get SSL certificate:" echo " sudo certbot --nginx -d your-domain.com -d www.your-domain.com" fi # Create startup script cat > start.sh << 'EOF' #!/bin/bash # University Portal Startup Script echo "🚀 Starting University Portal..." # Check if Ollama is running if ! curl -s http://localhost:11434/api/tags > /dev/null; then echo "⚠️ Ollama is not running. Starting Ollama..." ollama serve & sleep 5 fi # Start the application if command -v pm2 &> /dev/null; then pm2 start ecosystem.config.js pm2 save pm2 startup else npm start fi echo "✅ University Portal started successfully!" EOF chmod +x start.sh # Create maintenance script cat > maintenance.sh << 'EOF' #!/bin/bash # University Portal Maintenance Script echo "🔧 University Portal Maintenance Mode" case "$1" in backup) echo "📦 Creating database backup..." cp prod.db "backup-$(date +%Y%m%d-%H%M%S).db" echo "✅ Backup created" ;; update) echo "🔄 Updating application..." git pull npm ci --only=production npm run build npx prisma migrate deploy npx prisma generate echo "✅ Application updated" ;; logs) echo "📋 Showing logs..." if command -v pm2 &> /dev/null; then pm2 logs university-portal else tail -f logs/combined.log fi ;; restart) echo "🔄 Restarting application..." if command -v pm2 &> /dev/null; then pm2 restart university-portal else pkill -f "npm start" || true npm start & fi echo "✅ Application restarted" ;; *) echo "Usage: $0 {backup|update|logs|restart}" exit 1 ;; esac EOF chmod +x maintenance.sh echo "" echo "🎉 Production deployment setup completed!" echo "" echo "📋 Next steps:" echo "1. Update .env.local with your production settings" echo "2. Configure your domain in nginx.conf" echo "3. Set up SSL certificate with Let's Encrypt" echo "4. Start the application: ./start.sh" echo "5. Monitor logs: ./maintenance.sh logs" echo "" echo "🔧 Maintenance commands:" echo " ./maintenance.sh backup - Create database backup" echo " ./maintenance.sh update - Update application" echo " ./maintenance.sh logs - View logs" echo " ./maintenance.sh restart - Restart application" echo "" echo "🌐 Access your application at: http://localhost:3000" echo "📚 Documentation: README.md"