Search code examples
djangolinuxnginxdebiangunicorn

Solving 502 Bad Gateway Error for a Django Project Nginx - gunicorn - Debian


Hi I am trying to deploy my Django Project to Production for the first time using Nginx and gunicorn on linode.

Currently I did not set a venv file I just downloaded all the requirements in the system as a whole. I am using Linode Server and selected Django from the market place and the set system is Debian. The project was working perfectly well with portal 8000 but now I am trying to take it to development.

I have made the following steps: sudo nano /etc/systemd/system/gunicorn.service

[Unit]
Description=Gunicorn service for project
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/DjangoApp/
ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:/var/www/DjangoApp/project.sock project.wsgi:application

[Install]
WantedBy=multi-user.target

I can't find the sock file in the project but this is the tree of the project if it helps cd /var/www/DjangoApp:

api  db.sqlite3  project  manage.py  media  README.md  requirements.txt  static  tac  users

in the /var/www/DjangoApp/project

asgi.py  __init__.py  __pycache__  settings.py  urls.py  wsgi.py

in my sudo nano /etc/nginx/sites-available/project

server {
        listen 80;
        server_name 111.111.111.11;
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
                root /var/www/DjangoApp/;
        }
        location / {
                include proxy_params;
            proxy_pass http://unix:/var/www/DjangoApp/project.sock;
        }
}

here is the log error

root@139-177-193-82:~# sudo tail -50 /var/log/nginx/error.log
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * open socket #11 left in connection 4                                                                                  ...........
......................: * open socket #13 left in connection 6                                                                                  ...........
......................: aborting                                                                                                                ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........

My question: Where can I find the .sock file to connect to it to fix this error. Any suggestions would be appreciated to find solution to fix 502 Bad Gateway


Solution

  • Before creating the service file you need to create systemd Socket that in turn create the sock file, to do that you need to create a file under /etc/systemd/system/<file name you want> like the following.

    sudo nano /etc/systemd/system/gunicorn_django_app.socket
    

    in that file you need to provide the following details

    [Unit]
    Description=Gunicorn service for project
    
    [Socket]
    ListenStream=/run/gunicorn_django_app.sock
    
    [Install]
    WantedBy=sockets.target
    

    This will create a sock file under /run/gunicorn_django_app.sock

    Then you need to create a systemd service file in the path /etc/systemd/system/<file name you like> like the following

    sudo nano /etc/systemd/system/gunicorn_django_app.service 
    

    In the file you need to provide service related details

    [Unit]
     Description=gunicorn daemon
     Requires=gunicorn_django_app.socket
     After=network.target
    
    [Service]
      User=username # user that can access those files
      Group=www-data # group that can access those files 
      WorkingDirectory=<projectpath>
      ExecStart=/<guincorn path>\
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn_django_app \
          DjangoApp.wsgi:application
    

    then you have to start the services

    sudo systemctl start gunicorn_django_app.socket
    sudo systemctl enable gunicorn_django_app.socket 
    

    to know more use the below link this will help you to understand more on this concepts. it's in ubuntu linux but debain based linux (it will work on Linode too) digitalocean