Skip to main content

Command Palette

Search for a command to run...

How to Configure Nginx for Multiple Domains with SSL Certificates: A Step-by-Step Guide

Updated
3 min read

Nginx is a robust and versatile web server capable of managing multiple domains and ensuring secure content delivery through SSL/TLS certificates. In this guide, we will provide a detailed walkthrough on setting up Nginx to handle multiple domains and configuring SSL certificates for secure connections.

Prerequisites:

Domain Names and DNS Records

Make sure you have valid domain names, like testaditya.duckdns.org and testaditya1.duckdns.org in this example, and that they are correctly registered. You can use DuckDNS for free domain registration.

Ensure the DNS A records for these domains point to your server's public IP address. Use a DNS management tool or service to set up these records.

Ubuntu Server: This guide assumes you are using an Ubuntu-based server. If you use a different distribution, adjust the commands as needed. Your server should have a public IP address that can be accessed from the internet.

Root or Sudo Access: You'll need root or sudo access to install software, tweak configuration files, and restart services.

Step 1. Install Nginx

First, you need to have Nginx installed on your server. If you haven't installed Nginx yet, you can do so with the following commands:

sudo apt update

sudo apt install nginx

Step 2: Installing Certbot for SSL Certificates

Install Certbot and Nginx Plugin:

sudo apt install certbot python3-certbot-nginx

Step 3: Configure Nginx

Create a configuration file for your domain. Create one directory for two domains and add index.html with the desired content.

sudo mkdir -p /var/www/domain1.com

echo 'Hello, I am Aditya 1' | sudo tee /var/www/domain1.com/index.html

Step 3: Create Nginx Config Files

sudo nano /etc/nginx/sites-available/domain1.com

Step 3: Enable the sites

sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/

Step 4: Test Nginx Configuration

sudo nginx -t

Step 4: Obtain SSL Certificates Using Certbot

sudo certbot --nginx -d testaditya1.duckdns.org

sudo certbot --nginx -d testaditya.duckdns.org

Step 5: Obtain SSL Certificates Using Certbot

sudo certbot certificates

Nginx configuration file should look like this at the end.

Now, your server is correctly serving the content for those domains. Enter both domains on browser.

Bash Script to automate this process

#!/bin/bash

# Variables
DOMAINS="testaditya.duckdns.org testaditya1.duckdns.org"
WEB_ROOT="/var/www/domain1.com"
NGINX_CONF="/etc/nginx/sites-available/domain1.com"
CERTBOT_LOG="/var/log/letsencrypt/letsencrypt.log"

# Update and install necessary packages
echo "Updating package lists..."
sudo apt update

echo "Installing Nginx and Certbot..."
sudo apt install -y nginx certbot python3-certbot-nginx

# Create Nginx configuration
echo "Creating Nginx configuration..."
cat <<EOF | sudo tee $NGINX_CONF
server {
    listen 80;
    listen [::]:80;
    server_name $DOMAINS;

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

    location / {
        return 301 https://\$host\$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name $DOMAINS;

    root $WEB_ROOT;
    index index.html;

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

    # SSL configuration
    ssl_certificate /etc/letsencrypt/live/testaditya.duckdns.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/testaditya.duckdns.org/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
EOF

# Enable the Nginx site
echo "Enabling Nginx site configuration..."
sudo ln -s $NGINX_CONF /etc/nginx/sites-enabled/

# Test Nginx configuration
echo "Testing Nginx configuration..."
sudo nginx -t

# Reload Nginx to apply changes
echo "Reloading Nginx..."
sudo systemctl reload nginx

# Obtain SSL certificates using Certbot
echo "Obtaining SSL certificates with Certbot..."
sudo certbot --nginx -d testaditya.duckdns.org -d testaditya1.duckdns.org

# Check Certbot status
echo "Checking Certbot status..."
sudo certbot certificates

echo "SSL setup is complete. Please verify your domains."