【问题标题】:Flask - Deployment with Gunicorn, Nginx and Supervisor, Supervisor error logFlask - 使用 Gunicorn、Nginx 和 Supervisor 部署,Supervisor 错误日志
【发布时间】:2015-04-21 02:14:01
【问题描述】:

我已经使用 Gunicorn + Nginx + Supervisor 部署了一个 Flask 应用程序。它不起作用。 在线搜索发现了几份类似问题的报告,但没有一个提供适合我们情况的解释或解决问题的修复程序。 Here 说要关闭守护进程 gunicorn。如果我是对的,那不是我的情况。

似乎有什么东西已经启动了一个监听 8000 端口的服务。 Supervisor 每秒都会向日志文件发送错误。

什么可以帮助我?

来自 Supervisor app-stderr.log 的错误:

[2015-02-19 18:56:19 +0300] [964] [INFO] Starting gunicorn 19.2.1
[2015-02-19 18:56:19 +0300] [964] [INFO] Listening at: http://127.0.0.1:8000 (964)
[2015-02-19 18:56:19 +0300] [964] [INFO] Using worker: sync
[2015-02-19 18:56:19 +0300] [1078] [INFO] Booting worker with pid: 1078
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
[2015-02-19 18:56:20 +0300] [1081] [INFO] Starting gunicorn 19.2.1
[2015-02-19 18:56:20 +0300] [1081] [ERROR] Connection in use: ('localhost', 8000)
[2015-02-19 18:56:20 +0300] [1081] [ERROR] Retrying in 1 second.
[2015-02-19 18:56:21 +0300] [1081] [ERROR] Connection in use: ('localhost', 8000)
[2015-02-19 18:56:21 +0300] [1081] [ERROR] Retrying in 1 second.
[2015-02-19 18:56:22 +0300] [1081] [ERROR] Connection in use: ('localhost', 8000)
[2015-02-19 18:56:22 +0300] [1081] [ERROR] Retrying in 1 second.
[2015-02-19 18:56:23 +0300] [1081] [ERROR] Connection in use: ('localhost', 8000)
[2015-02-19 18:56:23 +0300] [1081] [ERROR] Retrying in 1 second.
[2015-02-19 18:56:24 +0300] [1081] [ERROR] Connection in use: ('localhost', 8000)
[2015-02-19 18:56:24 +0300] [1081] [ERROR] Retrying in 1 second.
[2015-02-19 18:56:25 +0300] [1081] [ERROR] Can't connect to ('localhost', 8000)
[2015-02-19 18:56:25 +0300] [1078] [INFO] Worker exiting (pid: 1078)
[2015-02-19 18:56:25 +0300] [1122] [INFO] Booting worker with pid: 1122
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
[2015-02-19 18:56:27 +0300] [1148] [INFO] Starting gunicorn 19.2.1
[2015-02-19 18:56:27 +0300] [1148] [ERROR] Connection in use: ('localhost', 8000)
[2015-02-19 18:56:27 +0300] [1148] [ERROR] Retrying in 1 second.
[2015-02-19 18:56:28 +0300] [1148] [ERROR] Connection in use: ('localhost', 8000)
[2015-02-19 18:56:28 +0300] [1148] [ERROR] Retrying in 1 second.
[2015-02-19 18:56:29 +0300] [1148] [ERROR] Connection in use: ('localhost', 8000)
[2015-02-19 18:56:29 +0300] [1148] [ERROR] Retrying in 1 second.
[2015-02-19 18:56:30 +0300] [1148] [ERROR] Connection in use: ('localhost', 8000)
[2015-02-19 18:56:30 +0300] [1148] [ERROR] Retrying in 1 second.
[2015-02-19 18:56:31 +0300] [1148] [ERROR] Connection in use: ('localhost', 8000)
[2015-02-19 18:56:31 +0300] [1148] [ERROR] Retrying in 1 second.
[2015-02-19 18:56:32 +0300] [1148] [ERROR] Can't connect to ('localhost', 8000)
[2015-02-19 18:56:32 +0300] [1122] [INFO] Worker exiting (pid: 1122)
[2015-02-19 18:56:32 +0300] [1206] [INFO] Booting worker with pid: 1206
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
[2015-02-19 18:56:32 +0300] [1211] [INFO] Starting gunicorn 19.2.1
[2015-02-19 18:56:32 +0300] [1211] [ERROR] Connection in use: ('localhost', 8000)

我的主管.conf:

[program:app]
command = /home/www/app/flask/bin/gunicorn app:app -b localhost:8000 --preload
directory = /home/www/app
user = webhost

Netstat -tulpin

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      930/nginx
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      855/sshd
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      1392/python3
tcp6       0      0 :::22                   :::*                    LISTEN      855/sshd

还有 ps aux | grep 蟒蛇

root       954  0.1  2.4  60564 12440 ?        Ss   18:56   0:01 /usr/bin/python /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
webhost   1392  0.0  3.2  67148 16512 ?        S    19:00   0:00 /home/www/app/flask/bin/python3 /home/www/app/flask/bin/gunicorn app:app -b localhost:8000
webhost   2124  5.2  4.2  86072 21344 ?        S    19:12   0:00 /home/www/app/flask/bin/python3 /home/www/app/flask/bin/gunicorn app:app -b localhost:8000
webhost   2126  5.2  3.2  67148 16488 ?        S    19:12   0:00 /home/www/app/flask/bin/python3 /home/www/app/flask/bin/gunicorn app:app -b localhost:8000
webhost   2130  0.0  0.1  11744   920 pts/0    S+   19:13   0:00 grep --color=auto python

UPD。 主管配置在上面

nginx.conf

server {
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
    location /static {
        alias  /home/www/myapp/app/static/;
    }
}

文件结构

myapp
  |
  |-app-
  |    |
  |    __init__.py  
  |
  run.py
  |
  config.py

__init__.py

from flask import Flask

app = Flask(__name__)
app.config.from_object('config')

from app import views

运行.py

#!flask/bin/python
from app import app
app.run()

config.py

# For valid forms
CSRF_ENABLED = True
SECRET_KEY = 'never-guess'

【问题讨论】:

  • 你能提供你的应用初始化设置,nginx conf,supervisor conf吗?
  • @Ibrahim 已更新。我已经发布了主管配置。
  • 需要你的app/__init__.py 创建app源码。给你一些提示可能会发生什么,在 gunicorn 设置中,你指定端口 8000 来运行应用程序,但应用程序在 5000 上运行,这是烧瓶应用程序运行的默认端口。
  • @Ibrahim 请提供更多提示。 “需要你的 app/__init__.py 创建应用源。”这是什么意思?在我看来 - 主管启动 Gunicorn。 Gunicorn 启动 run.py。并 run.py 创建 _init_.py

标签: python nginx gunicorn supervisord


【解决方案1】:

我吓坏了。感谢@易卜拉欣 我通过 gunicorn 在 run.py

中启动了一个应用程序
#!flask/bin/python
from app import app
app.run()

所以 gunicorn 创建了进程和app.run()。 它们相互冲突。 刚刚删除了那个字符串。

【讨论】:

  • 是的,这就是我想说的。
【解决方案2】:

我遇到了同样的问题,通过调整 Alwx 的答案,我的解决方案是仅在模块被称为 main (run.py) 时才运行应用程序:

from my_app import create_app

app = create_app()
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)

然后我用 gunicorn 运行它:

gunicorn run:app

我仍然可以像以前一样只用python run.py 运行它

【讨论】:

    猜你喜欢
    • 2014-03-31
    • 2020-12-26
    • 2013-10-05
    • 2015-08-01
    • 2017-02-14
    • 2016-04-23
    • 2018-11-27
    • 2017-01-07
    • 2013-01-06
    相关资源
    最近更新 更多