简体   繁体   English

django gunicorn 袜子文件不是由 wsgi 创建的

[英]django gunicorn sock file not created by wsgi

I have a basic django rest application in my digital ocean server (Ubuntu 16.04) with a local virtual environment.我的数字海洋服务器(Ubuntu 16.04)中有一个基本的 django rest 应用程序,具有本地虚拟环境。 The basic wsgi.py is:基本的 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)

I have followed step by step this tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04我已经一步一步地遵循了本教程: https : //www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

When I test Gunicorn's ability to serve the project with this command: gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application All works well.当我使用以下命令测试 Gunicorn 为项目提供服务的能力时: gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application 一切正常。

So I've tried to setup Gunicorn to use systemd service file.所以我尝试设置 Gunicorn 以使用 systemd 服务文件。 My /etc/systemd/system/gunicorn.service file is:我的 /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

My Nginx configuration is:我的 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;
    }
}

I've changed listen port from 80 to 8000 because 80 give me a err_connection_refused error.我已将监听端口从 80 更改为 8000,因为 80 给了我一个 err_connection_refused 错误。 After starting the server with this command:使用此命令启动服务器后:

sudo systemctl restart nginx

When I try to run my website, I get an 502 Bad Gateway error.当我尝试运行我的网站时,我收到 502 Bad Gateway 错误。 I've tried these commands (found on the tutorial comments):我已经尝试过这些命令(在教程评论中找到):

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

but nothing changes.但没有任何变化。 When I take a look at the Nginix logs with this command:当我使用以下命令查看 Nginix 日志时:

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

I can read that sock file doesn't exists:我可以读到 sock 文件不存在:

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"

Why this sock file isn't created?为什么没有创建这个 sock 文件? How can I configure django/gunicorn to create this file?如何配置 django/gunicorn 来创建这个文件? I have added gunicorn in my INSTALLED_APP in my Django project but it doesn't change anything.我在 Django 项目的 INSTALLED_APP 中添加了 gunicorn 但它没有改变任何东西。

EDIT:编辑:

When I test the nginx config file with nginx -t I get an error: open() "/run/nginx.pid" failed (13: Permission denied) .当我使用nginx -t测试 nginx 配置文件时,出现错误: open() "/run/nginx.pid" failed (13: Permission denied) But if I run the command with sudo: sudo nginx -t , the test is successful.但是,如果我使用 sudo: sudo nginx -t运行命令,则测试成功。 Does that mean that I have to allow 'ben' user to run Ngnix?这是否意味着我必须允许“ben”用户运行 Ngnix?

About gunicorn logfile, I cannot find a way to read them.关于 gunicorn 日志文件,我找不到阅读它们的方法。 Where are they stored?它们存放在哪里?

When I check whether gunicorn is running by using ps aux | grep gunicorn当我使用ps aux | grep gunicorn检查 gunicorn 是否正在运行时ps aux | grep gunicorn ps aux | grep gunicorn : ps aux | grep gunicorn

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

Here is hat happens when you run the systemctl enable and start commands for 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.

I had to change the permissions of my sock folder:我不得不更改我的 sock 文件夹的权限:

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

Another thing is that I have changed the sock location after reading in many post that it's not a good pratice to keep the sock file in the django project.另一件事是,在阅读了许多帖子后,我更改了 sock 位置,将 sock 文件保留在 django 项目中不是一个好习惯。 My new location is:我的新位置是:

/home/ben/run/

Don't forget to change permissions:不要忘记更改权限:

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

To be sure that gunicorn is refreshed, run these commands:要确保 gunicorn 已刷新,请运行以下命令:

pkill gunicorn
sudo systemctl daemon-reload
sudo systemctl start gunicorn

That will kill the gunicorn processes and start new ones.这将终止 gunicorn 进程并启动新进程。

You can run this command to make the process start at server boot:您可以运行此命令使进程在服务器启动时启动:

sudo systemctl enable gunicorn

All works well now.现在一切正常。

While the accepted answer works, there is one (imo major) issue with it, which is that the gunicorn web server is (probably) running as root, which is not recommended.虽然接受的答案有效,但它存在一个(imo 主要)问题,即 gunicorn Web 服务器(可能)以 root 身份运行,这是不推荐的。 The reason you end up needing to chown the socket is because it is owned by root:root , because that is the user/group your init job assumes by default.您最终需要 chown 套接字的原因是它归root:root ,因为这是您的 init 作业默认假定的用户/组。 There are multiple ways to get your job to assume another role.有多种方法可以让您的工作承担另一个角色。 As of this time (with gunicorn 19.9.0), in my opinion, the simplest solution to this is to use the --user and --group flags provided as part of the gunicorn command.截至目前(使用 gunicorn 19.9.0),在我看来,最简单的解决方案是使用作为gunicorn命令的一部分提供的--user--group标志。 This means your server can start with the user/group you specify.这意味着您的服务器可以从您指定的用户/组开始。 In your case:在你的情况下:

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

will start gunicorn under ben:www-data user and create a socket owned by ben:www-data with the permissions 770 , or read/write/execute privilege for the user ben and group www-data on the socket, which is exactly what you ned in this case.将在ben:www-data用户下启动 gunicorn 并创建一个由ben:www-data拥有的具有770权限的套接字,或者用户ben读/写/执行权限和套接字上的www-data组,这正是你在这种情况下。

I have given path to the sock file outside my project.我已经在我的项目之外给出了 sock 文件的路径。 I needed to just create the directory so that the gunicorn can create the file inside that directory as I had had mentioned that path in the .services file.我只需要创建目录,以便 gunicorn 可以在该目录中创建文件,正如我在 .services 文件中提到的那个路径。 Basically, I made sure that I had all directories existing according to the path in the .services file.基本上,我确保根据 .services 文件中的路径存在所有目录。 No need to change permissions or ownership无需更改权限或所有权

Try run尝试运行

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

The last line helped me to re-create .scok file最后一行帮助我重新创建 .scok 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM