Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gunicorn and Django error permission denied for sock

Trying to setup a site with django and gunicorn getting this error in the nginx log file:

2017/01/31 07:04:50 [crit] 30386#30386: *1 connect() to unix:/home/ubuntu/webapps/kenyabuzz/kb.sock failed (13: Permission denied) while connecting to upstream, client: 197.232.12.165, server: kenyabuzz.nation.news, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/webapps/kenyabuzz/kb.sock:/", host: "kenyabuzz.nation.news"

static files are served correctly. The gunicorn file in nginx/sites-enabled settings

#kb gunicorn nginx settings

server {
    listen 80;
    server_name kenyabuzz.nation.news;

    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/ubuntu/webapps/kenyabuzz/kb/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/ubuntu/webapps/kenyabuzz/kb/static; # your Django project's static files - amend as required
    }

    location /favicon.ico {
        alias /home/ubuntu/webapps/kenyabuzz/kb/static/kb/favicon.ico; # favicon
    }


    location / {
        include proxy_params;
        proxy_pass http://unix:/home/ubuntu/webapps/kenyabuzz/kb.sock;
    }
}

and the gunicorn setting /etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/webapps/kenyabuzz
ExecStart=/home/ubuntu/djangoenv/bin/gunicorn --workers 10 --bind unix:/home/ubuntu/kenyabuzz/kb.sock kb.wsgi:application

[Install]
WantedBy=multi-user.target

checked the status of gunicorn

ubuntu@ip-172-31-16-133:/etc/nginx/sites-enabled$ sudo systemctl status gunicorn
● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2017-01-31 06:59:49 UTC; 8min ago
 Main PID: 30281 (code=exited, status=203/EXEC)

Jan 31 06:59:48 ip-172-31-16-133 systemd[1]: Started gunicorn daemon.
Jan 31 06:59:49 ip-172-31-16-133 systemd[1]: gunicorn.service: Main process exited, code=exited, sta
Jan 31 06:59:49 ip-172-31-16-133 systemd[1]: gunicorn.service: Unit entered failed state.
Jan 31 06:59:49 ip-172-31-16-133 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
like image 443
Sam B. Avatar asked Jan 31 '17 07:01

Sam B.


1 Answers

You have your gunicorn process running as user Ubuntu and Group www-data

[Service]
User=ubuntu
Group=www-data

Typically in ubuntu, nginx runs as www-data. I See that you have defined www-data as the group for gunicorn. Therefore you can solve this problem by

chmod g+x /home/ubuntu/
chmod g+r /home/ubuntu/

Assuming that you have www-data as the group for the above folder. If not you can change it with

sudo chgrp www-data /home/ubuntu/
like image 162
e4c5 Avatar answered Nov 17 '22 17:11

e4c5