Last active
February 24, 2025 05:38
-
-
Save tas33n/62583ccb3a28a72ae08af7002d94dfd0 to your computer and use it in GitHub Desktop.
Automate Nginx Domain configuration for VPS server to simplify the work... π
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 | |
# 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." |
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 to simplify the work...
π How to Use
1οΈβ£ Save the script as setupdomain.sh
Paste the script, then save & exit (CTRL + X, then Y, then ENTER).
2οΈβ£ Give it execute permissions
3οΈβ£ Run the script with your domain and port
π― 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.