【问题标题】:Running Gunicorn behind chrooted nginx inside virtualenv在 virtualenv 中的 chrooted nginx 后面运行 Gunicorn
【发布时间】:2013-05-14 00:38:53
【问题描述】:

如果我手动启动 gunicorn 或将 gunicorn 添加到 django 安装的应用程序中,我可以使用此设置。但是当我尝试使用 systemd 启动 gunicorn 时, gunicorn 套接字和服务启动正常,但它们对 Nginx 没有任何帮助;我得到一个 502 错误网关。

Nginx 在“http”用户/组、chroot jail 下运行。我使用 pythonbrew 来设置 virtualenvs,因此 gunicorn 安装在我的主目录中的 .pythonbrew 下。 vitualenv 目录归我的用户和 adm 组所有。

我很确定某处存在权限问题,因为如果我启动 gunicorn 一切正常,但如果 systemd 启动它就不行。我尝试更改 gunicorn.service 文件中的用户和组指令,但没有任何效果;如果 root 启动服务器,那么我不会收到任何错误和 502,如果我的用户启动它,我不会收到任何错误和 504。

我检查了 Nginx 日志并且没有错误,所以我确定这是一个 gunicorn 问题。我应该在 app 目录中有 virtualenv 吗?谁应该是应用程序目录的所有者?如何缩小问题范围?

/usr/lib/systemd/system/gunicorn-app.service

#!/bin/sh

[Unit]
Description=gunicorn-app

[Service]
ExecStart=/home/noel/.pythonbrew/venvs/Python-3.3.0/nlp/bin/gunicorn_django
User=http
Group=http
Restart=always
WorkingDirectory = /home/noel/.pythonbrew/venvs/Python-3.3.0/nlp/bin

[Install]
WantedBy=multi-user.target

/usr/lib/systemd/system/gunicorn-app.socket

[Unit] 
Description=gunicorn-app socket 

[Socket] 
ListenStream=/run/unicorn.sock 
ListenStream=0.0.0.0:9000 
ListenStream=[::]:8000 

[Install] 
WantedBy=sockets.target

我意识到这是一个庞大的问题,但我确信我可以通过一些指示来查明问题。谢谢。

更新

我开始缩小范围。当我手动运行 gunicorn 然后运行 ​​ps aux|grep gunicorn 时,我看到两个进程已启动:master 和 worker。但是当我用 systemd 启动 gunicorn 时,只启动了一个进程。我尝试将Type=forking 添加到我的gunicorn.services 文件中,但是在加载服务时出现错误。我想也许 gunicorn 没有在 virtualenv 下运行或者 venv 没有被激活?

有谁知道我在这里做错了什么?也许 gunicorn 没有在 venv 中运行?

【问题讨论】:

    标签: django nginx permissions gunicorn


    【解决方案1】:

    我在 OSX 上使用 launchd 时遇到了类似的问题。 问题是我需要允许该进程产生子进程。

    尝试添加Type=forking:

    [Unit]
    Description=gunicorn-app
    
    [Service]
    Type=forking
    

    【讨论】:

    • 我尝试添加分叉,但服务根本无法加载。
    【解决方案2】:

    我知道这不是最好的方法,但我能够通过将 gunicorn 添加到 django INSTALLED_APPS 列表中来使其工作。然后我刚刚创建了一个新的 systemd 服务:

    [Unit]
    Description=hack way to start gunicorn and django
    
    [Service]
    User=http
    Group=http
    ExecStart=/srv/http/www/nlp.com/nlp/bin/python /srv/http/www/nlp.com/nlp/nlp/manage.py run_gunicorn
    Restart=always
    [Install]
    WantedBy=multi-user.target
    

    一定有更好的方法,但从缺乏回应来看,没有多少人知道更好的方法是什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-30
      • 2018-12-08
      • 1970-01-01
      • 2015-04-22
      • 2013-01-11
      • 2019-02-28
      • 2013-06-08
      • 2012-02-16
      相关资源
      最近更新 更多