#!/bin/bash # ScriptShare Production Deployment with Database set -e echo "🚀 Deploying ScriptShare with Database..." # Check if Docker and Docker Compose are available if ! command -v docker &> /dev/null; then echo "❌ Docker is not installed. Please install Docker first." exit 1 fi if ! docker compose version &> /dev/null && ! command -v docker-compose &> /dev/null; then echo "❌ Docker Compose is not installed. Please install Docker Compose first." exit 1 fi # Check if environment file exists if [ ! -f "env.production.example" ]; then echo "❌ Environment example file 'env.production.example' not found." exit 1 fi # Copy environment file if it doesn't exist if [ ! -f ".env" ]; then echo "📋 Creating .env file from example..." cp env.production.example .env echo "⚠️ Please edit .env file with your production settings before continuing!" echo " - Update database passwords" echo " - Set your domain URL" echo " - Change JWT secret" read -p "Press Enter after editing .env file..." fi # Create necessary directories echo "📁 Creating required directories..." mkdir -p logs mkdir -p backups # Pull latest images echo "📥 Pulling Docker images..." docker compose -f docker-compose.production.yml pull mysql:8.0 # Build application images echo "🔨 Building application images..." docker compose -f docker-compose.production.yml build --no-cache # Stop existing containers if running echo "🛑 Stopping existing containers..." docker compose -f docker-compose.production.yml down # Create Docker network if it doesn't exist echo "🌐 Setting up Docker network..." docker network create scriptshare-network 2>/dev/null || echo "Network already exists" # Start the database first echo "🗄️ Starting database..." docker compose -f docker-compose.production.yml up -d scriptshare-db # Wait for database to be ready echo "⏳ Waiting for database to be ready..." sleep 20 # Check database health echo "🏥 Checking database health..." until docker compose -f docker-compose.production.yml exec -T scriptshare-db mysqladmin ping -h"localhost" -u"root" -p"${DB_ROOT_PASSWORD:-ScriptShare_Root_2024_Secure}" --silent; do echo "Database is starting up - waiting..." sleep 5 done echo "✅ Database is ready!" # Start API server echo "🚀 Starting API server..." docker compose -f docker-compose.production.yml up -d scriptshare-api # Wait for API to be ready echo "⏳ Waiting for API to be ready..." sleep 30 # Start frontend echo "🌐 Starting frontend..." docker compose -f docker-compose.production.yml up -d scriptshare-frontend # Wait for all services to be healthy echo "🏥 Checking service health..." sleep 30 # Check service status echo "📊 Checking service status..." services=("scriptshare-db" "scriptshare-api" "scriptshare-frontend") for service in "${services[@]}"; do if docker compose -f docker-compose.production.yml ps | grep -q "$service.*Up"; then echo "✅ $service is running" else echo "❌ $service failed to start" echo "Checking logs for $service:" docker compose -f docker-compose.production.yml logs "$service" exit 1 fi done # Display deployment information echo "" echo "🎉 ScriptShare deployment completed successfully!" echo "" echo "📊 Service URLs:" API_PORT=$(grep API_PORT .env | cut -d'=' -f2 | tr -d '"' || echo "3001") FRONTEND_PORT=$(grep FRONTEND_PORT .env | cut -d'=' -f2 | tr -d '"' || echo "80") echo " Frontend: http://localhost:${FRONTEND_PORT}" echo " API: http://localhost:${API_PORT}/api/health" echo " Database: localhost:3306" echo "" echo "🔧 Management commands:" echo " View logs: docker compose -f docker-compose.production.yml logs -f" echo " Stop: docker compose -f docker-compose.production.yml down" echo " Restart: docker compose -f docker-compose.production.yml restart" echo " Database shell: docker compose -f docker-compose.production.yml exec scriptshare-db mysql -u scriptshare_user -p scriptshare" echo "" echo "📝 Next steps:" echo " 1. Configure your domain DNS to point to this server" echo " 2. Set up SSL/HTTPS if needed" echo " 3. Configure automated backups" echo " 4. Set up monitoring and alerting"