簡體   English   中英

如何在Ubuntu服務器上使用Supervisor / Nginx / Gunicorn部署Flask應用

[英]How to deploy Flask app with Supervisor/Nginx/Gunicorn on Ubuntu server

我正在嘗試在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錯誤頁面。

當我嘗試重新啟動主管進程時,它死於致命錯誤:

找不到命令“ 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$

您在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 {
   ...

你必須“激活”中的virtualenv supervisor配置。 為此,請將以下行添加到您的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/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,它正常工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM