How to configure multiple django project running on different system ports using same IP with gunicorn

Viewed 182

This question is a bit lengthy, but I tried to provide as much information as possible from my doubts and trials. I've been fiddling with gunicorn and nginx configurations to host 3 django projects on an AWS EC2 ubuntu server. I've been unable to sort them to run, neither been able to fully understand the settings.

(1) First of all I have them them as follows (Each project folder has its own venv setup):

drwxrwxr-x 10 ubuntu ubuntu 4096 Mar 26 02:10 first_project/
drwxrwxr-x  7 ubuntu ubuntu 4096 Mar 26 01:20 second_project/
drwxrwxr-x  7 ubuntu ubuntu 4096 Mar 26 01:25 third_project/

(2) I tried creating separate .socket files in /etc/systemd/system/ folder

-rw-r--r--  1 root root  112 Mar 26 02:17  gunicorn_first.socket
-rw-r--r--  1 root root  112 Mar 26 02:19  gunicorn_second.socket
-rw-r--r--  1 root root  112 Mar 26 02:22  gunicorn_third.socket

(3) and included all of them with same content:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

(4) Post adding .socket files, I added .service files

-rw-r--r--  1 root root  112 Mar 26 02:35  gunicorn_first.service
-rw-r--r--  1 root root  112 Mar 26 02:39  gunicorn_second.service
-rw-r--r--  1 root root  112 Mar 26 02:44  gunicorn_third.service

(5) and added content as follows (NTH -> first , second, third)

[Unit]
Description=gunicorn daemon
Requires=gunicorn_NTH.socket
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/NTH_project
ExecStart=/home/ubuntu/NTH_project/venv/bin/gunicorn --workers 3 --access-logfile - --bind unix:/run/gunicorn.sock NTH_project.wsgi:application

[Install]
WantedBy=multi-user.target

(6) And finally my nginx default configuration (/etc/nginx/sites-enabled/default) as follows. I've opened ports 8081, 8082, 8083 - one for each project

server {
    listen 8081;
    server_name MY_IP;

    location /first_project {
        root /home/ubuntu/first_project;
    }
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn_first.sock;
    }
}

server {
    listen 8082;
    server_name MY_IP;

    location /second_project {
        root /home/ubuntu/second_project;
    }
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn_second.sock;
    }
}

server {
    listen 8083;
    server_name MY_IP;

    location /third_project {
        root /home/ubuntu/third_project;
    }
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn_third.sock;
    }
}

The above approach doesn't work, so I replaced proxy_pass in each with http://unix:/run/gunicorn.sock -> and if I do so, only the last application (8083) is working, when I send requests to endpoint from postman (http://MY_IP:8083/some_endpoint). The other 2 are stating django error that the route is not found (404)

Can someone please help on how I can ensure all 3 apps are running

(http://MY_IP:8081/some1_endpoint)
(http://MY_IP:8082/some2_endpoint)
(http://MY_IP:8083/some3_endpoint)

If you can point which part(s) among 1-6 can be edited, it would be great Thanks in advance.

0 Answers
Related