如何在Ubuntu服务器上使用Supervisor/Nginx/Gunicorn部署Flask应用程序



我正在尝试在Ubuntu服务器上部署Flask应用程序。我参考了这个,这个和这个,在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错误页面。

当我尝试重新启动监视器进程时,它死亡并出现致命错误:

找不到命令"gunicorn"

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


/webapps/patch/src/views.py (Flask app):

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/sit_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:

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/site .conf (supervisor config):

[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$

您在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"

通过以下更改可以使其工作:

/etc/supervisor/conf.d/site .conf (supervisor config):

[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:

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,它工作了

相关内容

最新更新