Skip to content

Instantly share code, notes, and snippets.

@tas33n
Last active June 9, 2025 10:23
Show Gist options
  • Save tas33n/62583ccb3a28a72ae08af7002d94dfd0 to your computer and use it in GitHub Desktop.
Save tas33n/62583ccb3a28a72ae08af7002d94dfd0 to your computer and use it in GitHub Desktop.
πŸš€ Automate full Nginx reverse proxy + SSL setup for multiple domains/subdomains on your VPS!
#!/bin/bash
# Exit on error
set -e
# Usage
if [ $# -lt 3 ]; then
echo "Usage: $0 <local_port> <email> <domain1> [domain2] [domain3] ..."
echo "Example: $0 3000 [email protected] example.com api.example.com"
exit 1
fi
LOCAL_PORT=$1
EMAIL=$2
shift 2
DOMAINS=("$@")
PRIMARY_DOMAIN=${DOMAINS[0]} # Used for config file naming
NGINX_CONF="/etc/nginx/sites-available/$PRIMARY_DOMAIN"
echo "πŸ›  Setting up domains: ${DOMAINS[*]} to forward traffic to localhost:$LOCAL_PORT with SSL..."
# Install dependencies
if ! command -v nginx &> /dev/null; then
echo "πŸ”Ή Installing Nginx..."
sudo apt update
sudo apt install -y nginx
fi
if ! command -v certbot &> /dev/null; then
echo "πŸ”Ή Installing Certbot..."
sudo apt install -y certbot python3-certbot-nginx
fi
# Build server_name list
SERVER_NAMES=$(IFS=' '; echo "${DOMAINS[*]}")
# Create Nginx config
echo "πŸ”Ή Creating Nginx config for domains..."
sudo tee "$NGINX_CONF" > /dev/null <<EOF
server {
listen 80;
server_name $SERVER_NAMES;
location / {
proxy_pass http://127.0.0.1:$LOCAL_PORT;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host \$host;
proxy_cache_bypass \$http_upgrade;
}
}
EOF
# Enable config
echo "πŸ”Ή Enabling site configuration..."
sudo ln -sf "$NGINX_CONF" /etc/nginx/sites-enabled/
# Test config and restart Nginx
echo "πŸ”Ή Testing Nginx configuration..."
sudo nginx -t
echo "πŸ”Ή Restarting Nginx..."
sudo systemctl restart nginx
# Build certbot domain args
CERTBOT_DOMAINS=()
for DOMAIN in "${DOMAINS[@]}"; do
CERTBOT_DOMAINS+=("-d" "$DOMAIN")
done
# Request SSL cert
echo "πŸ”Ή Requesting SSL certificates for: ${DOMAINS[*]}"
sudo certbot --nginx "${CERTBOT_DOMAINS[@]}" --non-interactive --agree-tos -m "$EMAIL"
# Set up SSL renewal
echo "πŸ”Ή Enabling auto-renewal for SSL..."
sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer
# Final restart
echo "πŸ”Ή Restarting Nginx..."
sudo systemctl restart nginx
echo "βœ… All domains are now secured and forwarded to localhost:$LOCAL_PORT"
@tas33n
Copy link
Author

tas33n commented Feb 24, 2025

Automate Nginx Domain Configuration for VPS Server
Simplify reverse proxy + SSL with one script!

πŸš€ How to Use
1️⃣ Save the script as setupdomain.sh

nano setupdomain.sh

Paste the script, then save & exit (CTRL + X, then Y, then ENTER).

2️⃣ Make it executable

chmod +x setupdomain.sh

3️⃣ Run the script with your app port, SSL email, and domains (main + subdomains)

./setupdomain.sh 3000 [email protected] example.com api.example.com dashboard.example.com

🎯 What This Script Does
βœ” Installs Nginx & Certbot (if missing)
βœ” Creates and enables a reverse proxy config for one or more domains/subdomains
βœ” Automatically requests and installs a free SSL certificate via Let's Encrypt
βœ” Enables auto-renewal for all SSL certs
βœ” Restarts Nginx to apply changes instantly

πŸ›‘οΈ Works with both main domains (example.com) and subdomains (api.example.com, etc.)
πŸ“¦ Ideal for web apps running on a VPS behind any local port (Node.js, PHP, etc.)
πŸ’‘ Minimal input, maximum automation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment