1 year ago

#296921

test-img

El_Merendero

Error 502: Deploy Django application with Gunicorn and Nginx

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

python

django

nginx

gunicorn

http-status-code-502

0 Answers

Your Answer

Accepted video resources