This commit is contained in:
2025-06-24 21:18:46 +01:00
parent 59023322f7
commit 238d690024

View File

@@ -1,11 +1,11 @@
#!/bin/bash
# ==============================================================================
# NGINX CONFIG & SSL DEPLOYMENT SCRIPT (v6)
# NGINX CONFIG & SSL DEPLOYMENT SCRIPT (v7)
#
# This script securely copies NGINX configuration files, tests the config,
# reloads Nginx, and then automates running Certbot to issue/expand SSL
# certificates for all domains found in the `sites-available` directory.
# This script securely copies NGINX configuration files, automatically
# creates symbolic links to enable the sites, tests the config, reloads Nginx,
# and then automates running Certbot to issue/expand SSL certificates.
#
# INSTRUCTIONS:
# 1. Ensure Certbot is installed on the remote server.
@@ -58,14 +58,19 @@ if [ ! -d "$SOURCE_SITES_DIR" ]; then
exit 1
fi
# --- Local Operations: Find all unique domains ---
# --- Local Operations ---
# Get a space-separated list of the config filenames.
# This will be used on the remote server to create symlinks.
CONFIG_FILES=$(ls "$SOURCE_SITES_DIR")
echo "-> Found site config files to process: $CONFIG_FILES"
# Find all unique domains for Certbot.
echo "-> Scanning local 'sites-available' for unique domain names..."
# This robust command finds all 'server_name' entries, handles multiple domains
# per line, removes duplicates, and creates a clean, space-separated list.
ALL_DOMAINS=$(grep -h -r "server_name" "$SOURCE_SITES_DIR" | sed 's/.*server_name\s*//' | sed 's/;//' | xargs -n1 | sort -u | tr '\n' ' ' | sed 's/ *$//')
if [ -z "$ALL_DOMAINS" ]; then
echo "⚠️ WARNING: No domains found in 'sites-available' config files. Skipping Certbot step later."
echo "⚠️ WARNING: No domains found. Skipping Certbot step later."
else
echo " ✅ Found domains: $ALL_DOMAINS"
fi
@@ -93,8 +98,8 @@ fi
echo " ✅ All files successfully transferred to temporary location."
echo
# Step 3: Move files into place, clean up, and test config.
echo "- Moving files into place with sudo and cleaning up..."
# Step 3: Move files, create symbolic links, clean up, and test config.
echo "- Moving files, enabling sites, 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"
@@ -102,20 +107,37 @@ ssh -i "$EVAL_KEY_FILE" "${REMOTE_USER}@${REMOTE_HOST}" << EOF
# Move the sites-available files
sudo mv "$REMOTE_TEMP_DIR/sites-available/"* "$DEST_SITES_PATH"
# --- NEW: Enable sites by creating symbolic links ---
echo " -> Checking and creating symbolic links in sites-enabled..."
for CONFIG_FILE in $CONFIG_FILES
do
SOURCE_FILE="/etc/nginx/sites-available/\$CONFIG_FILE"
LINK_FILE="/etc/nginx/sites-enabled/\$CONFIG_FILE"
if [ ! -L "\$LINK_FILE" ]; then
if [ -f "\$SOURCE_FILE" ]; then
echo " -> Creating link for \$CONFIG_FILE..."
sudo ln -s "\$SOURCE_FILE" "\$LINK_FILE"
else
echo " -> Source file \$SOURCE_FILE not found, skipping link."
fi
else
echo " -> Link for \$CONFIG_FILE already exists."
fi
done
# --- END NEW ---
# 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 configuration test passed."
echo " ✅ Files moved, sites enabled, and configuration test passed."
echo
# Step 4: Reload Nginx to apply new configs before running Certbot