Skip to content

Instantly share code, notes, and snippets.

@tas33n
Last active February 24, 2025 05:38
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 Nginx Domain configuration for VPS server to simplify the work... πŸš€
#!/bin/bash
# Exit on error
set -e
# Check if user provided domain and port
if [ $# -ne 2 ]; then
echo "Usage: $0 <domain> <local_port>"
exit 1
fi
DOMAIN=$1
LOCAL_PORT=$2
NGINX_CONF="/etc/nginx/sites-available/$DOMAIN"
echo "πŸ›  Setting up domain: $DOMAIN to forward traffic to localhost:$LOCAL_PORT with SSL..."
# Install Nginx and Certbot if not installed
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
# Create Nginx config
echo "πŸ”Ή Creating Nginx config for $DOMAIN..."
sudo tee "$NGINX_CONF" > /dev/null <<EOF
server {
listen 80;
server_name $DOMAIN www.$DOMAIN;
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;
}
}
server {
listen 443 ssl;
server_name $DOMAIN www.$DOMAIN;
ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem;
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 Nginx config
echo "πŸ”Ή Enabling site configuration..."
sudo ln -sf "$NGINX_CONF" /etc/nginx/sites-enabled/
# Restart Nginx
echo "πŸ”Ή Restarting Nginx..."
sudo systemctl restart nginx
# Obtain SSL certificate
echo "πŸ”Ή Requesting SSL certificate from Let's Encrypt..."
sudo certbot --nginx -d "$DOMAIN" -d "www.$DOMAIN" --non-interactive --agree-tos -m "admin@$DOMAIN"
# Enable automatic renewal
echo "πŸ”Ή Setting up automatic SSL renewal..."
sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer
# Final restart to apply SSL
echo "πŸ”Ή Restarting Nginx..."
sudo systemctl restart nginx
echo "βœ… Setup complete! Your domain https://$DOMAIN is now forwarding to localhost:$LOCAL_PORT."
@tas33n
Copy link
Author

tas33n commented Feb 24, 2025

Automate Nginx Domain configuration for VPS server to simplify the work...

πŸš€ 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️⃣ Give it execute permissions

chmod +x setupdomain.sh

3️⃣ Run the script with your domain and port

./setupdomain.sh mydomain.com 9090

🎯 What This Does
βœ” Installs Nginx & Certbot if missing.
βœ” Configures Nginx reverse proxy for the domain.
βœ” Requests & installs a free SSL certificate.
βœ” Enables auto-renewal for SSL.
βœ” Restarts Nginx to apply changes.

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