gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:34:11 25 4
gpt4 key购买 nike

我正在尝试在 Ubuntu 服务器上部署 Flask 应用程序。我引用了 this , thisthis并在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 error 而终止:

can't find command "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 {
...

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

关于python - 如何在 Ubuntu 服务器上使用 Supervisor/Nginx/Gunicorn 部署 Flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39236595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com