通过CloudPanel+uWSGI部署Flask应用至Azure时遇内部服务器错误
Flask应用部署问题(CloudPanel+uWSGI+Azure)
结果:内部服务器错误(Internal Server Error)
基础信息
- 应用使用Python 3.8.16版本
- 以下内容已隐去实际用户名及域名
- 严格遵循CloudPanel官方部署步骤
- 使用子域名部署
- 应用在本地及虚拟环境开发模式下运行正常
- uWSGI已监听8090端口
应用主模块(api.py)
from flask import Flask, request, jsonify, render_template import joblib app = Flask(__name__) classifier = joblib.load('./model/pipeline.pkl') @app.route('/') def home(): message = "API has been deployed correctly!" return render_template('index.html', message=message) @app.route('/predict', methods=['POST']) def predict(): json_ = request.json prediction = classifier.predict(json_['text']).tolist() return jsonify({'prediction': prediction[0] }) if __name__ == '__main__': classifier = joblib.load('./model/pipeline.pkl') app.run(host='0.0.0.0')
WSGI入口文件(wsgi.py)
from api import app if __name__ == "__main__": app.run()
uWSGI配置文件(ini)
[uwsgi] plugins = python3 master = true protocol = uwsgi socket = 127.0.0.1:8090 chdir = /home/username/htdocs/api.domain.com wsgi-file = /home/username/htdocs/api.domain.com/api/wsgi.py callable = app # In case you're using virtualenv uncomment this: virtualenv = /home/username/htdocs/api.domain.com/env # Needed for OAuth/OpenID buffer-size = 8192 # Reload when consuming too much of memory reload-on-rss = 250 # Increase number of workers for heavily loaded sites workers = 4 # Enable threads for Sentry error submission enable-threads = true # Child processes do not need file descriptors close-on-exec = true # Avoid default 0000 umask umask = 0022 #Run as weblate user uid = username gid = username # Enable harakiri mode (kill requests after some time) # harakiri = 3600 # harakiri-verbose = true # Enable uWSGI stats server # stats = :1717 # stats-http = true # Do not log some errors caused by client disconnects ignore-sigpipe = true ignore-write-errors = true disable-write-exception = true
Nginx虚拟主机配置文件
server { listen 80; listen [::]:80; listen 443 ssl http2; listen [::]:443 ssl http2; {{ssl_certificate_key}} {{ssl_certificate}} server_name api.domain.com; {{root}} {{nginx_access_log}} {{nginx_error_log}} if ($scheme != "https") { rewrite ^ https://$host$uri permanent; } location ~ /.well-known { auth_basic off; allow all; } {{settings}} index index.html; location / { include uwsgi_params; uwsgi_read_timeout 3600; #uwsgi_pass unix:///run/uwsgi/app/weblate/socket; uwsgi_pass 127.0.0.1:8090; } #location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|woff2|eot|mp4|ogg|ogv|webm|webp|zip|swf)$ { # add_header Access-Control-Allow-Origin "*"; # expires max; # access_log on; #} if (-f $request_filename) { break; } }
已执行操作
- 重启Nginx服务
- 重启uWSGI服务
- 重启Azure实例
预期效果
访问站点时显示包含"API has been deployed correctly!"的页面
内容的提问来源于stack exchange,提问作者iamuendo
相关产品推荐
相关产品推荐

