簡體   English   中英

django gunicorn 襪子文件不是由 wsgi 創建的

[英]django gunicorn sock file not created by wsgi

我的數字海洋服務器(Ubuntu 16.04)中有一個基本的 django rest 應用程序,具有本地虛擬環境。 基本的 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

我可以讀到 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"

為什么沒有創建這個 sock 文件? 如何配置 django/gunicorn 來創建這個文件? 我在 Django 項目的 INSTALLED_APP 中添加了 gunicorn 但它沒有改變任何東西。

編輯:

當我使用nginx -t測試 nginx 配置文件時,出現錯誤: open() "/run/nginx.pid" failed (13: Permission denied) 但是,如果我使用 sudo: sudo nginx -t運行命令,則測試成功。 這是否意味着我必須允許“ben”用戶運行 Ngnix?

關於 gunicorn 日志文件,我找不到閱讀它們的方法。 它們存放在哪里?

當我使用ps aux | grep gunicorn檢查 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

這是當您為 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.

我不得不更改我的 sock 文件夾的權限:

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

另一件事是,在閱讀了許多帖子后,我更改了 sock 位置,將 sock 文件保留在 django 項目中不是一個好習慣。 我的新位置是:

/home/ben/run/

不要忘記更改權限:

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

要確保 gunicorn 已刷新,請運行以下命令:

pkill gunicorn
sudo systemctl daemon-reload
sudo systemctl start gunicorn

這將終止 gunicorn 進程並啟動新進程。

您可以運行此命令使進程在服務器啟動時啟動:

sudo systemctl enable gunicorn

現在一切正常。

雖然接受的答案有效,但它存在一個(imo 主要)問題,即 gunicorn Web 服務器(可能)以 root 身份運行,這是不推薦的。 您最終需要 chown 套接字的原因是它歸root:root ,因為這是您的 init 作業默認假定的用戶/組。 有多種方法可以讓您的工作承擔另一個角色。 截至目前(使用 gunicorn 19.9.0),在我看來,最簡單的解決方案是使用作為gunicorn命令的一部分提供的--user--group標志。 這意味着您的服務器可以從您指定的用戶/組開始。 在你的情況下:

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組,這正是你在這種情況下。

我已經在我的項目之外給出了 sock 文件的路徑。 我只需要創建目錄,以便 gunicorn 可以在該目錄中創建文件,正如我在 .services 文件中提到的那個路徑。 基本上,我確保根據 .services 文件中的路徑存在所有目錄。 無需更改權限或所有權

嘗試運行

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

最后一行幫助我重新創建 .scok 文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM