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

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

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

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

Team:

/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

: :

:

ps ax | grep gunicorn

:

  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
+4
2

, nginx root, root project.sock

sudo service nginx stop

 sudo service nginx start

nginx root.

, . script . script, (* , )

#!/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
+2

, ( digitalocean.com), , , 502.

, IP-.

virtualenv ( "manage.py" ) gunicorn.py: (: 3.4)

#!/usr/bin/python3.4

"""
Run Gunicorn (Django) on a specific IP addr
"""

import os

# Change directory to the virtualenv folder
os.chdir("/home/your_path/name_of_env/")

# Run Gunicorn
os.system("bin/gunicorn -w 3 -b 127.0.0.1:8000 your_project.wsgi:application &")

script Gunicorn, virtualenv ( "bin/gunicorn" ) ( "&" ).

. virtualenv, "bin/" script .

script , .profile . ( )

python3.4 path/to/the/file/gunicorn.py

nginx :

location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
}

, IP- 127.0.0.1:8000 . , Django, 2 :

  • Gunicorn "ps aux | grep gunicorn" "kill -9 [PID]", , , "python3.4 manage.py runningerver"
  • Django, IP- , "python3.4 manage.py runningerver 0.0.0.0:8001"

.

-2

Source: https://habr.com/ru/post/1620927/


All Articles