CentOS7部署flask项目

Miss.Ceel大约 2 分钟Pythonflask

本想在winserver部署flask项目奈何坑太多,以至于短时间填不平。所以就转到linux系统了。

在服务器上找个目录创建一个python虚拟环境 参考这里open in new window 如果按照上面的教程不能成功的话,就接着往下看吧。因为我按照上面的教程完成了大部分。但仍然没有成功部署。 因为第一次使用linux系统,第一次部署python项目。走了很多弯路...

默认已经安装好 nginx虚拟环境 还有 gunicorn。 如果没有安装好请 参考上面链接安装

我的 flask 项目放在了 /mysource/ceel/pyproject/wallpaper/* 目录。

重点

  1. 激活并进入虚拟环境

  2. 安装项目需要的模块

  3. 启动 gunicorngunicorn -w 2 -b 127.0.0.1:8070 app:app # 指定运行端口 运行 app.py中的 app = flask(name)

  4. 配置启动 nginx 我的做法是在 nginx.conf 文件添加了

     // nginx.conf 文件
     http{
     	....
     	server {
     		listen 8060;
     		server_name 服务器IP地址/域名(***.***.***.***);
     	 
     		location / {
     			proxy_pass http://127.0.0.1:8070;
     			proxy_set_header Host $host;
     			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     			proxy_set_header Host $http_host;
     		}
     	}
     	...
     }
     
     nginx -t 命令检查配置是否正确
     nginx -s reload 重启 nginx
    

可能遇到的问题(nginxgunicorn 都有可能遇到 反正我是遇到了...倒霉):

  • Connection in use: ('127.0.0.1', 8070) [2019-12-16 10:34:27 +0800] [9997] [ERROR] Retrying in 1 second. [2019-12-16 10:34:28 +0800] [9997] [ERROR] Connection in use: ('127.0.0.1', 8070) [2019-12-16 10:34:28 +0800] [9997] [ERROR] Retrying in 1 second. [2019-12-16 10:34:29 +0800] [9997] [ERROR] Connection in use: ('127.0.0.1', 8070) [2019-12-16 10:34:29 +0800] [9997] [ERROR] Retrying in 1 second. [2019-12-16 10:34:30 +0800] [9997] [ERROR] Connection in use: ('127.0.0.1', 8070) [2019-12-16 10:34:30 +0800] [9997] [ERROR] Retrying in 1 second. [2019-12-16 10:34:31 +0800] [9997] [ERROR] Connection in use: ('127.0.0.1', 8070) [2019-12-16 10:34:31 +0800] [9997] [ERROR] Retrying in 1 second. [2019-12-16 10:34:32 +0800] [9997] [ERROR] Can't connect to ('127.0.0.1', 8070) 端口被占用...

解决办法:先检查一下是谁占用了,用kill -9 *** 杀一下进程,如果还是不行用下面的办法 强制关闭端口的占用, sudo fuser -k 8070/tcp

  • 还要增加阿里云或者腾讯云的安全组策略。添加出站入站规则。

最后就能访问了一个壁纸网站open in new window

  1. 通过执行如下命令,可以获取Gunicorn进程树: pstree -ap|grep gunicorn
  2. 重启Gunicorn任务

Python kill -HUP 14226 3. 退出Gunicorn任务

Python kill -9 28097

上次编辑于:
贡献者: misszhangxm
Loading...