如何在centos7上部署flask项目
方法1:(使用flask内置的web服务器)
- 配置flask项目: app.run(host=‘0.0.0.0’,port=5000)
- 打开防火墙5000端口:
开启防火墙 service firewalld start
(查看防火墙状态 systemctl status firewalld
开启防火墙 systemctl start firewalld ) - 添加指定需要开放的端口:
firewall-cmd --add-port=5000/tcp --permanent
(移除指定端口:firewall-cmd --permanent --remove-port=123/tcp) - 重载入添加的端口:
firewall-cmd --reload - 查询指定端口是否开启成功:
firewall-cmd --query-port=5000/tcp
方法2:(flask + gunicorn + nginx + supervisor)
为了稳定、同时可以承受大量的请求,使用flask + gunicorn + nginx + supervisor是最常用的flask应用部署方案。
WSGI简介
它的全称是web server gateway interface,即web服务器网关接口,专门用来解决web服务器和web应用程序/框架的兼容性问题。
常见的WSGI:
- Gunicorn
- uWSGI
- Gevent
- Twisted Web
- Proxy Setups
Gunicorn的使用较为简单,所以本文采用Gunicorn作为WSGI。
- 安装方法:pip install gunicorn 或者 pip3 install gunicorn
- 启动gunicorn服务:
gunicorn -w 2 -b :5000 hello:app
-w指的是处理请求的进程数,
-b指的是绑定本机的ip,可以省略不写,使用的端口是5000
hello:app指的是hello.py中
app=Flask(__name__)
更多参数可以参照gunicorn -h
nginx简介
nginx是一个功能强大的反向代理服务器。使用nginx,来弥补gunicorn的不足,比如SSL支持、高并发处理、负载均衡处理等,同样的,它也可以托管一些静态文件。
- 安装方法:yum install nginx
- 启动nginx服务:systemctl start nginx.service
supervisor简介
supervisor是一个用Python语言编写的进程管理工具,可以监听、启动、停止、重启一个或多个进程。
- 安装方法:pip install supervisor 或者 pip3 install supervisor
- 启动supervisor服务:/etc/init.d/supervisor start
以下是我在centos7使用flask + gunicorn + nginx + supervisor来部署的全过程。
-
添加一个普通用户
- adduser grand #新增用户,用户名为grand
- passwd grand #修改密码
- gpasswd -a grand wheel #添加到sudo用户组
- su grand #切换到grand用户
-
初始化环境
- sudo yum install epel-release #初始化仓库
- sudo yum install gcc nginx #安装gcc和Nginx
-
创建虚拟环境
- pip3 install virtualenv #安装virtualebnv
- mkdir /home/grand/textweb #新建目录
- cd /home/grand/textweb #进入目录
- virtualenv textenv #创建虚拟环境目录
- source textenv/bin/activate #激活新创建的虚拟环境 (deactivate #退出虚拟环境)
-
初始化一个简单的flask项目
- pip3 install flask gunicorn #安装flask和gunicorn
- vim hello.py #新建一个flask文件
- python text.py #启动测试flask服务
-
打开防火墙5000端口:
- 开启防火墙 service firewalld start
(查看防火墙状态 systemctl status firewalld) - 添加指定需要开放的端口:firewall-cmd --add-port=5000/tcp --permanent
(移除指定端口:firewall-cmd --permanent --remove-port=123/tcp) - 重载入添加的端口:firewall-cmd --reload
- 查询指定端口是否开启成功:firewall-cmd --query-port=5000/tcp
- 开启防火墙 service firewalld start
-
启动gunicorn服务
- gunicorn -w 2 -b :5000 hello:app
-w指的是处理请求的进程数,
-b指的是绑定本机的ip,可以省略不写,使用的端口是5000
hello:app指的是hello.py中的flask app,
更多参数可以参照gunicorn -h - 浏览器访问:http://xxxxxxxx:5000
- ctrl+c 退出gunicorn服务
- gunicorn -w 2 -b :5000 hello:app
-
配置nginx
- cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak 备份
- sudo vim /etc/nginx/nginx.conf 编辑
#如果有多台服务器的话,可以使用upstream
#flask是组名,可以写其他的
upstream flask {
server 10.211.55.2:5000;
server 10.211.55.2:5001;
}
server {
listen 80;
server_name localhost; #改为自己的域名,如果没有域名就修改为127.0.0.1 或者 localhost
location / {
proxy_pass http://10.211.55.2:5000; # 此处可以写单个主机信息(前面的gunicorn启动的ip地址),也可以写upstream的组名。
# proxy_pass http://flask;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
- 部署
- sudo systemctl start nginx.service 启动nginx
- gunicorn -w 2 -b :5000 hello:app 启动gunicorn服务
(sudo systemctl stop nginx.service 关闭 nginx )
(ctrl+c 退出gunicorn服务)
下面看个人需求来添加使用,如果不想添加进程管理,那么每次都要手动来启动nginx、gunicorn服务;若是添加了进程管理,那么可以把它设置成自启动,这样会方便很多。
9. 创建进程管理
+ pip3 install supervisor 安装supervisor
+ vim /etc/supervisor/conf.d/gunicorn.conf 配置gunicorn文件
+ vim /etc/supervisor/conf.d/nginx.conf 配置nginx文件
+ /etc/init.d/supervisor start 启动supervisor
+ ps ax | grep nginx 查看进程是否打开
+ ps ax | grep gunicorn