#!/usr/bin/env bash # ============================================================================== # NourishGlobal - Live VPS Deployment Script for https://food.jackpilot.co # ============================================================================== # Usage: # bash deploy.sh [VPS_HOST_OR_IP] [SSH_USER] [SSH_PORT] [SSH_KEY_PATH] # Examples: # bash deploy.sh 192.168.1.100 root # bash deploy.sh food.jackpilot.co root 22 ~/.ssh/id_rsa # bash deploy.sh (interactive mode will prompt for parameters) # ============================================================================== set -euo pipefail # Text formatting BOLD="\033[1m" GREEN="\033[0;32m" BLUE="\033[0;34m" YELLOW="\033[1;33m" RED="\033[0;31m" CYAN="\033[0;36m" RESET="\033[0m" # Default Target Configuration TARGET_DOMAIN="food.jackpilot.co" DEFAULT_REMOTE_DIR="/var/www/food.jackpilot.co" DEFAULT_USER="root" DEFAULT_PORT="22" echo -e "${CYAN}${BOLD}" echo "================================================================================" echo " 🚀 NourishGlobal VPS Live Deployment: https://${TARGET_DOMAIN}" echo "================================================================================" echo -e "${RESET}" # 1. Gather Connection Details VPS_HOST="${1:-}" SSH_USER="${2:-}" SSH_PORT="${3:-}" SSH_KEY="${4:-}" if [ -z "$VPS_HOST" ]; then echo -e "${YELLOW}Please provide your VPS connection details:${RESET}" read -rp " ➤ Enter VPS IP address or hostname [default: ${TARGET_DOMAIN}]: " INPUT_HOST VPS_HOST="${INPUT_HOST:-$TARGET_DOMAIN}" fi if [ -z "$SSH_USER" ]; then read -rp " ➤ Enter SSH Username [default: ${DEFAULT_USER}]: " INPUT_USER SSH_USER="${INPUT_USER:-$DEFAULT_USER}" fi if [ -z "$SSH_PORT" ]; then read -rp " ➤ Enter SSH Port [default: ${DEFAULT_PORT}]: " INPUT_PORT SSH_PORT="${INPUT_PORT:-$DEFAULT_PORT}" fi if [ -z "$SSH_KEY" ]; then read -rp " ➤ Enter SSH Private Key path (optional, press Enter to use default SSH agent/password): " INPUT_KEY SSH_KEY="${INPUT_KEY:-}" fi REMOTE_DIR="$DEFAULT_REMOTE_DIR" # Build SSH identity option SSH_KEY_OPT="" if [ -n "$SSH_KEY" ] && [ -f "$SSH_KEY" ]; then SSH_KEY_OPT="-i $SSH_KEY" fi # Locate the project root directory where this script resides SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" echo "" echo -e "${BLUE}ℹ Deployment Summary:${RESET}" echo -e " • Target Subdomain : ${BOLD}https://${TARGET_DOMAIN}${RESET}" echo -e " • VPS Remote Server : ${BOLD}${SSH_USER}@${VPS_HOST}:${SSH_PORT}${RESET}" echo -e " • Remote Web Root : ${BOLD}${REMOTE_DIR}${RESET}" echo -e " • Local Source Dir : ${BOLD}${SCRIPT_DIR}${RESET}" echo "" # 2. Verify SSH Connectivity echo -e "${CYAN}[1/5] Testing SSH connection to ${SSH_USER}@${VPS_HOST}...${RESET}" SSH_CMD="ssh -p ${SSH_PORT} ${SSH_KEY_OPT} -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new ${SSH_USER}@${VPS_HOST}" if ! $SSH_CMD "echo 'SSH connection verified successfully on \$(hostname)'" ; then echo -e "${RED}✘ Error: Unable to connect via SSH to ${SSH_USER}@${VPS_HOST} on port ${SSH_PORT}.${RESET}" echo -e "${YELLOW}Troubleshooting Tips:${RESET}" echo " 1. Verify the VPS IP address is correct and running." echo " 2. Confirm your SSH key or password is accepted." echo " 3. Ensure your server firewall allows inbound connections on port ${SSH_PORT}." exit 1 fi echo -e "${GREEN}✓ SSH connection established!${RESET}" # 3. Create Remote Web Directory echo "" echo -e "${CYAN}[2/5] Initializing remote directory: ${REMOTE_DIR}...${RESET}" $SSH_CMD "sudo mkdir -p ${REMOTE_DIR} ${REMOTE_DIR}/data_storage && sudo chown -R ${SSH_USER} ${REMOTE_DIR}" echo -e "${GREEN}✓ Remote directories ready.${RESET}" # 4. Synchronize Files to the Live VPS echo "" echo -e "${CYAN}[3/5] Synchronizing application files to ${REMOTE_DIR}...${RESET}" EXCLUDES=( "--exclude=.git" "--exclude=node_modules" "--exclude=.DS_Store" "--exclude=*.log" "--exclude=*.swp" "--exclude=dist" ) # Use rsync if available, otherwise fallback to streaming tar over SSH if command -v rsync >/dev/null 2>&1; then echo -e " → Using ${BOLD}rsync${RESET} for fast incremental sync..." rsync -avz --progress --delete "${EXCLUDES[@]}" \ -e "ssh -p ${SSH_PORT} ${SSH_KEY_OPT} -o StrictHostKeyChecking=accept-new" \ "${SCRIPT_DIR}/" "${SSH_USER}@${VPS_HOST}:${REMOTE_DIR}/" else echo -e " → rsync not found locally; falling back to streaming ${BOLD}tar over SSH${RESET}..." tar "${EXCLUDES[@]}" -czf - -C "${SCRIPT_DIR}" . | \ $SSH_CMD "tar -xzf - -C ${REMOTE_DIR}" fi echo -e "${GREEN}✓ File synchronization complete.${RESET}" # 5. Set Remote Permissions & Reload Web Services echo "" echo -e "${CYAN}[4/5] Setting file permissions and reloading Nginx + PHP-FPM...${RESET}" $SSH_CMD "bash -s" << 'REMOTE_COMMANDS' set -e REMOTE_PATH="/var/www/food.jackpilot.co" # Set standard web server ownership if id www-data >/dev/null 2>&1; then sudo chown -R www-data:www-data "$REMOTE_PATH" fi # Read & execute permissions for all files, write permissions for data_storage sudo find "$REMOTE_PATH" -type d -exec chmod 755 {} + sudo find "$REMOTE_PATH" -type f -exec chmod 644 {} + sudo chmod +x "$REMOTE_PATH"/*.sh 2>/dev/null || true if [ -d "$REMOTE_PATH/data_storage" ]; then sudo chmod -R 775 "$REMOTE_PATH/data_storage" fi # Reload PHP-FPM service dynamically PHP_FPM_SVC=$(systemctl list-units --type=service --state=running | grep -oE 'php[0-9.]+-fpm\.service|php-fpm\.service' | head -n 1 || true) if [ -n "$PHP_FPM_SVC" ]; then echo " → Reloading $PHP_FPM_SVC..." sudo systemctl reload "$PHP_FPM_SVC" || sudo systemctl restart "$PHP_FPM_SVC" fi # Check and reload Nginx if command -v nginx >/dev/null 2>&1; then echo " → Validating Nginx configuration..." sudo nginx -t && sudo systemctl reload nginx fi REMOTE_COMMANDS echo -e "${GREEN}✓ Remote permissions updated and services reloaded.${RESET}" # 6. Live Subdomain Verification echo "" echo -e "${CYAN}[5/5] Checking live status of https://${TARGET_DOMAIN}...${RESET}" HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -k --connect-timeout 5 "https://${TARGET_DOMAIN}/" || echo "000") if [ "$HTTP_STATUS" = "200" ] || [ "$HTTP_STATUS" = "301" ] || [ "$HTTP_STATUS" = "302" ]; then echo -e "${GREEN}${BOLD}✓ SUCCESS! Subdomain is responding with HTTP ${HTTP_STATUS}!${RESET}" else echo -e "${YELLOW}ℹ Response code: ${HTTP_STATUS}.${RESET}" echo -e " If DNS was just created, allow 5-15 minutes for global DNS propagation." echo -e " You can verify your DNS record with: ${BOLD}dig A ${TARGET_DOMAIN} +short${RESET}" fi echo "" echo -e "${GREEN}${BOLD}================================================================================" echo " 🎉 Deployment to https://${TARGET_DOMAIN} Completed!" echo "================================================================================" echo -e "${RESET}" echo -e " • Storefront URL : ${BOLD}${CYAN}https://${TARGET_DOMAIN}/${RESET}" echo -e " • Admin Console : ${BOLD}${CYAN}https://${TARGET_DOMAIN}/admin${RESET}" echo -e " • REST API : ${BOLD}${CYAN}https://${TARGET_DOMAIN}/api.php${RESET}" echo -e " • Web Directory : ${BOLD}${REMOTE_DIR}${RESET}" echo "" echo -e "${YELLOW}Tip: If this is a brand-new VPS, ensure you ran 'setup-subdomain-vps.sh' first${RESET}" echo -e "${YELLOW} to install Nginx, PHP 8.2-FPM, and obtain the free Let's Encrypt SSL certificate.${RESET}" echo ""