Score:0

Error 502: Deploy Django application with Gunicorn and Nginx

ar flag

I have developed a web application with Django and I am trying to publish it in a virtual machine created with Azure. To do that I'm using the following software stack:

  • Ubuntu (20.04)
  • Django (3.0.7)
  • Virtualenv (20.0.17)
  • Gunicorn (20.1.0)
  • Nginx (1.18.0)

To deploy the app I followed that guide: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

My django project folders are organized as follows:

home/
├─ useradmin/
│  ├─ myproject/
│  │  ├─ proj/
│  │  │  ├─ settings.py
│  │  │  ├─ urls.py
│  │  │  ├─ wsgi.py
│  │  │  ├─ ...
│  │  ├─ static/
│  │  ├─ templates/
│  │  ├─ venv/
│  │  ├─ manage.py
│  │  ├─ ...

This is my /etc/systemd/system/gunicorn.service:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=useradmin
Group=www-data
WorkingDirectory=/home/useradmin/myproject
ExecStart=/home/useradmin/myproject/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/useradmin/myproject/myproject.sock proj.wsgi:application
[Install]
WantedBy=multi-user.target

This is my /etc/nginx/sites-available/myproject:

server {
    listen 80;
    server_name mydomain.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/useradmin/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/useradmin/myproject/myproject.sock;
    }
}

When I navigate to mydomain.com I get a 502 Bad Gateway error. If I check the Nginx logs by running "sudo tail -F /var/log/nginx/error.log" I see the following error:

2022/03/16 08:27:33 [crit] 64480#64480: *3 connect() to unix:/home/useradmin/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: XX.XX.XX.XX, server: mydomain.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/useradmin/myproject/myproject.sock:/", host: "mydomain.com"

To create the sock file I run the following instructions but nothing changes:

systemctl daemon-reload
systemctl restart gunicorn.service

UPDATE 1:

Trying to execute the instruction ls -la /home/useradmin/myproject/myproject.sock I get the error ls: cannot access '/home/useradmin/myproject/myproject.sock': No such file or directory

djdomi avatar
za flag
what does `ls -la /home/useradmin/myproject/myproject.sock` shows? and afaik it might a easier issue - please try http://unix:/home/useradmin/myproject/myproject.sock: ;
El_Merendero avatar
ar flag
It returns `ls: cannot access '/home/useradmin/myproject/myproject.sock': No such file or directory`
djdomi avatar
za flag
fix your unicorn service at first and second please dont post comments with your informations, edit the question and add the information there
El_Merendero avatar
ar flag
Ok very thanks, I'm editing the question. How can I fix the unicorn service?
djdomi avatar
za flag
```ExecStart='/home/useradmin/myproject/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/useradmin/myproject/myproject.sock proj.wsgi:application' ``` may be the issue... BEcause it States ``` gunicorn: error: argument -b/--bind: expected one argument ``` and you provide 2 arguments not one. And a Minus-Symbol that i dont understand for what it should be
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.