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
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
/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
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 -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,参数告诉gunicorn如何加载应用程序实例。冒号前面的名称是包含应用程序的模块,冒号后面的名称是此应用程序的名称。
yum install -y nginx supervisor
安装版本为nginx:1.20.1
,supervisor:3.4.0
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: ....supervisor/xmlrpc.py
异常。service supervisord start && supervisorctl reload
systemctl restart supervisord && supervisorctl reload
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