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

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`](http://testaditya.duckdns.org) and [`testaditya1.duckdns.org`](http://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`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724910107180/29ea5a92-139c-4c02-8364-bdf79485dfe3.png align="center")

`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`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724910968314/d0fc4d90-7fb0-44c9-8483-d72605360903.png align="center")

### **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`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724912804359/435ab48d-2aa1-4da5-b3c6-4e11e6da7eea.png align="center")

Nginx configuration file should look like this at the end.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724916519893/e09cdd26-b9b0-4da4-a431-1b7790b6bf59.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724919869830/b1531b47-f2bf-4eba-862e-1c3cc9c83b74.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724919895196/ea532a10-a0d1-4f8c-8886-5b6a4f7ac96e.png align="center")

## Bash Script to automate this process

```bash
#!/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."
```
