【大数据】python 项目部署 uwsgi + nginx

163 阅读4分钟

image.png

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第24天,点击查看活动详情

前言

周末期间完成了neo4j查询数据的处理以及Flask接口的封装,实现起来很简单,数据经过程序进行处理,返回格式为前端G6所需的数据形式,包含nodes、edges、combos三个对象,其中分别包含了节点信息、边信息和标签属性信息,之后对这个方法进行接口封装,使用python flask web框架搭建简单的后端程序,但是flask并不能之间作为后端进行部署,这是与Java spring系列差别很大的地方,需要依靠uwsgi和nginx完成后端部署。


为啥flask需要uwsgi和nginx

我们在生产环境启动flask服务的时候,通常看到如下画面:

WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.

意思是说,这个是开发环境,不要用于生产环境部署。并且我们常常看到Flask、Uwsgi和Nginx经常同时出现,所有的组件目的只有一个,完成客户端到服务端的访问,我们无论是通过前端调用接口的形式,还是通过postman等形式访问后端服务,均需要发送请求到后端服务器,后端服务器完成响应返回给客户端。这中间需要一个纽带或者必要环节:

  1. 客户端和服务端的通信需要打通,这一步骤是通过Nginx完成
  2. 而服务端需要将客户端所需的正确数据格式返回过去,这一过程是uwsgi完成的

image.png

---

python flask 只能说是一个轻量级的web框架,一个后端应用,而不是web服务,完整的web服务包含

image.png

web应用发起请求,通过WSGI协议或者标准接口完成与APP的通信,APP或者服务端完成响应后,通过WSGI处理,再由Web服务器返回给前端,而server一般由nginx充当,当app.run()启动服务后,flask可以做到低配的server,当生产环境时,高并发或者大吞吐的情况下,则需要稳定的中间件完成Server与WSGI接口,因此就需要nginx和uwsgi的加持。这也能理解flask启动后是建议运行在测试环境,但并不意味着直接上生产是不行的,只是不建议。

WSGI是一种Web应用的网关接口,是通过python语言定义的web服务器和web应用程序之间一种简单而通用的接口,作用就是在协议之间进行转换。而uWsgi同WSGI也是一种通信协议,实现了uwsgi和WSGI两种协议的web服务器。

既然uwsgi被叫做了服务器,那么可不可以仅使用flask+uwsgi呢,其实是可以的,但是由于其本身性能非常有限,为了方便,还需要集成http服务的功能,因此会通过nginx完成http server的功能,将静态内容处理为动态内容转发给uwsgi服务器,达到很好的客户端响应。

image.png

通常一个完整的请求过程是这样的:

image.png


uwsgi安装和使用

首先使用pip安装uwsgi服务

pip install uwsgi

在我们的flask项目目录下建立一个uwsgi.ini配置文件 如下:

http=0.0.0.0:5000
chdir=/home/xxx/
wsgi-file=/home/xxx/app.py
callable=app
master=true
processes=1
threads=10
daemonize=uwsgi.log
disable-logging=true
pidfile=uwsgi.pid
buffer-size=65536
harakiri=60
vacuum=True

  • http 服务地址
  • chdir 项目路径
  • wsgi-file 需要run的对象
  • master 主进程
  • processes 工作进程
  • threads 每个工作进程的线程数量
  • daemonize 输出日志的地方
  • pidfile 保存的主进程号
  • buffer-size 设置用于uwsgi包解析的内部缓存区大小
  • harakiri 服务器响应时间
  • vacuum 当服务器退出的时候自动删除unix socket文件和pid文件

启动 uwsgi

uwsgi --ini uwsgi.in

结束 uswgi

uwsgi --stop uwsgi.pid

启动成功情况下:

uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 23923)
spawned uWSGI worker 1 (pid: 23926, cores: 10)
spawned uWSGI http 1 (pid: 23936)

nginx安装和使用

nginx安装需要依赖一些其他包

安装gcc linux下的编译器

yum -y install gcc

安装### pcre、pcre-devel

pcre是一个perl库,包括perl兼容的正则表达式库,nginx需要用这两个模块解析http协议,因此需要安装。

yum install -y pcre pcre-devel

zlib安装,用于对协议内容压缩等

yum install -y zlib zlib-devel

openssl安装 安全协议 保护安全不裸奔

yum install -y openssl openssl-devel

下载nginx

wget http://nginx.org/download/nginx-1.9.9.tar.gz  

解压安装等

tar -zxvf nginx-1.9.9.tar.gz

./configure
 
make
 
make install

编辑配置文件:

cd /usr/local/nginx/conf
vi nginx.conf

#user  nobody;
worker_processes  1;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       10011;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            index  index.html index.htm;
            root   /home/dist; # 路径改成自己的dist路径
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }

        location /api/ {
           proxy_pass http://127.0.0.1:5000/;
        }

        location @router {
            rewrite ^.*$ /index.html last;
      }
    }
  }

配置一些代理地址如 location /api/ 就是uwsgi地址以及监听端口10011等

启动nginx

./nginx

查看是否启动成功:

ps -ef | grep nginx

image.png

表示启动成功,今天分析了flask和uwsgi与nginx,之后将通过这几个组件完成后端程序以及模型程序的部署,蟹蟹~