← Back to all snippets
BASH

Automating Nginx Virtual Host Setup

Streamline your web server configuration by using a Bash script to quickly create new Nginx virtual host files for domains and enable them.

#!/bin/bash

# --- Configuration ---
NGINX_SITES_AVAILABLE="/etc/nginx/sites-available"
NGINX_SITES_ENABLED="/etc/nginx/sites-enabled"
WEB_ROOT="/var/www"

# Function to display usage
usage() {
  echo "Usage: $0 <domain_name> <port_number> [php_fpm_socket]"
  echo "Example: $0 example.com 8000 /run/php/php8.1-fpm.sock"
  exit 1
}

# Check for required arguments
if [ -z "$1" ] || [ -z "$2" ]; then
  usage
fi

DOMAIN="$1"
PORT="$2"
PHP_FPM_SOCKET="${3:-/run/php/php-fpm.sock}" # Default PHP-FPM socket

CONFIG_FILE="$NGINX_SITES_AVAILABLE/$DOMAIN"

# Create web root directory if it doesn't exist
sudo mkdir -p "$WEB_ROOT/$DOMAIN/public_html"
sudo chown -R $USER:$USER "$WEB_ROOT/$DOMAIN"
sudo chmod -R 755 "$WEB_ROOT/$DOMAIN"

# Generate Nginx configuration
echo "Creating Nginx configuration for $DOMAIN..."
sudo tee "$CONFIG_FILE" > /dev/null <<EOF
server {
    listen $PORT;
    listen [::]:$PORT;

    server_name $DOMAIN www.$DOMAIN;
    root $WEB_ROOT/$DOMAIN/public_html;

    index index.html index.htm index.nginx-debian.html index.php;

    location / {
        try_files \$uri \$uri/ =404;
    }

    # Pass PHP scripts to FastCGI server
    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:$PHP_FPM_SOCKET;
    }

    # Deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    location ~ /\\.ht {
        deny all;
    }
}
EOF

# Enable the site
echo "Enabling site $DOMAIN..."
sudo ln -s "$CONFIG_FILE" "$NGINX_SITES_ENABLED/$DOMAIN"

# Test Nginx configuration
echo "Testing Nginx configuration..."
sudo nginx -t || { echo "Error: Nginx configuration test failed. Check $CONFIG_FILE"; exit 1; }

# Reload Nginx
echo "Reloading Nginx service..."
sudo systemctl reload nginx || { echo "Error: Failed to reload Nginx. Check logs."; exit 1; }

echo "Nginx virtual host for $DOMAIN on port $PORT configured successfully!"

# Create a dummy index.html
sudo tee "$WEB_ROOT/$DOMAIN/public_html/index.html" > /dev/null <<EOF_HTML
<!DOCTYPE html>
<html>
<head>
    <title>$DOMAIN - Hello Nginx!</title>
</head>
<body>
    <h1>Welcome to $DOMAIN!</h1>
    <p>This is a test page served by Nginx.</p>
</body>
</html>
EOF_HTML

echo "Dummy index.html created at $WEB_ROOT/$DOMAIN/public_html/index.html"
How it works: This Bash script automates the creation and enabling of an Nginx virtual host configuration. It takes a domain name and port as arguments, generates a basic Nginx server block including PHP-FPM support (if specified), creates the necessary web root directory, symlinks the configuration to `sites-enabled`, tests the Nginx configuration, and reloads the Nginx service. It also creates a simple `index.html` file for quick testing.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs