Flask是一个基于Python编写的轻量级Web框架,它简单易用、灵活且功能强大。本文将介绍如何在CentOS7上使用uwsgi来部署Flask在线Web服务。
安装依赖
在开始前,请确保您的CentOS7服务器已经安装了Python和pip包管理工具。如果没有安装,请先执行以下命令:
sudo yum install python3 -y
sudo yum install python3-pip -y
安装完成后,可以使用以下命令来验证是否安装成功:
python3 --version
pip3 --version
安装Flask和uwsgi
使用pip3命令安装Flask和uwsgi:
pip3 install flask uwsgi
编写Flask应用程序
创建一个名为app.py的Python文件,并输入以下代码:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Flask!'
if __name__ == '__main__':
app.run()
这是一个简单的Flask应用程序,当访问根路由时,网页将显示”Hello, Flask!”。
配置uwsgi
创建一个名为app.ini的配置文件,并输入以下内容:
[uwsgi]
module = app:app
master = true
processes = 5
socket = <服务器IP地址>:8000
chmod-socket = 666
vacuum = true
die-on-term = true
请将<服务器IP地址>替换为您的服务器实际IP地址。
使用uwsgi启动应用程序
通过以下命令来启动应用程序:
uwsgi --ini app.ini
您将看到类似如下的输出:
*** Operational MODE: preforking+pthread ***
spawned uWSGI master process (pid: xxxxx)
spawned uWSGI worker 1 (pid: xxxxx, cores: 1)
spawned uWSGI worker 2 (pid: xxxxx, cores: 1)
spawned uWSGI worker 3 (pid: xxxxx, cores: 1)
spawned uWSGI worker 4 (pid: xxxxx, cores: 1)
spawned uWSGI worker 5 (pid: xxxxx, cores: 1)
配置Nginx反向代理
使用Nginx作为反向代理服务器,将请求转发到uwsgi:
编辑Nginx的配置文件(通常位于/etc/nginx/nginx.conf),在http块中添加以下配置:
server {
listen 80;
server_name <服务器IP地址>;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
}
保存文件后,重启Nginx服务:
sudo systemctl restart nginx
完成部署
现在,您已经成功部署了一个Flask在线Web服务。使用浏览器访问服务器的IP地址,您将看到显示”Hello, Flask!”的网页。
如此简单,您就能够在CentOS7上使用uwsgi来部署Flask应用程序了。
希望本文对您有所帮助。如有任何问题,请随时留言。感谢阅读!
本文来自极简博客,作者:狂野之翼喵,转载请注明原文链接:使用uwsgi在CentOS7上部署Flask在线Web服务
微信扫一扫,打赏作者吧~