#!/bin/bash # Deployment script for Mnemo Cards Web App # Usage: ./deploy.sh set -e # Load configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/config.sh" echo "🚀 Starting deployment of $APP_TITLE..." # Check if we're in the right directory check_project_root # Build the Flutter web app for production build_flutter_app print_status "Uploading files to server using rsync..." # Upload files directly using rsync (much faster and more reliable) rsync -avz --delete build/web/ "$SERVER_USER@$SERVER_IP:$WEB_ROOT/" print_status "Uploading nginx configuration..." # Upload nginx config separately scp deploy/nginx.conf "$SERVER_USER@$SERVER_IP:/tmp/nginx.conf" print_status "Deploying on server..." # Execute deployment commands on server ssh "$SERVER_USER@$SERVER_IP" << EOF set -e # Create web directory if it doesn't exist mkdir -p $WEB_ROOT # Backup existing deployment if [ -d "$WEB_ROOT" ] && [ "\$(ls -A $WEB_ROOT)" ]; then echo "Creating backup of existing deployment..." cp -r $WEB_ROOT $BACKUP_DIR.\$(date +%Y%m%d_%H%M%S) fi # Set proper permissions chown -R $WEB_USER:$WEB_GROUP $WEB_ROOT chmod -R $WEB_PERMISSIONS $WEB_ROOT # Install SSL certificate - try Let's Encrypt first if [ ! -d "/etc/letsencrypt/live/memo-cards.online" ]; then echo "🔐 Attempting to get Let's Encrypt SSL certificate for memo-cards.online..." # Stop nginx temporarily for certificate issuance systemctl stop nginx 2>/dev/null || true if certbot certonly --standalone -d memo-cards.online --non-interactive --agree-tos --email $LETSENCRYPT_EMAIL; then echo "✅ Let's Encrypt certificate obtained successfully!" else echo "❌ Failed to get Let's Encrypt certificate. Generating self-signed certificate..." if [ ! -f "$SSL_SELF_CERT" ]; then openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ -keyout $SSL_SELF_KEY \ -out $SSL_SELF_CERT \ -subj "/C=RU/ST=Moscow/L=Moscow/O=MnemoCards/OU=IT/CN=memo-cards.online" fi fi # Start nginx again systemctl start nginx 2>/dev/null || true else echo "✅ Let's Encrypt certificate already exists for memo-cards.online" fi # Configure nginx echo "Configuring nginx..." # Install nginx if not installed if ! command -v nginx &> /dev/null; then apt update apt install -y nginx fi # Copy nginx configuration cp /tmp/nginx.conf $NGINX_CONFIG # Enable site ln -sf $NGINX_CONFIG $NGINX_ENABLED # Remove default nginx site if it exists rm -f /etc/nginx/sites-enabled/default # Test nginx configuration nginx -t # Restart nginx systemctl restart nginx systemctl enable nginx # Configure firewall ufw allow '$FIREWALL_ALLOW_NGINX' ufw allow $FIREWALL_ALLOW_SSH ufw --force enable echo "Deployment completed successfully!" echo "Application is available at: https://memo-cards.online" EOF print_success "Deployment completed successfully! 🎉" print_success "Your app is now available at: https://memo-cards.online" print_info "SSL certificate: Let's Encrypt (preferred) or self-signed (fallback)"