Last active
June 9, 2025 10:23
-
-
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!
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 | |
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Automate Nginx Domain Configuration for VPS Server
Simplify reverse proxy + SSL with one script!
π How to Use
1οΈβ£ Save the script as
setupdomain.sh
Paste the script, then save & exit (CTRL + X, then Y, then ENTER).
2οΈβ£ Make it executable
3οΈβ£ Run the script with your app port, SSL email, and domains (main + subdomains)
π― 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.