华为云部署Centos7.6 Flask+Gunicorn+Gevent+Supervisor+Nginx

Flask+Gunicorn+Gevent+Supervisor+Nginx是相对比较成熟的部署方案。

参考https://blog.csdn.net/gh254172840/article/details/81224921完成了在华为云上的部署。

基础环境

华为云ECS
OS:CentOS7.6
Python:Python3.6.8

配置pip

  • 升级pip,否则不能使用pip config命令。升级后pip版本为21.3
pip3 install -U pip -i https://repo.huaweicloud.com/repository/pypi/simple
  • 设置pip
pip3 config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple

安装Python第三方包

  • 安装Python3开发包,否则出现gcc命令异常。
yum install -y python3-devel
  • 安装gunicorn gevent`` flask
pip3 install gunicorn gevent flask

最终安装版本为:

Flask==2.0.2
gevent==21.8.0
gunicorn==20.1.0

测试Flask

  • /root目录创建manage.py
vi manage.py

内容如下:

# -*- coding:utf-8 -*-
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run()
  • 设置环境变量
export FLASK_APP=manage.py
  • 测试运行Flask
python3 manage.py

运行信息如下:

 * Serving Flask app 'manage' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

测试运行gunicorn

  • 测试同步模式
gunicorn -b localhost:8001 -w 4 manage:app
[2021-11-27 12:40:27 +0800] [8167] [INFO] Starting gunicorn 20.1.0
[2021-11-27 12:40:27 +0800] [8167] [INFO] Listening at: http://127.0.0.1:8001 (8167)
[2021-11-27 12:40:27 +0800] [8167] [INFO] Using worker: sync
[2021-11-27 12:40:27 +0800] [8170] [INFO] Booting worker with pid: 8170
[2021-11-27 12:40:27 +0800] [8171] [INFO] Booting worker with pid: 8171
[2021-11-27 12:40:27 +0800] [8172] [INFO] Booting worker with pid: 8172
[2021-11-27 12:40:27 +0800] [8174] [INFO] Booting worker with pid: 8174
  • 测试异步模式
gunicorn -b localhost:8001 -w 4 -k gevent manage:app
[2021-11-27 12:40:51 +0800] [8178] [INFO] Starting gunicorn 20.1.0
[2021-11-27 12:40:51 +0800] [8178] [INFO] Listening at: http://127.0.0.1:8001 (8178)
[2021-11-27 12:40:51 +0800] [8178] [INFO] Using worker: gevent
[2021-11-27 12:40:51 +0800] [8181] [INFO] Booting worker with pid: 8181
[2021-11-27 12:40:51 +0800] [8182] [INFO] Booting worker with pid: 8182
[2021-11-27 12:40:51 +0800] [8183] [INFO] Booting worker with pid: 8183
[2021-11-27 12:40:51 +0800] [8184] [INFO] Booting worker with pid: 8184

参数解释:
-w: 指定worker的数量(根据实际情况设定,一般是cpu核数*2 + 1)
-b:指定绑定的地址和端口号
-k: 指定worker-class模式,默认为sync,这里用gevent使之变为异步协程,提高性能。
最后指定app的位置manage:app,参数告诉g​​unicorn如何加载应用程序实例。冒号前面的名称是包含应用程序的模块,冒号后面的名称是此应用程序的名称。

配置supervisor

  • 安装nginx和supervisor
 yum install -y nginx supervisor

安装版本为nginx:1.20.1supervisor:3.4.0

  • 添加supervisor子配置
vi /etc/supervisord.d/manage.ini

内容如下:

[program:manage]
 
; supervisor启动命令 即在bash使用的命令,注意要是在虚拟环境下要写命令的绝对地址
command = gunicorn -w 4 -b 127.0.0.1:8001 -k gevent manage:app
 
; 项目目录
directory = /root
 
; 是否自启动
autostart=true
 
; 是否自己重启
autorestart=true
 
; 启动时间
startsecs = 5
 
; 终止等待时间
startretries = 3
 
user = root
  • 重新加载配置,避免提示error: <class ‘FileNotFoundError’> ....supervisor/xmlrpc.py异常。
service supervisord start && supervisorctl reload
systemctl restart supervisord  && supervisorctl reload

配置Nginx

  • 设置反向代理
vi /etc/nginx/conf.d/manage.conf

内容如下:

server {
    
    
    listen       80;
    server_name  xxxxx;


    location / {
    
    
        proxy_pass              http://127.0.0.1:8001;
        proxy_redirect          off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
  • 重新启动服务
systemctl restart nginx.service

参考

https://blog.csdn.net/gh254172840/article/details/81224921

猜你喜欢

转载自blog.csdn.net/mighty13/article/details/121571451