Gunicorn和Django错误:sock权限被拒绝

5
尝试使用Django和Gunicorn设置网站时,在Nginx日志文件中出现以下错误:
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"

静态文件被正确地提供了。在nginx/sites-enabled设置中,gunicorn文件。

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

还有gunicorn设置/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

查看 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'.
1个回答

7
你的gunicorn进程以Ubuntu用户和www-data用户组的身份运行。
[Service]
User=ubuntu
Group=www-data

通常在Ubuntu中,Nginx以www-data身份运行。我看到您已将www-data定义为gunicorn的组。因此,您可以通过以下方式解决此问题:
chmod g+x /home/ubuntu/
chmod g+r /home/ubuntu/

假设您的文件夹中 www-data 是上述组。如果不是,您可以使用以下命令进行更改:
sudo chgrp www-data /home/ubuntu/

尝试过程中出现了文件不存在的情况,因此我上传了一个同名文件到文件夹中,并对该文件夹应用了权限。检查了日志(仍然得到502错误)和权限被拒绝的信息2017/01/31 08:10:57 [crit] 31000#31000: *13 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" - Sam B.
你不能简单地把一个文件放在那里!那是 gunicorn 启动时创建的套接字。如果那里没有文件,这意味着 gunicorn 没有运行。请删除你的虚拟文件,然后重新启动 gunicorn,看看文件是否存在。 - e4c5
已更新细节,文件未创建,同时包含gunicorn状态日志。 - Sam B.
创建了一个相关问题 @e4c5 http://stackoverflow.com/questions/41955530/gunicorn-django-sock-file-not-created - Sam B.
伟大的外观 - e4c5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接