5
votes

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
3

3 Answers

2
votes

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
1
votes

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
-3
votes

I had a very similar problem like you before (also proceeded according to the mentioned tutorial on the digitalocean.com), by this I mean the socket was not working and also I was getting the 502 error.

The solution for me was to move from socket binding to binding a normal IP address.

Inside of a virtualenv (next to "manage.py") I have a gunicorn.py file: (NOTE: I use 3.4 version)

#!/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 &")

The script runs Gunicorn without activating the virtualenv (command "bin/gunicorn") and in background ("&").

NOTE: If you don't use virtualenv than remove "bin/" from the script above.

For calling that script after booting PC and login, add this line to your ".profile" file at the end. (It's inside your home directory)

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

In a nginx file you should have this part of code:

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;
}

Remember, the IP addr 127.0.0.1:8000 is reserved. So if you want to run Django developing server, you have 2 options:

  1. Turn off Gunicorn by commands "ps aux | grep gunicorn" and "kill -9 [PID]" and then normally run a command "python3.4 manage.py runserver"
  2. Or run Django developing server on a different IP and port, for example "python3.4 manage.py runserver 0.0.0.0:8001"

This solution works for me.