updated deployment logic

This commit is contained in:
2025-06-24 20:47:27 +01:00
parent 410f6ea813
commit 948c01638b

104
deploy.sh
View File

@@ -1,17 +1,17 @@
#!/bin/bash
# ==============================================================================
# NGINX CONFIG DEPLOYMENT SCRIPT
# NGINX CONFIG DEPLOYMENT SCRIPT (v3)
#
# This script securely copies NGINX configuration files to a remote server
# using an SSH key for authentication.
# This script securely copies NGINX configuration files to a remote server.
# It handles "Permission Denied" errors by first uploading files to a
# temporary directory, and then using `sudo` to move them to the
# protected /etc/nginx/ directory.
#
# INSTRUCTIONS:
# 1. Edit the `REMOTE_USER` and `REMOTE_HOST` variables below.
# 2. Place this script in the same directory as your `nginx.conf` file
# and your `sites-available` folder.
# 3. Make the script executable with: chmod +x <script_name>.sh
# 4. Run the script with: ./<script_name>.sh
# 1. Edit the `REMOTE_USER` and `REMOTE_HOST` variables.
# 2. Make the script executable: chmod +x <script_name>.sh
# 3. Run the script: ./<script_name>.sh
# ==============================================================================
# --- Configuration ---
@@ -20,77 +20,99 @@ REMOTE_USER="ubuntu" # Example: ubuntu, ec2-user, root
REMOTE_HOST="3.9.182.122" # Example: 192.168.1.100 or my-server.com
# --- File & Path Definitions ---
# Path to your private SSH key.
# The '~' symbol represents your home directory.
KEY_FILE="~/repos/azeem-macbookair.pem"
# Source files and directory.
# These are expected to be in the same directory as this script.
# Note: I've corrected the spelling of 'sites-available' for you.
SOURCE_NGINX_CONF="nginx.conf"
SOURCE_SITES_DIR="sites-available"
# Destination paths on the remote server.
# Destination paths on the remote server
DEST_NGINX_PATH="/etc/nginx/"
DEST_SITES_PATH="/etc/nginx/sites-available/"
# Temporary directory on the remote server (relative to the user's home dir)
# NOTE: Removed the '~' to make it more compatible with scp.
REMOTE_TEMP_DIR="nginx_deploy_temp"
# --- Script Logic ---
echo "🚀 Starting NGINX configuration deployment to $REMOTE_HOST..."
echo "--------------------------------------------------------"
# Expand the tilde (~) in the key file path to an absolute path.
# This is necessary because scp might not expand '~' correctly otherwise.
EVAL_KEY_FILE=$(eval echo "$KEY_FILE")
# --- Pre-flight Checks ---
# Check 1: Ensure the SSH key file exists.
if [ ! -f "$EVAL_KEY_FILE" ]; then
echo "❌ ERROR: SSH key not found at $EVAL_KEY_FILE"
echo "Please ensure the path in the KEY_FILE variable is correct."
exit 1
fi
# Check 2: Ensure the main nginx.conf file exists.
if [ ! -f "$SOURCE_NGINX_CONF" ]; then
echo "❌ ERROR: Source file '$SOURCE_NGINX_CONF' not found in this directory."
echo "❌ ERROR: Source file '$SOURCE_NGINX_CONF' not found."
exit 1
fi
# Check 3: Ensure the sites-available directory exists.
if [ ! -d "$SOURCE_SITES_DIR" ]; then
echo "❌ ERROR: Source directory '$SOURCE_SITES_DIR' not found."
echo "Note: The script expects this directory to be named 'sites-available'."
exit 1
fi
# --- File Transfer Operations ---
# --- Remote Operations ---
# Step 1: Transfer the main nginx.conf file.
echo "- Transferring '$SOURCE_NGINX_CONF' to $DEST_NGINX_PATH..."
scp -i "$EVAL_KEY_FILE" "$SOURCE_NGINX_CONF" "${REMOTE_USER}@${REMOTE_HOST}:${DEST_NGINX_PATH}"
# Check if the last command (scp) was successful.
# Step 1: Create the temporary directory on the remote server.
# This command runs in the user's home directory by default.
echo "-> Creating temporary directory on remote server..."
ssh -i "$EVAL_KEY_FILE" "${REMOTE_USER}@${REMOTE_HOST}" "mkdir -p $REMOTE_TEMP_DIR"
if [ $? -ne 0 ]; then
echo "❌ ERROR: Failed to transfer '$SOURCE_NGINX_CONF'. Aborting."
echo "❌ ERROR: Failed to create temporary directory on the remote server. Aborting."
exit 1
fi
echo " ✅ Success."
echo " ✅ Remote temporary directory is ready."
echo
# Step 2: Transfer the contents of the sites-available directory.
# The '-r' flag is for recursive copy (required for directories).
# The '/*' at the end of the source path copies the *contents* of the directory.
echo "- Transferring all files from '$SOURCE_SITES_DIR/' to $DEST_SITES_PATH..."
scp -i "$EVAL_KEY_FILE" -r "${SOURCE_SITES_DIR}/"* "${REMOTE_USER}@${REMOTE_HOST}:${DEST_SITES_PATH}"
# Step 2: Transfer all files to the temporary directory.
# We transfer nginx.conf AND the entire sites-available directory in one command.
echo "- Transferring configuration files to temporary location..."
scp -i "$EVAL_KEY_FILE" -r "$SOURCE_NGINX_CONF" "$SOURCE_SITES_DIR" "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_TEMP_DIR}/"
if [ $? -ne 0 ]; then
echo "❌ ERROR: Failed to transfer contents of '$SOURCE_SITES_DIR'. Aborting."
echo "❌ ERROR: File transfer failed. Aborting."
exit 1
fi
echo " ✅ Success."
echo " ✅ All files successfully transferred to temporary location."
echo
# Step 3: Move files from temp to final destination using sudo and clean up.
echo "- Moving files into place with sudo and cleaning up..."
ssh -i "$EVAL_KEY_FILE" "${REMOTE_USER}@${REMOTE_HOST}" << EOF
# Move the main config file
sudo mv "$REMOTE_TEMP_DIR/nginx.conf" "${DEST_NGINX_PATH}nginx.conf"
# Move the sites-available files
sudo mv "$REMOTE_TEMP_DIR/sites-available/"* "$DEST_SITES_PATH"
# Remove the temporary directory
rm -rf "$REMOTE_TEMP_DIR"
echo " -> Verifying Nginx configuration..."
# Test the Nginx configuration for syntax errors
sudo nginx -t
EOF
if [ $? -ne 0 ]; then
echo "⚠️ WARNING: An error occurred on the remote server during the move or config test."
echo "You may need to log in manually to fix it: ssh -i $EVAL_KEY_FILE ${REMOTE_USER}@${REMOTE_HOST}"
exit 1
fi
echo " ✅ Files moved and temporary directory removed."
echo
# Step 4: Ask to reload Nginx
read -p "Nginx config test was successful. Reload Nginx to apply changes? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "- Reloading Nginx on the server..."
ssh -i "$EVAL_KEY_FILE" "${REMOTE_USER}@${REMOTE_HOST}" "sudo systemctl reload nginx"
echo " ✅ Nginx reloaded."
fi
# --- Completion ---
echo "--------------------------------------------------------"
echo "🎉 Deployment complete! All files transferred successfully."
echo "🎉 Deployment complete!"