DEV Community

Korir Moses
Korir Moses

Posted on

Hosting multiple Websites on a single Nginx Server

Running multiple websites on a single server is a common requirement for web developers and system administrators. This guide will walk you through the process of configuring Nginx to serve multiple websites with separate domains, each running their own Python/Django application using Gunicorn.
In this article am assuming you know how to deploy and setup the user and permisions.
Directory Structure
Your projects should be organized in a clean directory structure:

/home/username/
├── FirstProject/
│   ├── manage.py
│   └── ... (other Django files)
├── SecondProject/
│   ├── manage.py
│   └── ... (other Django files)
└── venv/
Enter fullscreen mode Exit fullscreen mode

Step 1: Configure Nginx Virtual Hosts
Nginx uses server blocks (virtual hosts) to handle multiple domains. Create separate configuration files for each website:

Create the first website's configuration:

server {
    listen 80;
    server_name your-first-domain.com;

    location = /favicon.ico { 
        access_log off; 
        log_not_found off; 
    }

    location /static/ {
        root /home/username/FirstProject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
Enter fullscreen mode Exit fullscreen mode

Create the second website's configuration:

server {
    listen 80;
    server_name your-second-domain.com;

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }

    location /static/ {
        root /home/username/SecondProject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn-second.sock;
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up Gunicorn Services
Each website needs its own Gunicorn service and socket. Here's how to set them up:

Create the first Gunicorn service file (/etc/systemd/system/gunicorn.service):

[Unit]
Description=gunicorn daemon for first site
Requires=gunicorn.socket
After=network.target

[Service]
User=username
Group=www-data
WorkingDirectory=/home/username/FirstProject
ExecStart=/home/username/venv/bin/gunicorn \
    --access-logfile - \
    --workers 3 \
    --bind unix:/run/gunicorn.sock \
    FirstProject.wsgi:application

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Create the second Gunicorn service file (/etc/systemd/system/gunicorn-second.service):

[Unit]
Description=gunicorn daemon for second site
Requires=gunicorn-second.socket
After=network.target

[Service]
User=username
Group=www-data
WorkingDirectory=/home/username/SecondProject
ExecStart=/home/username/venv/bin/gunicorn \
    --access-logfile - \
    --workers 3 \
    --bind unix:/run/gunicorn-second.sock \
    SecondProject.wsgi:application

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Create socket files for both services:

First socket (/etc/systemd/system/gunicorn.socket):

Description=gunicorn socket for first site

[Socket]
ListenStream=/run/gunicorn.sock
User=www-data

[Install]
WantedBy=sockets.target

Enter fullscreen mode Exit fullscreen mode

Second socket (/etc/systemd/system/gunicorn-second.socket):

[Unit]
Description=gunicorn socket for second site

[Socket]
ListenStream=/run/gunicorn-second.sock
User=www-data

[Install]
WantedBy=sockets.target
Enter fullscreen mode Exit fullscreen mode

Step 3: Start and Enable Services

Reload systemd to recognize new services:
sudo systemctl daemon-reload
Start and enable both sockets:
sudo systemctl start gunicorn.socket gunicorn-second.socket
sudo systemctl enable gunicorn.socket gunicorn-second.socket

Start and enable both services:
sudo systemctl start gunicorn gunicorn-second
sudo systemctl enable gunicorn gunicorn-second

Test Nginx configuration:
sudo nginx -t
Reload and check service status

sudo systemctl reload nginx
sudo systemctl status gunicorn
sudo systemctl status gunicorn-second
Enter fullscreen mode Exit fullscreen mode

and wallah

Top comments (0)