【Flask-1】Nginx 、Gunicorn 、 Supervisor的联合部署

原理图:

nginx:一个反向代理服务器,用来连接本地与互联网

gunicorn:容器,容纳发布在云端的网站

supervisor:进程管理工具,管理gunicorn进程

  • 用gunicorn运行Flask项目

安装gunicorn,需安装在项目使用的虚拟环境中。

pip install gunicorn

当我们安装好 gunicorn 之后,需要用 gunicorn 启动 flask,注意test.py代码中的name启动了 app.run(),这个含义是用 flask 自带的服务器启动 app。这里我们使用了 gunicorn,test.py 就等同于一个库文件,被gunicorn 调用。

启动Flask有下述两种方式:

1、手动命令启动

# 在test.py的目录下
gunicorn -w 4 -b (0.0.0.0):5000 test:app

浏览器中访问http://10.1.93.28:5000,这里的10.1.93.28是ubuntu系统的ip地址。

-w 表示开启多少个 worker,-b 表示 gunicorn 开发的访问地址,是指绑定本机ip,可以省略掉不写,使用的端口是5000,test为模块名,test:app意味着我的test.py中的服务器实例化对象为app。

另:work count=(2*cpu count)+1

# 查询CPU数量
import multiprocessing
print(multiprocessing.cpu_count())

2、创建配置文件启动

# gunicron.py
worker = 4
worker_class ="gevent"  # 采用gevent库,支持异步处理请求
bind = "0.0.0.0:8000"

启动命令:

gunicorn -c gunicorn.py test:app

另:相关命令:

# 查看Gunicorn进程:
ps -aux | grep gunicorn
# pid是第二列
kill -9 进程号
# 杀掉gunicorn进程
pkill gunicorn
  • 使用nginx实现本地访问服务器端发布的网站

nginx安装命令:

sudo apt-get install nginx

编辑配置文件/etc/nginx/sites-available/default,修改location/如下

# 注意下这里的监听端口,访问的时候会用到
listen 80 default_server;
listen [::]:80 default_server;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
    proxy_pass http://localhost:9000/;
    proxy_redirect off;

    proxy_set_header Host $http_post;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

接下来重启nginx服务

sudo /etc/init.d/nginx restart

这时候一定要确保gunicorn服务已经启动,然后访问http://10.1.93.28

其他相关命令:

# 关闭nginx
sudo /etc/init.d/nginx stop
# 重启nginx
sudo /etc/init.d/nginx restart
  • supervisor 配置管理进程

supervisor不是必需品,若不嫌麻烦,每次都可以通过gunicorn来启动,并且kill进程来关闭。

supervisor安装命令:

pip install supervisor
#或通过apt安装,通过此方式安装后会自动加入到系统服务里,随着系统启动而启动 
sudo apt install supervisord

1、生成配置文件:

# 创建 /etc/supervisor 文件夹
mkdir /etc/supervisor
# 生成默认supervisord.conf 文件
echo_supervisord_conf > /etc/supervisor/supervisord.conf
# 为supervisord.conf增加执行权限
chmod +x /etc/supervisor/supervisord.conf

2、在/etc/supervisor/中创建conf.d文件夹

# 创建 /etc/supervisor/conf.d 文件夹 
mkdir /etc/supervisor/conf.d

3、修改supervisord.conf最后的[include]部分配置:

[include] 
files = /etc/supervisor/conf.d/*.conf

这样就可以支持子配置文件,而不用改动主配置文件。

4、在/etc/supervisor/conf.d/中新增子进程配置文件test.conf。

# 创建test.conf 文件
vim /etc/supervisor/conf.d/test.conf
# 为hello.conf增加执行权限
chomod +x hello.conf

创建test.conf配置文件

[program:test]
command=/home/zxh/anaconda3/envs/yolov5/bin/gunicorn -w 4 -b :9000 test:app
directory=/home/zxh/impediment_perception_service  //项目目录
autostart=true
autorestart=true
user=zxh
redirect_stderr=true

在上面的配置文件中,[program:hello]设置了进程名,这与之后操作进程的状态名称有关,为test;command为进程运行的命令,必须使用绝对路径,并且使用虚拟环境下的gunicorn命令;user指定了运行进程的用户。

注:hello.conf中不要有汉字

5、配置supervisor开机自启动

在/lib/systemd/system/中添加supervisord.service文件,配置如下:

[Unit]
Description=Supervisord Service

[Service]
Restart=on-failure
RestartSec=42s
User=zxh
ExecStart=/home/zxh/anaconda3/bin/supervisord -n -c /etc/supervisor/supervisord.conf

[Install]
WantedBy=multi-user.target

执行如下命令:

sudo systemctl daemon-reload
sudo systemctl enable supervisord.service
sudo systemctl start supervisord.service

最后重启就可以看到,supervisor已经自启动。

  • 启动这个demo进程
# 权限在test.conf中的user设置
supervisord -c /etc/supervisor/supervisord.conf

supervisor停止命令:

service supervisor stop
nginx是一个高性能的HTTP和反向代理服务器,可以用来处理静态文件和动态请求。gunicorn是一个Python WSGI HTTP服务器,可以将Flask应用程序部署到生产环境中。supervisor是一个进程控制系统,可以用来管理和监控进程。这三个工具可以一起使用部署Flask应用程序。 以下是使用nginxgunicornsupervisor部署Flask应用程序的步骤: 1. 安装nginxgunicornsupervisor。 2. 编写Flask应用程序,并使用工厂函数构建应用程序对象。 3. 创建一个gunicorn配置文件,例如gunicorn.conf.py,指定工作进程数和线程数。 4. 使用gunicorn启动Flask应用程序,例如: ```shell gunicorn -c gunicorn.conf.py "my_project:create_app()" ``` 这将启动一个gunicorn进程,监听8000端口,并将请求转发到Flask应用程序。 5. 配置nginx,将请求转发到gunicorn进程。例如,在nginx.conf文件中添加以下内容: ```nginx server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` 这将将所有请求转发到gunicorn进程,并将请求头中的一些信息传递给Flask应用程序。 6. 使用supervisor管理gunicorn进程。例如,在supervisor.conf文件中添加以下内容: ```ini [program:gunicorn] command=/path/to/gunicorn -c /path/to/gunicorn.conf.py "my_project:create_app()" directory=/path/to/project user=user autostart=true autorestart=true stopasgroup=true killasgroup=true ``` 这将启动一个名为gunicorn的进程,并在系统启动时自动启动该进程。如果该进程崩溃或被杀死,supervisor将自动重新启动该进程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值