django gunicorn sock文件不是由wsgi创建的

时间:2016-10-07 13:57:02

标签: python django nginx gunicorn

我的数字海洋服务器(Ubuntu 16.04)中有一个基本的django休息应用程序,具有本地虚拟环境。 基本的wsgi.py是:

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "workout_rest.settings")

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)

我已经按照本教程的步骤进行了操作: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

当我测试Gunicorn使用此命令服务项目的能力时: gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application 一切顺利。

所以我试图设置Gunicorn使用systemd服务文件。 我的/etc/systemd/system/gunicorn.service文件是:

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

[Service]
User=ben
Group=www-data
WorkingDirectory=/home/ben/myproject
ExecStart=/home/ben/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/ben/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

我的Nginx配置是:

server {
    listen 8000;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ben/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/ben/myproject/myproject.sock;
    }
}

我已将侦听端口从80更改为8000,因为80给了我一个err_connection_refused错误。 使用此命令启动服务器后:

sudo systemctl restart nginx

当我尝试运行我的网站时,出现502 Bad Gateway错误。 我已经尝试过这些命令(在教程评论中找到):

sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl restart nginx

但没有任何改变。 当我使用此命令查看Nginix日志时:

sudo tail -f /var/log/nginx/error.log

我可以读到袜子文件不存在:

2016/10/07 09:00:18 [crit] 24974#24974: *1 connect() to unix:/home/ben/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 86.197.20.27, server: 139.59.150.116, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ben/myproject/myproject.sock:/", host: "server_ip_adress:8000"

为什么没有创建这个袜子文件?如何配置django / gunicorn来创建此文件? 我在我的Django项目的INSTALLED_APP中添加了gunicorn,但它并没有改变任何东西。

修改

当我使用nginx -t测试nginx配置文件时,出现错误:open() "/run/nginx.pid" failed (13: Permission denied)。 但是,如果我使用sudo:sudo nginx -t运行命令,则测试成功。这是否意味着我必须允许这样做?用户运行Ngnix?

关于gunicorn logfile,我找不到一种方法来阅读它们。它们存放在哪里?

当我使用ps aux | grep gunicorn检查gunicorn是否正在运行时:

ben      26543  0.0  0.2  14512  1016 pts/0    S+   14:52   0:00 grep --color=auto gunicorn

当你为gunicorn运行systemctl enable和start命令时会发生这样的问题:

sudo systemctl enable gunicorn
Synchronizing state of gunicorn.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable gunicorn

sudo systemctl start gunicorn
I get no output with this command

sudo systemctl is-active gunicorn
active

sudo systemctl status gunicorn
● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: active (exited) since Thu 2016-10-06 15:40:29 UTC; 23h ago

Oct 06 15:40:29 DevUsine systemd[1]: Started gunicorn.service.
Oct 06 18:52:56 DevUsine systemd[1]: Started gunicorn.service.
Oct 06 20:55:05 DevUsine systemd[1]: Started gunicorn daemon.
Oct 06 20:55:17 DevUsine systemd[1]: Started gunicorn daemon.
Oct 06 21:07:36 DevUsine systemd[1]: Started gunicorn daemon.
Oct 06 21:16:42 DevUsine systemd[1]: Started gunicorn daemon.
Oct 06 21:21:38 DevUsine systemd[1]: Started gunicorn daemon.
Oct 06 21:25:28 DevUsine systemd[1]: Started gunicorn daemon.
Oct 07 08:58:43 DevUsine systemd[1]: Started gunicorn daemon.
Oct 07 15:01:22 DevUsine systemd[1]: Started gunicorn daemon.

4 个答案:

答案 0 :(得分:17)

我必须更改我的sock文件夹的权限:

sudo chown ben:www-data /home/ben/myproject/

另一件事是我在许多帖子中阅读之后改变了袜子的位置,将sock文件保存在django项目中并不是一个好的实践。 我的新位置是:

/home/ben/run/

不要忘记更改权限:

sudo chown ben:www-data /home/ben/run/

为了确保刷新gunicorn,请运行以下命令:

pkill gunicorn
sudo systemctl daemon-reload
sudo systemctl start gunicorn

这将杀死枪支进程并启动新进程。

您可以运行此命令使进程从服务器启动开始:

sudo systemctl enable gunicorn

现在一切正常。

答案 1 :(得分:0)

虽然接受的答案有效,但是存在一个(imo major)问题,即gunicorn Web服务器以root用户身份运行,不建议这样做。您之所以最终需要加塞套接字,是因为它是root:root所拥有的,因为这是init作业默认情况下假定的用户/组。有多种方法可以使您的工作担当另一个角色。我认为目前为止(使用gunicorn 19.9.0),最简单的解决方案是使用--user命令中提供的--groupgunicorn标志。这意味着您的服务器可以从您指定的用户/组开始。就您而言:

exec gunicorn --user ben --group www-data --bind unix:/home/ben/myproject/myproject.sock -m 007 wsgi

将在ben:www-data用户下启动gunicorn,并创建一个ben:www-data拥有的,具有权限770或用户ben和组的读/写/执行特权的套接字www-data在插座上,这正是您在此情况下需要的。

答案 2 :(得分:0)

我已经在我的项目之外给出了sock文件的路径。我只需要创建目录,以便Gunicorn可以在该目录内创建文件,就像我在.services文件中提到的路径一样。基本上,我确保根据.services文件中的路径存在所有目录。无需更改权限或所有权

答案 3 :(得分:0)

尝试运行

sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl gunicorn.service

最后一行帮助我重新创建.scok文件

相关问题