Created
May 3, 2025 02:12
-
-
Save timbogdanov/509ef78bd7f4f866d8d6050dd59ef3c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Exit on error | |
set -e | |
# Usage check | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 <old_domain> <new_domain>" | |
echo "Example: $0 uchurn.online uchurn.click" | |
exit 1 | |
fi | |
OLD_DOMAIN=$1 | |
NEW_DOMAIN=$2 | |
NGINX_CONF_DIR="/etc/nginx/sites-available" | |
OLD_CONF_FILE="${NGINX_CONF_DIR}/${OLD_DOMAIN}" | |
NEW_CONF_FILE="${NGINX_CONF_DIR}/${NEW_DOMAIN}" | |
# Check old config exists | |
if [ ! -f "$OLD_CONF_FILE" ]; then | |
echo "Error: Nginx config for $OLD_DOMAIN not found in $NGINX_CONF_DIR" | |
exit 1 | |
fi | |
# Backup old config | |
cp "$OLD_CONF_FILE" "${OLD_CONF_FILE}.bak" | |
# Replace old domain with new (including subdomains) | |
sed "s/${OLD_DOMAIN//./\\.}/${NEW_DOMAIN//./\\.}/g" "$OLD_CONF_FILE" > "$NEW_CONF_FILE" | |
# Enable the new site | |
ln -sfn "$NEW_CONF_FILE" "/etc/nginx/sites-enabled/${NEW_DOMAIN}" | |
# Test Nginx config | |
echo "Testing Nginx configuration..." | |
nginx -t | |
# Reload Nginx | |
echo "Reloading Nginx..." | |
systemctl reload nginx | |
echo "✅ Nginx config updated from $OLD_DOMAIN to $NEW_DOMAIN" | |
# Optional SSL setup | |
read -p "Do you want to issue a new SSL certificate for $NEW_DOMAIN and its subdomains (like dashboard.$NEW_DOMAIN)? (y/n): " SSL_ISSUE | |
if [[ "$SSL_ISSUE" == "y" ]]; then | |
certbot --nginx -d "$NEW_DOMAIN" -d "dashboard.$NEW_DOMAIN" | |
systemctl reload nginx | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment