【问题标题】:How to deploy Flask app with Supervisor/Nginx/Gunicorn on Ubuntu server如何在 Ubuntu 服务器上使用 Supervisor/Nginx/Gunicorn 部署 Flask 应用程序
【发布时间】:2017-01-07 06:37:22
【问题描述】:

我正在尝试在 Ubuntu 服务器上部署 Flask 应用程序。我参考了thisthisthis,在SO上发现了很多类似的问题,但我还是想不通。

我可以通过执行uwsgi siti_uwsgi.ini 并导航到http://server_IP_address:8080/ 从源目录手动运行它。但是当我尝试uwsgi --socket 127.0.0.1:3031 --wsgi-file views.py --master --processes 4 --threads 2 并导航到http://server_IP_address:3031 时,我什么也得不到。

如果我去siti.company.loc(我设置的DNS名称),有一个标准的Nginx 502错误页面。

当我尝试重新启动主管进程时,它会因 FATAL 错误而死:

找不到命令“gunicorn”

我做错了什么?如果我需要提供更多信息或背景,请告诉我。


/webapps/patch/src/views.py(Flask 应用):

from flask import Flask, render_template, request, url_for, redirect
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources={r"/*": {'origins': '*'}})

@app.route('/')
def home():
    return 'Hello'

@app.route('/site:<site>/date:<int:day>-<month>-<int:year>')
def application(site, month, day, year):
    if request.method == 'GET':
        # Recompile date from URL. todo: better way
        dte = str(day) + "-" + str(month) + "-" + str(
        print('about to run')
        results = run_SITI(site, dte)
        return results

def run_SITI(site, dte):
    print('running SITI')
    return render_template('results.html', site=site, dte=dte, results=None)  # todo: Show results

if __name__ == '__main__':
    app.run(debug=True)

/webapps/patch/siti_wsgi.ini (uWSGI ini):

[uwsgi]
http = :8008
chdir = /webapps/patch/src
wsgi-file = views.py
processes = 2
threads = 2
callable = app

/etc/nginx/sites-available/siti(Nginx 配置):

upstream flask_siti {
        server 127.0.0.1:8008 fail_timeout=0;
}
server {
        listen 80;
        server_name siti.company.loc;
        charset utf-8;
        client_max_body_size 75M;

        access_log /var/log/nginx/siti/access.log;
        error_log /var/log/nginx/siti/error.log;

        keepalive_timeout 5;

        location /static {
                alias /webapps/patch/static;
        }
        location /media {
                alias /webapps/patch/media;
        }
        location / {
                # checks for static file, if not found proxy to the app
                try_files $uri @proxy_to_app;
        }

        location @proxy_to_app {
                proxy_redirect off;
                proxy_set_header Host $http_host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://flask_siti;
        }
}

/etc/supervisor/conf.d/siti.conf(主管配置):

[program:webapp_siti]
command=gunicorn -b views:app
directory=/webapps/patch/src
user=nobody
autostart=true
autorestart=true
redirect_stderr=true

/var/log/nginx/siti/error.log(Nginx错误日志):

2016/08/30 11:44:42 [error] 25524#0: *73 connect() failed (111: Connection refused) while connecting to upstream, $
2016/08/30 11:44:42 [error] 25524#0: *73 connect() failed (111: Connection refused) while connecting to upstream, $
2016/08/30 11:44:42 [error] 25524#0: *73 no live upstreams while connecting to upstream, client: 10.1.2.195, serve$

【问题讨论】:

  • 您使用的是 uwsgi conf 中的http 设置,这是不同的。只需指定一个端口
  • 您在 supervisord conf 中有错字:directory=/webapps/batch 而不是 directory=/webapps/patch
  • @reptilicus 我尝试作为套接字 (uwsgi --socket 127.0.0.1:3031 --wsgi-file views.py --master --processes 4 --threads 2),但 http:\\server_ip_address:3031 没有返回任何内容
  • @TomaszJakubRup 已修复,但这不是问题 - 在复制粘贴问题时发生
  • nginx 配置中的下一个错字:server 127.0.0.1:8008 fail_timeout=0; 而不是server 127.0.0.1:8080 fail_timeout=0;

标签: python nginx flask uwsgi gunicorn


【解决方案1】:

nginx 配置中有错误:

代替:

upstream flask_siti {
        server 127.0.0.1:8008 fail_timeout=0;

server {
   ...

尝试:

upstream flask_siti {
        server 127.0.0.1:8080 fail_timeout=0;
}
server {
   ...

您必须在supervisor 配置中“激活” virtualenv。为此,请将以下行添加到您的 supervisor 配置中:

environment=PATH="/webapps/patch/venv/bin",VIRTUAL_ENV="/webapps/patch/venv",PYTHONPATH="/webapps/patch/venv/lib/python:/webapps/patch/venv/lib/python/site-packages"

【讨论】:

  • 谢谢 - 这是一个真正的错字,今天早些时候我记得收到了一个错误。不过,它并没有为我解决(见编辑)
  • 我会试一试.. 当我尝试重新启动服务时,主管崩溃了(我今天有魔力!)。打算今晚留下,明天早上再看。感谢您的帮助
  • 好的,让主管工作 - 不幸的是,仍然死于同样的 can't find command 'gunicorn' 错误。 Gunicorn 安装在我的 venv 中,所以不知道为什么,但这有帮助......明天会再次拿起
  • 是的。以不同的方式解决。感谢您的所有帮助!
【解决方案2】:

能够通过以下更改使其工作:

/etc/supervisor/conf.d/siti.conf(主管配置):

[program:webapp_siti]
command=/webapps/patch/venv/bin/gunicorn -b :8118 views:app  # didn't use uwsgi.ini after all
directory=/webapps/patch/src
user=nobody
autostart=true
autorestart=true
redirect_stderr=true

/etc/nginx/sites-enabled/siti(Nginx 配置):

upstream flask_siti {
        server 127.0.0.1:8118 fail_timeout=0;  # changed ports because 8008 was already in use by something else
}
# snip ...

原来我已经设置了 uWSGI 来监听端口 8008。我还在 /etc/nginx/sites-enabled 中有一个名为 siti.save 的额外文件,它阻止了 Nginx 重新加载。我删除了它,重新加载/重启了 Nginx,重启了 Supervisor,它工作了。

【讨论】:

    猜你喜欢
    • 2017-02-14
    • 2015-04-21
    • 2021-12-14
    • 2021-09-24
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2021-10-06
    相关资源
    最近更新 更多