Question

How to deploy multiple Django apps using nginx and Gunicorn on single Dropplet

We have followed these below resources :

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-20-04

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

https://www.digitalocean.com/community/questions/how-to-deploy-multiple-django-apps-as-subdomains-using-nginx-and-gunicorn

Here is our path specific to perticular website :

root@Way2agribusiness-2:/var/www# ls html karnatakaagribusiness.com urbanagriculture.in way2agribusiness.com way2groceries.in way2vegetables.com

In that way2vegetables i working,

Our Nginx Configuration:

root@Way2agribusiness-2:/etc/nginx/sites-enabled# ls
default  karnatakaagribusiness.com  urbanagriculture.in  way2agribusiness.com  way2groceries.in.conf  way2vegetables.com.conf
root@Way2agribusiness-2:/etc/nginx/sites-enabled# cat karnatakaagribusiness.com 
server {
     listen 80;
     listen [::]:80;
     server_name karnatakaagribusiness.com www.karnatakaagribusiness.com;

     root /var/www/karnatakaagribusiness.com/html;

     index index.html index.htm;

     location /static/ {
           root /var/www/karnatakaagribusiness.com/html/Karnatakaagribusiness;
     }
     location / {
          include proxy_params;
          proxy_pass http://unix:/run/gunicorn-kab.sock;
     }
}
root@Way2agribusiness-2:/etc/nginx/sites-enabled# cat way2agribusiness.com 
server {
     listen 80;
     listen [::]:80;
     server_name way2agribusiness.com www.way2agribusiness.com 165.232.181.109;

     root /var/www/way2agribusiness.com/html/Way2agribusiness;

     index index.html index.htm;

     location = /favicon.ico { access_log off; log_not_found off; }
     location /static/ {
           root /var/www/way2agribusiness.com/html/Way2agribusiness;
     }
     location / {
          include proxy_params;
          proxy_pass http://unix:/run/gunicorn-abi.sock;
     }
}
root@Way2agribusiness-2:/etc/nginx/sites-enabled# cat way2vegetables.com.conf 
server {
     server_name way2vegetables.com www.way2vegetables.com;

     root /var/www/way2vegetables.com/public_html;

     index index.html index.htm;
     
   
     location /static/ {
         root /var/www/way2vegetables.com/public_html/Way2vegetables;
     }
     location / {
          include proxy_params;
          proxy_pass http://unix:/run/gunicorn-vegetables.sock;
     }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/way2vegetables.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/way2vegetables.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot




}
server {
    if ($host = www.way2vegetables.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = way2vegetables.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


     listen 80;
     listen [::]:80;
     server_name way2vegetables.com www.way2vegetables.com;
    return 404; # managed by Certbot

}

Gunicorn Configuration :

root@Way2agribusiness-2:/etc/systemd/system# ls
cloud-final.service.wants               gunicorn-vegetables.socket         paths.target.wants         snap.lxd.daemon.unix.socket
cloud-init.target.wants                 gunicorn.service                   rescue.target.wants        snap.lxd.user-daemon.service
dbus-org.freedesktop.resolve1.service   gunicorn.socket                    sleep.target.wants         snap.lxd.user-daemon.unix.socket
dbus-org.freedesktop.timesync1.service  gunicorn_abi.service               snap-core20-1623.mount     snapd.mounts.target.wants
droplet-agent.service                   gunicorn_kab.service               snap-core20-1891.mount     sockets.target.wants
emergency.target.wants                  gunicorn_urbanagriculture.service  snap-core22-634.mount      ssh.service.d
final.target.wants                      iscsi.service                      snap-lxd-23680.mount       sshd-keygen@.service.d
getty.target.wants                      mdmonitor.service.wants            snap-lxd-24846.mount       sysinit.target.wants
gunicorn-abi.socket                     multi-user.target.wants            snap-snapd-19122.mount     syslog.service
gunicorn-kab.socket                     multipath-tools.service            snap-snapd-19361.mount     timers.target.wants
gunicorn-urbanagriculture.socket        network-online.target.wants        snap.lxd.activate.service  vmtoolsd.service
gunicorn-vegetables.service             open-vm-tools.service.requires     snap.lxd.daemon.service
root@Way2agribusiness-2:/etc/systemd/system# cat gunicorn.socket 
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

root@Way2agribusiness-2:/etc/systemd/system# cat gunicorn.service 
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/way2groceries.in/public_html/Way2groceries
ExecStart=/var/www/way2groceries.in/public_html/Way2groceries/venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          clinic.wsgi:application

[Install]
WantedBy=multi-user.target
root@Way2agribusiness-2:/etc/systemd/system# cat gunicorn-abi.socket 
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn-abi.sock

[Install]
WantedBy=sockets.target
root@Way2agribusiness-2:/etc/systemd/system# cat gunicorn_abi.service 
[Unit]
Description=gunicorn daemon
Requires=gunicorn-abi.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/way2agribusiness.com/html/Way2agribusiness
ExecStart=/var/www/way2agribusiness.com/html/Way2agribusiness/abi_env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn-abi.sock \
          projabi.wsgi:application

[Install]
WantedBy=multi-user.target
root@Way2agribusiness-2:/etc/systemd/system# cat gunicorn_kab.service 
[Unit]
Description=gunicorn daemon
Requires=gunicorn-kab.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/karnatakaagribusiness.com/html/Karnatakaagribusiness
ExecStart=/var/www/karnatakaagribusiness.com/html/Karnatakaagribusiness/kab_env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn-kab.sock \
          til.wsgi:application
[Install]
WantedBy=multi-user.target
root@Way2agribusiness-2:/etc/systemd/system# cat gunicorn-kab.socket 
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn-kab.sock

[Install]
WantedBy=sockets.target
root@Way2agribusiness-2:/etc/systemd/system# cat gunicorn-urbanagriculture.socket 
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn-urbanagriculture.sock

[Install]
WantedBy=sockets.target
root@Way2agribusiness-2:/etc/systemd/system# cat gunicorn_urbanagriculture.service 
[Unit]
Description=gunicorn daemon
Requires=gunicorn-urbanagriculture.socket
After=network.target
[Service]
User=root 
Group=www-data
WorkingDirectory=/var/www/urbanagriculture.in/public_html/Urban_Agriculture
ExecStart=/var/www/urbanagriculture.in/public_html/Urban_Agriculture/urban_env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn-urbanagriculture.sock \
          clinic.wsgi:application
[Install]
WantedBy=multi-user.target

root@Way2agribusiness-2:/# nginx -t
nginx: [warn] conflicting server name "way2agribusiness.com" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "www.way2agribusiness.com" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "165.232.181.109" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "way2agribusiness.com" on [::]:80, ignored
nginx: [warn] conflicting server name "www.way2agribusiness.com" on [::]:80, ignored
nginx: [warn] conflicting server name "165.232.181.109" on [::]:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

In that above only the way2vegetables.com is working fine and in perticular IP address one django website is working.

Please help us with your suggestion and resoureces.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
May 31, 2023

The warning message indicates that there are duplicate server blocks, or server names, in your Nginx configuration files. Nginx uses the server name to identify which server block to use for a request. If the same server name appears in two or more server blocks, Nginx can only use one, and the others are ignored.

As for the Nginx blocks and gunicorn. Here is an example of how to configure it:

Step 1: Configuring Gunicorn

Firstly, you need to configure Gunicorn to serve your web application. This involves creating a Gunicorn configuration file. You may already have this, but if not, here’s an example of how it could look:

# /etc/systemd/system/karnatakaagribusiness.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=youruser
Group=www-data
WorkingDirectory=/var/www/karnatakaagribusiness.com
ExecStart=/var/www/karnatakaagribusiness.com/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/var/www/karnatakaagribusiness.com/karnatakaagribusiness.sock yourapplication.wsgi:application

[Install]
WantedBy=multi-user.target

Replace youruser with your username, yourapplication with your application name. Then start and enable the Gunicorn service:

sudo systemctl start karnatakaagribusiness
sudo systemctl enable karnatakaagribusiness

Step 2: Configuring Nginx Next, you need to create a new Nginx server block configuration file for your website.

# /etc/nginx/sites-available/karnatakaagribusiness
server {
    listen 80;
    server_name karnatakaagribusiness.com www.karnatakaagribusiness.com;
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/karnatakaagribusiness.com/karnatakaagribusiness.sock;
    }

    location /static/ {
        alias /var/www/karnatakaagribusiness.com/static/;
    }

    location /media/ {
        alias /var/www/karnatakaagribusiness.com/media/;
    }
}

Then you need to enable the site by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/karnatakaagribusiness /etc/nginx/sites-enabled

After that, test Nginx configuration:

sudo nginx -t

If the test is successful, restart Nginx:

sudo systemctl restart nginx

You will need to repeat this process for each of your other sites, ensuring that you update the server names and root directories appropriately. Also, if you’re using SSL (https) you’ll need to add those settings to each server block as well.

Additionally, I’ll recommend starting over and adding the websites one by one and testing the application after each config that’s added.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel