zoukankan      html  css  js  c++  java
  • virtualenv 环境下 Flask + Nginx + Gunicorn+ Supervisor 搭建 Python Web

    在这篇文章里,我们将搭建一个简单的 Web 应用,在虚拟环境中基于 Flask 框架,用 Gunicorn 做 wsgi 容器,用 Supervisor 管理进程,然后使用 OneAPM Python 探针来监測应用性能,形成一个「闭环」 。希望能对大家有所帮助,首先简单来介绍一下环境:

    系统环境:ubuntu 14.04 Python 2.7.6

    安装组件库

    第一步安装所须要的存储库,由于打算用到虚拟环境。用到 pip 安装和管理 Python 组件,所以先更新本地包,然后安装组件:

    sudo apt-get update
    sudo apt-get install python-pip python-dev nginx
    

    创建虚拟环境 virtualenv

    在一个系统中创建不同的 Python 隔离环境。相互之间还不会影响,为了使系统保持干净,遂决定用 virtualenv 跑应用程序。创建一个easy识别的文件夹,開始安装。再创建项目文件夹 super。然后激活环境:

    sudo pip install virtualenv
    mkdir ~/supervisor && cd ~/supervisor
    virtualenv super
    source super/bin/activate
    

    安装 Flask 框架

    好了,如今在虚拟环境里面。開始安装 Flask 框架,flask 依赖两个库 werkzeug 和 jinjia2, 採用 pip 方式安装就可以, pip 是一个重要的工具,Python 用它来管理包:

    pip install flask
    

    先用 Flask 写一个简单的 Web 服务 myweb.py ,由于后面要做一些測试。所以设置了几个请求:

    from flask import Flask
    app = Flask(__name__)
    @app.route('/')
    def index():
        return 'hello world  supervisor gunicorn '
    @app.route('/1')
    def index1():
        return 'hello world  supervisor gunicorn  ffffff'
    @app.route('/qw/1')
    def indexqw():
        return 'hello world  supervisor gunicorn fdfdfbdfbfb '
    if __name__ == '__main__':
        app.debug = True
        app.run()
    

    启动 Flask 看看!

    python myweb.py
    

    在浏览器中訪问 http://127.0.0.1:5000 就能够看到了「几个路径都试一试」

    用 Gunicorn 部署 Python Web

    如今我们使用 Flask 自带的服务器,完毕了 Web 服务的启动。

    生产环境下,Flask 自带的服务器。无法满足性能要求。

    所以我们这里採用 Gunicorn 做 wsgi 容器,用来部署 Python。首先还是安装 Gunicorn:

    pip install gunicorn
    

    当我们安装好 Gunicorn 之后,须要用 Gunicorn 启动 Flask。Flask 用自带的服务器启动时,Flask 里面的 name 里面的代码启动了 app.run()。

    而这里我们使用 Gunicorn,myweb.py 就等同于一个库文件,被 Gunicorn 调用,这样启动:

    gunicorn -w 4 -b 0.0.0.0:8000 myweb:app
    

    当中 myweb 就是指 myweb.py,app 就是那个 wsgifunc 的名字,这样执行监听 8000 端口。原先的 5000 端口并没有启用,-w 表示开启多少个 worker。-b 表示 Gunicorn 开发的訪问地址。

    想要结束 Gunicorn 仅仅需执行 pkill Gunicorn。但有时候还要 ps 找到 pid 进程号才干 kill。

    但是这对于一个开发来说,太过于繁琐,因此出现了另外一个神器 —supervisor,一个专门用来管理进程的工具。还能够管理系统的工具进程。

    安装 Supervisor

    pip install supervisor
    echo_supervisord_conf > supervisor.conf  # 生成 supervisor 默认配置文件
    gedit  supervisor.conf                   # 改动 supervisor 配置文件,加入 gunicorn 进程管理
    
    
    在 supervisor.conf 底部下加入 myweb.py 的配置 `/home/wang/supervisor/super` 是我的项目文件夹」
    
    
    [program:myweb]
    command=/home/wang/supervisor/super/bin/gunicorn -w 4 -b 0.0.0.0:8000 myweb:app                                                                     
    directory=/home/wang/supervisor                                             
    startsecs=0                                                                   
    stopwaitsecs=0                                                                  
    autostart=false                                                                
    autorestart=false                                                                 
    user=wang                                                                     
    stdout_logfile=/home/wang/supervisor/log/gunicorn.log                  
    stderr_logfile=/home/wang/supervisor/log/gunicorn.err
    

    supervisor 的基本使用命令:

    supervisord -c supervisor.conf    
    supervisorctl -c supervisor.conf status                  查看supervisor的状态                                      
    supervisorctl -c supervisor.conf reload                  又一次载入 配置文件
    supervisorctl -c supervisor.conf start [all]|[appname]   启动指定/全部 supervisor 管理的程序进程
    supervisorctl -c supervisor.conf stop [all]|[appname]    关闭指定/全部 supervisor 管理的程序进程
    

    supervisor 另一个 web 的管理界面,能够激活。

    更改下配置:

    [inet_http_server]     ; inet (TCP) server disabled by default
    port=127.0.0.1:9001    ; (ip_address:port specifier, *:port for alliface)
    username=wang          ; (default is no username (open server)
    password=123           ; (default is no password (open server))
    
    [supervisorctl]
    serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
    serverurl=http://127.0.0.1:9001       ; use an http:// url to specify an inet socket
    username=wang                         ; should be same as http_username if set
    password=123                          ; should be same as http_password if set
    ;prompt=mysupervisor                  ; cmd line prompt (default "supervisor")
    ;history_file=~/.sc_history           ; use readline history if available
    

    如今能够使用 supervsior 启动 gunicorn 啦。

    执行命令:

    supervisord -c supervisor.conf
    

    浏览器訪问 http://127.0.0.1:9001 能够得到 supervisor 的 web 管理界面,訪问 http://127.0.0.1:8000 能够看见 gunicorn 启动的返回的页面。

    配置 Nginx

    前面我们已经在系统环境下安装了 Nginx, 安装好的 Nginx 二进制文件放在 /usr/sbin/ 文件夹下。接下来使用 Supervisor 来管理 Nginx。

    这里须要注意一个问题。权限问题。

    Nginx 是 sudo 的方式安装。启动的适合也是 root 用户。那么我们如今也须要用 root 用户启动 supervisor。在 supervisor.conf 下加入配置文件:

    [program:nginx]
    command=/usr/sbin/nginx
    startsecs=0
    stopwaitsecs=0
    autostart=false
    autorestart=false
    stdout_logfile=/home/wang/supervisor/log/nginx.log
    stderr_logfile=/home/wang/supervisor/log/nginx.err 
    

    好了。都配置完之后,启动 supervisor:

    supervisord -c supervisor.conf
    

    訪问页面。也能够用 ab 进行压力測试:

    ab -c 100 -n 100 http://127.0.0.1:8000/qw/1
    

    -c 用于指定压力測试的并发数, -n 用于指定压力測试总共的执行次数。

    安装 Python 探针

    搭建好了 web。想实时监控应用数据,有什么好的工具。用 OneAPM 的 Python 探针试试,
    首先也是安装 Python 探针:

    pip install -i http://pypi.oneapm.com/simple --upgrade blueware
    

    依据 License Key 生成配置文件:

    blueware-admin generate-config (License Key) = blueware.ini
    

    由于是在虚拟环境下,所以要特别注意路径,改动 supervisor.conf 里面两项:

    [program:myapp]
    command = /home/wang/supervisor/super/bin/blueware-admin run-program /home/wang/supervisor/super/bin/gunicorn -w 4 -b 0.0.0.0:8000 myapp:app
    environment = BLUEWARE_CONFIG_FILE=blueware.ini
    

    重新启动应用

    supervisorctl    # 进入命令行
    supervisor>  reload    # 又一次载入
    

    向上面一样訪问页面,也能够用 ab 进行压力測试
    几分钟后有下图,能够看到页面载入时间,web 事物,页面吞吐量。当中后面是设置自己定义事物「Business Transaction」。

    virtualenv 环境下 Django + Nginx + Gunicorn+ Supervisor 搭建Python Web
    virtualenv 环境下 Django + Nginx + Gunicorn+ Supervisor 搭建Python Web
    virtualenv 环境下 Django + Nginx + Gunicorn+ Supervisor 搭建Python Web
    參考文章:

    How To Serve Flask Applications with uWSGI and Nginx on Ubuntu 14.04

    OneAPM Python 探针安装

    python web 部署:nginx + gunicorn + supervisor + flask 部署笔记

    Tornado+nginx+supervisor 在生产环境上部署

    supervisor + Tornado + Nginx 使用具体解释

    本文系 OneAPM project师编译整理。OneAPM 是应用性能管理领域的新兴领军企业。能帮助企业用户和开发人员轻松实现:缓慢的程序代码和 SQL 语句的实时抓取。想阅读很多其它技术文章。请訪问 OneAPM 官方博客

  • 相关阅读:
    Redis 优化之 tcp-backlog
    linux下生成带符号的随机密码
    mysqldump导出sql文件中insert多行问题
    /usr/lib64/python2.6/site-packages/cryptography/__init__.py:26: DeprecationWarning: Python 2.6 is no longer supported by the Python core team
    ldconfig: /usr/lib/libpython2.6.so.1.0-gdb.py is not an ELF file
    [Errno 14] problem making ssl connection Trying other mirror.
    docker commit 显示“invalid reference format”
    (转)从Python的0.1输出0.1000000000000001说浮点数的二进制
    mysql中replicate_wild_do_table和replicate_do_db区别
    ipsec验证xl2tpd报错:handle_packet: bad control packet!
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7243848.html
Copyright © 2011-2022 走看看