python - 无法用织物启动 gunicorn

标签 python fabric gunicorn

我的代码位于远程服务器的/home/ubuntu/api 中。 WSGI 对象被命名为 app,它存在于 /home/ubuntu/api/api.py 中。我的 gunicorn conf 文件名为 gunicorn.conf.py 并且存在于 /home/ubuntu/api

我的gunicorn.conf.py

import multiprocessing

bind = "127.0.0.1:8000"
workers = multiprocessing.cpu_count() * 2 + 1
backlog = 2048
worker_class = 'gevent'
daemon = True
debug = True
loglevel = 'debug'
accesslog = '/mnt/log/gunicorn_access.log'
errorlog = '/mnt/log/gunicorn_error.log'
max_requests = 1000
graceful_timeout = 20

我正在尝试通过 fabric 在服务器上远程启动 gunicorn。我的 Fabric 代码看起来像这样

@roles('stag_api')
def run_server():
    with cd('/home/ubuntu/api'):
        sudo('gunicorn -c gunicorn.conf.py api:app')

现在 fabric 没有显示任何错误,但是 gunicorn 没有启动。

所以我在 /home/ubuntu/api 中创建了 __init__.py 使其成为一个包。我在 __init__.py 文件中写了这个

from api import app

这使得 WSGI app 在包的命名空间中可用。然后我把我的 Fabric 代码改成了这个

@roles('stag_api')
def run_server():
    sudo('gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app')

即使现在 fabric 没有显示任何错误,但是 gunicorn 没有启动。

所以我创建了一个名为 server 的 shell 脚本,它的代码如下所示

if [ "$1" = "start" ]; then
        echo 'starting'
        gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app
fi

if [ "$1" = "stop" ]; then
        echo 'stopping'
        pkill gunicorn
fi

if [ "$1" = "restart" ]; then
        echo 'restarting'
        pkill gunicorn
        gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app
fi

我把这个 shell 脚本放在 /home/ubuntu/api

现在我的 Fabric 代码看起来像这样

@roles('stag_api')
def stag_server(action='restart'):
    if action not in ['start', 'stop', 'restart']:
        print 'not a valid action. valid actions are start, stop and restart'
        return
    sudo('./server %s' % action)

现在,当我尝试通过 fabric 启动服务器时,它会打印 starting 因此 shell 脚本正在执行并且到达了 if block ,但我仍然无法启动服务器通过 fabric。

但是如果我通过 ssh 连接到服务器并执行 sudo ./server start,gunicorn 就会启动。

谁能解释一下我做错了什么?

最佳答案

关于python - 无法用织物启动 gunicorn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17748234/

相关文章:

python - PyCharm 中基于关键字的注释颜色

python - 如何从字典中删除比 python 中的特定长度短的键?

python - 如何使用 Fabric 传递 SSH 选项?

python - kill linux 命令执行后结构进程中断

python - Selenium ( python ): can't switch to iframe (name is dynamically generated)

python - 随机数生成器仅打印一个数字

python - 打印命令时如何在结构中隐藏密码?

python - gunicorn - 如果从 shell 脚本执行则不获取配置文件

python - 登录 Django 和 gunicorn

python - Ubuntu 16.04/Django - gunicorn - Worker 启动失败