Gunicorn和Django错误许可否认了袜子

Sam*_* M. 5 django nginx gunicorn

尝试使用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"
Run Code Online (Sandbox Code Playgroud)

静态文件正确提供.启用了nginx/sites的设置中的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

和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
Run Code Online (Sandbox Code Playgroud)

检查了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'.
Run Code Online (Sandbox Code Playgroud)

e4c*_*4c5 5

您的 gunicorn 进程以用户 Ubuntu 和 Group www-data 的身份运行

[Service]
User=ubuntu
Group=www-data
Run Code Online (Sandbox Code Playgroud)

通常在 ubuntu 中,nginx 作为 www-data 运行。我看到您已将 www-data 定义为 gunicorn 组。因此,您可以通过以下方式解决此问题

chmod g+x /home/ubuntu/
chmod g+r /home/ubuntu/
Run Code Online (Sandbox Code Playgroud)

假设您将 www-data 作为上述文件夹的组。如果没有,你可以改变它

sudo chgrp www-data /home/ubuntu/
Run Code Online (Sandbox Code Playgroud)