nginx+gunicorn+flask的Web服务部署详细流程

插件的选择

flask

  • 1、使用WSGI协议的web框架
  • 2、web部署灵活快捷
  • 3、框架自带实现了WSGI协议的server ,但只用于调试,性能不能得到保障

gunicorn

  • 1、实现了WSGI协议的服务器层,主要是将HTTP协议转换为WSGI协议,可供python解析
  • 2、内置了gevent来提高服务器性能,想要发挥多核的优势可配合多进程使用

nginx

  • 1、可理解为缓冲层,也是属于服务器层次
  • 2、对静态文件的支持好
  • 3、提高并发度
  • 4、负载均衡
  • 5、反向代理

Web部署流程

flask

安装flask

sudo pip3 install flask

检验是否安装成功

Python 3.5.2 (default, Oct  8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>> from flask import Flask

gunicorn

安装gunicorn

sudo pip3 install greenlet
sudo pip3 install gevent
sudo pip3 install gunicorn

测试gunicorn,编写一个简单的web应用程序

文件"demo.py"

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_wprld():
    return "hello world!"

if __name__ == "__main__":
    app.run()

终端输入命令

sudo gunicorn -w 4 -b 0.0.0.0:10001 demo:app

或者编写一个conf配置文件"demo.conf"

bind = '0.0.0.0:10001'
workers = 4
backlog = 2048
worker_class = "gevent" #sync, gevent,meinheld
daemon = False           # 是否后台运行

执行命令

sudo gunicorn -c demo.conf demo:app

显示为以下结果表示测试成功

[2019-10-22 18:57:51 +0800] [207368] [INFO] Starting gunicorn 19.9.0
[2019-10-22 18:57:51 +0800] [207368] [INFO] Listening at: http://0.0.0.0:10001 (207368)
[2019-10-22 18:57:51 +0800] [207368] [INFO] Using worker: sync
[2019-10-22 18:57:51 +0800] [207371] [INFO] Booting worker with pid: 207371
[2019-10-22 18:57:51 +0800] [207373] [INFO] Booting worker with pid: 207373
[2019-10-22 18:57:52 +0800] [207376] [INFO] Booting worker with pid: 207376
[2019-10-22 18:57:52 +0800] [207377] [INFO] Booting worker with pid: 207377

在浏览器中输入ip:port 可看到以下画面
在这里插入图片描述

nginx

nginx安装

sudo apt install nginx

备份nginx的默认配置文件

cp /etc/nginx/sites-available/default /etc/nginx/sites-availabel/default.bak

修改default文件

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass http://0.0.0.0:10001;
                proxy_redirect     off;
                proxy_set_header   Host                 $http_host;
                proxy_set_header   X-Real-IP            $remote_addr;
                proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto    $scheme;
                # ment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
}

常用nginx命令

sudo /etc/init.d/nginx start # 启动 
sudo /etc/init.d/nginx stop # 停止 
sudo /etc/init.d/nginx restart  # 重启

输入命令

sudo /etc/init.d/nginx start

在浏览器输入0.0.0.0可看到
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值