Created
May 24, 2026 09:20
-
-
Save sphinxid/2326b817229385b63d48b9ee6a77948f to your computer and use it in GitHub Desktop.
Usage: create_nginx_vhost.sh <vhost_name> <app_ip> <app_port> || Example: create_nginx_vhost.sh myapp.local 127.0.0.1 8080
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 | |
| # Check if script is run as root | |
| if [ "$EUID" -ne 0 ]; then | |
| echo "Please run as root (use sudo)" | |
| exit 1 | |
| fi | |
| # Check for required parameters | |
| if [ "$#" -ne 3 ]; then | |
| echo "Error: Missing parameters." | |
| echo "" | |
| echo "Usage: $0 <vhost_name> <app_ip> <app_port>" | |
| echo "Example: $0 myapp.local 127.0.0.1 8080" | |
| echo "" | |
| exit 1 | |
| fi | |
| # Assign variables | |
| VHOST=$1 | |
| APP_IP=$2 | |
| APP_PORT=$3 | |
| SSL_DIR="/etc/ssl" | |
| NGINX_CONF="/etc/nginx/sites-available/$VHOST" | |
| NGINX_ENABLED="/etc/nginx/sites-enabled/$VHOST" | |
| echo "------------------------------------------" | |
| echo "Setting up VHost: $VHOST" | |
| echo "Proxying to: $APP_IP:$APP_PORT" | |
| echo "------------------------------------------" | |
| # 1. Generate Self-Signed SSL Certificate | |
| echo "[1/4] Generating self-signed SSL certificate..." | |
| openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ | |
| -keyout "$SSL_DIR/$VHOST.key" \ | |
| -out "$SSL_DIR/$VHOST.crt" \ | |
| -subj "/C=US/ST=State/L=City/O=Dev/OU=Web/CN=$VHOST" \ | |
| 2>/dev/null | |
| # 2. Create Nginx Configuration | |
| echo "[2/4] Creating Nginx configuration..." | |
| cat > "$NGINX_CONF" <<EOF | |
| server { | |
| listen 80; | |
| server_name $VHOST; | |
| # Redirect all HTTP traffic to HTTPS | |
| return 301 https://\$host\$request_uri; | |
| } | |
| server { | |
| listen 443 ssl; | |
| server_name $VHOST; | |
| ssl_certificate $SSL_DIR/$VHOST.crt; | |
| ssl_certificate_key $SSL_DIR/$VHOST.key; | |
| # SSL optimization | |
| ssl_protocols TLSv1.2 TLSv1.3; | |
| ssl_ciphers HIGH:!aNULL:!MD5; | |
| access_log /var/log/nginx/$VHOST.access.log; | |
| error_log /var/log/nginx/$VHOST.error.log; | |
| location / { | |
| proxy_pass http://$APP_IP:$APP_PORT; | |
| proxy_set_header Host \$host; | |
| proxy_set_header X-Real-IP \$remote_addr; | |
| proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; | |
| proxy_set_header X-Forwarded-Proto \$scheme; | |
| # WebSocket support (optional but recommended) | |
| proxy_http_version 1.1; | |
| proxy_set_header Upgrade \$http_upgrade; | |
| proxy_set_header Connection "upgrade"; | |
| } | |
| } | |
| EOF | |
| # 3. Enable the config (create symlink if it doesn't exist) | |
| if [ ! -L "$NGINX_ENABLED" ]; then | |
| ln -s "$NGINX_CONF" "$NGINX_ENABLED" | |
| fi | |
| # 4. Check Nginx Config and Reload | |
| echo "[3/4] Testing Nginx configuration..." | |
| nginx -t | |
| if [ $? -eq 0 ]; then | |
| echo "[4/4] Config OK. Reloading Nginx..." | |
| systemctl reload nginx | |
| echo "------------------------------------------" | |
| echo "SUCCESS!" | |
| echo "You can now access https://$VHOST" | |
| echo "Note: Since this is a self-signed cert, your browser will show a warning." | |
| echo "Make sure $VHOST is mapped to your IP in /etc/hosts if testing locally." | |
| else | |
| echo "------------------------------------------" | |
| echo "ERROR: Nginx configuration test failed." | |
| echo "Review the errors above." | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment