Flask+Nginx+gunicorn部署

本文介绍如何使用Gunicorn和Nginx部署Flask应用至服务器,包括配置步骤、常见问题解决及守护进程设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了大四校内实习,用写一个微信小程序,所以决定用Flask写/注册登录接口

写完之后就准备要部署到服务器上

首先查看服务器上有无Python3,我的服务器上本来就有Python2.7和Python3.5,然后根据https://www.cnblogs.com/Yanfang20180701/p/10588087.html将默认Python版本改为3.5.

查看有无pip3 没有的话apt-get install pip3,

安装缺少的python包,如果提示Unable to locate package python3-pip3 就查看https://www.cnblogs.com/jp1021/p/9885890.html

如果提示pip3不是最新版本 最好不要更新(我更了之后就无法安装Python包了)

安装完后,安装mysql 根据https://blog.youkuaiyun.com/james_nan/article/details/82053430

然后将自己的Flask项目上传到服务器 比如/root这个目录下 项目名称是login 所以整个结构是/root/login/

然后执行

gunicorn -w 4 -b 127.0.0.1:5000 app:app

第一个app就是引导用的 python 入口文件名称(不包括后缀/模块名)第二个app就是 Flask 实例的名称

查看是否能正常运行 没有报错信息代表配置成功,ctrl+c退出。

安装nginx 直接 apt-get install nginx  然后可以去浏览器访问ip看是否安装成功 出现下图即成功

然后去/etc/nginx/conf.d下新建配置文件 比如default.conf 内容如下:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
server{
     listen 8080;                              #你想访问的服务器端口
     server_name 47.97.214.2;                  #你的服务器ip
     location / {
          proxy_pass http://127.0.0.1:5000;    #gunicorn和nginx通信的端口,需要和接下来 
                                               #gunicorn配置里的端口相同
     }
     error_log logs/error.log error;           #错误日志
}


需要去/usr/share/nginx目录下新建logs文件夹

保存 然后运行 nginx -t 查看配置文件是否有错 没错的话就systemctl restart nginx重启nginx

进入项目目录/root/login下 执行

gunicorn -w 4 -b 127.0.0.1:5000 app:app

然后访问自己的ip:8080 如下图就成功了

如果想退出远程连接后还能继续访问 需要将gunicorn设置为守护进程,在项目目录/root/login下新建gunicorn.conf配置文件,内容为

# 并行工作线程数
workers = 4
# 监听内网端口5000【按需要更改】
bind = '127.0.0.1:5000'
# 设置守护进程【关闭连接时,程序仍在运行】
daemon = True
# 设置超时时间120s,默认为30s。按自己的需求进行设置
timeout = 120
# 设置访问日志和错误信息日志路径
accesslog = './logs/acess.log'
errorlog = './logs/error.log'

保存之后 在当前项目目录下执行

gunicorn app:app -c gunicorn.conf

第一个app是运行模块名 第二个app是应用名

就可以退出远程连接也能访问接口了

如果更改了接口 就需要重启gunicorn 可以参考https://blog.youkuaiyun.com/yiifaa/article/details/78786405
通过执行如下命令,可以获取Gunicorn进程树:

pstree -ap|grep gunicorn

得到如下的结果。

|-grep,6194 --col gunicorn
  |   `-gunicorn,30080 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,4413 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,8030 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,8135 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,8137 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,11532 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,13460 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,19728 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,23585 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,23825 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,27921 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,28899 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,28900 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,28901 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,35637 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,36963 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,43074 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,43118 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,43232 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,43307 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,43308 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,44018 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,46996 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       |-gunicorn,47000 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py
  |       `-gunicorn,47650 /usr/local/bin/gunicorn collect:app -c collect_gunicorn.py

很显然,30080就是Gunicorn的主进程。

 

2. 重启Gunicorn任务
按照官方的推荐方法,执行命令:

# 不需要sudo
kill -HUP 30080

执行上述命令后,再次执行“pstree -ap|grep gunicorn”,我们很容易发现,除了主进程,其他的Gunicorn进程都已经销毁,并新建了进程(进程ID发生了变化)。
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值