Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

502 Bad Gateway - django + nginx + gunicorn - sock failed (13: Permission denied)

I am following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04

I believe everything else in my setup is correct. The file /var/log/nginx/error.log has many of the following error:

2015/12/19 18:41:58 [crit] 10850#0: *23 connect() to unix:/home/root/myproject/myproject.sock failed (13: Permission denied) while connecting to upstream, client: [[my ip]], server: [[server ip]], request: "GET / HTTP/1.1", upstream: "http://unix:/home/root/myproject/myproject.sock:/", host: "[[server ip]]"

The command:

/home/root/myproject ls -l /home/root/classNote/classNote.sock

outputs:

srwxrwxrwx 1 root www-data 0 Dec 19 18:17 /home/root/myproject/myproject.sock

EDIT: Andrei's comment's response:

the command:

ps ax | grep gunicorn

outputs:

  847 ?        Ss     0:00 /home/root/myproject/myprojectEnv/bin/python myprojectEnv/bin/gunicorn --workers 3 --bind unix:/home/root/myproject/myproject.sock myproject.wsgi:application
  921 ?        S      0:00 /home/root/myproject/myprojectEnv/bin/python myprojectEnv/bin/gunicorn --workers 3 --bind unix:/home/root/myproject/myproject.sock myproject.wsgi:application
  923 ?        S      0:00 /home/root/myproject/myprojectEnv/bin/python myprojectEnv/bin/gunicorn --workers 3 --bind unix:/home/root/myproject/myproject.sock myproject.wsgi:application
  928 ?        S      0:00 /home/root/myproject/myprojectEnv/bin/python myprojectEnv/bin/gunicorn --workers 3 --bind unix:/home/root/myproject/myproject.sock myproject.wsgi:application
 1136 pts/0    S+     0:00 grep gunicorn
like image 561
David Avatar asked Nov 21 '22 19:11

David


2 Answers

link to similar question

I just ran into this problem. I was able to create the gunicorn socket file, but nginx complained about permission denied. The issue was that my socket file was in a sub-folder and the root folder did not have read or execute permissions. So even though the sub-folder had the correct permissions, the root folder prevented nginx from entering the sub-folder.

The solution was to add read and execute permissions to the root folder:

chmod o+rx /example_root_folder
like image 89
Bryce Avatar answered Nov 23 '22 10:11

Bryce


Seems there are permission issues start your nginx service as root because root has permission to access your my project.sock

sudo service nginx stop

Then

 sudo service nginx start

But it is not a good idea to start nginx as root. You may try changing permission of your current user

I can suggest you an alternative which works fine. Let a shell script handle all this. Create a shell script like this (* indicates required in comments)

#!/bin/bash

NAME=""                              #Name of the application (*)
DJANGODIR=/path/to/django/project            # Django project directory (*)
SOCKFILE=/path/to/socket/file/myproject.sock        # we will communicate using this unix socket (*)
USER=                                      # the user to run as (*)
GROUP=                                     # the group to run as (*)
NUM_WORKERS=1                                     # how many worker   processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=yourproject.settings             # which settings file should Django use (*)
DJANGO_WSGI_MODULE=yourproject.wsgi                     # WSGI module name (*)

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /path/to/virtualenv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /path/to/virtualenv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user $USER \
  --bind=unix:$SOCKFILE
like image 45
Yugandhar Chaudhari Avatar answered Nov 23 '22 10:11

Yugandhar Chaudhari