腾讯云服务器部署Nginx+gunicorn+flask,亲测成功部署

需求背景:多用户高并发访问web部署的服务,单靠纯flask是性能不够的,必须要有Nginx+gunicorn的加持。

解决思路:

1.云服务器部署测试纯flask,通过运行调试

2.云服务器分别配置好Nginx、Nginx+gunicorn配合

3.Nginx+gunicorn+flask联调

实现操作:

1.云服务器部署测试纯flask,已经通过,详细见上一篇博文。

2.Nginx配置:

首先是安装 Nginx,在 CentOS 中安装 Nginx 很简单,使用如下命令即可:

yum install nginx -y

若yum报错,则问题可能出在系统中既有python2,也有python3,解决办法是重装系统为“Centos7.6 腾讯云TI平台AI训练加速框架(img-jd96w7yr)”

安装成功后,nginx 还没有启动,要先开启 nginx 服务

systemctl start nginx

然后使用 ps 命令查看 nginx 服务是否成功开启

ps -ef | grep nginx

console控制台出现如下响应,则表明 nginx 服务已经开启

 开启 nginx 成功后,在 windows 浏览器上访问服务器的 80 端口(阿里云上已经配置好80端口了,访问 ip:port,ip是服务器ip,port默认就是80),页面如下,说明 nginx 安装和开启成功。

修改Nginx设置 

nginx 已经安装成功了,但 nginx 默认监听的是80端口,要部署自己的项目,还需要修改配置,增加监听端口和路由转发规则。

nginx 的配置文件是 /etc/nginx/ 下的 nginx.conf

console控制台输入如下代码,修改nginx.conf

# vim nginx.conf

I 代表修改,:wq 代表保存并退出修改界面

默认 user 是 nginx 用户(需要先创建这个用户才行),先将 user 修改为 root (实际项目中一般不会使用 root) 。

然后参照默认监听80端口的配置增加一份 server 配置。这份配置是监听7777端口,这个端口在腾讯云上配置好了,当服务器监听到7777端口的请求时,会将请求转发到内网ip及端口 xx.xx.xx.xx:5000/ (云服务器运行的flask项目)

完整 nginx.conf 如下:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
 
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
 
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
 
events {
    worker_connections 1024;
}
 
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
 
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
 
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    # 务必修改proxy_pass http://xx.xx.xx.xx:5000/  为内网ip及5000端口

    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
 
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
 
        location / {
        }
 
        error_page 404 /404.html;
            location = /40x.html {
        }
 
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    server {
        listen       7777;
        listen       [::]:7777 default_server;
        server_name  flask_project;
 
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
 
        location / {
            proxy_pass http://xx.xx.xx.xx:5000/;
        }
    }
 
# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
 
}

修改完成配置文件后需要重启 nginx ,使配置生效:

systemctl restart nginx

2.2 gunicorn 安装及配置

在运行 纯Flask 部署时,使用的是 Flask 的 runserver 服务器,是直接 python app.py 运行,在电脑上用浏览器访问 http://xx.xx.xx.xx:7777/ (记得将 xx.xx.xx.xx 换成自己云服务器的公网ip),就可以访问到 Flask 项目。

但是,需要注意的是,flask自带的 runserver 只是一个供开发者调试的微型服务器,性能很弱,实际部署时不会这样使用。

通常使用的 HTTP 服务器有 Gunicorn 或 uWsgi ,两个都是满足 Python WSGI 协议的HTTP服务器。使用 uWsgi 需要再配置一份 uWsgi 的配置文件,使用 Gunicorn 会简单些,直接用命令运行代码就可以了,接下来就介绍 Gunicorn 的部署方法。

首先是安装 gunicorn

pip3 install gunicorn

然后使用如下命令,在 gunicorn 上运行 flask 程序(务必将xx.xx.xx.xx:5000 换成自己的云服务器内网ip及端口)

gunicorn -w 1 -b xx.xx.xx.xx:5000 app:app

-w 表示:启动的进程数量。

-b 表示:服务运行的 ip 和端口,与 nginx 配置文件中转发的地址保持一致。

app:app 表示:跟着的是启动文件和 flask 实例名称,启动文件不带后缀 .py,与实例名称中间用冒号连接。

(如果需要以守护进程运行项目的话,再加一个 -D 参数,关于 gunicorn 的更多参数,可以使用 -h 查看帮助信息。)

运行之后,(如果需要的话)可以查看 gunicorn 是否开启成功,也可以查看服务器是否在监听 7777 和 5000 端口。

ps -ef | grep gunicorn
netstat -ntlp

现在,项目运行起来了,在任何一个浏览器上访问 http://云服务器公网ip:5000/ ,功能正常,部署成功。

小结:

1.云服务器部署测试纯flask,通过运行调试,见上一篇博文。

2.云服务器分别配置好Nginx、Nginx+gunicorn配合。

3.Nginx+gunicorn+flask联调,部署成功。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值