virtualenv 环境下 Flask + Nginx + Gunicorn+ Supervis

2023-10-09 07:40

本文主要是介绍virtualenv 环境下 Flask + Nginx + Gunicorn+ Supervis,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

在这篇文章里,我们将搭建一个简单的 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 跑应用程序,创建一个容易识别的目录,开始安装,再创建项目目录 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 = Trueapp.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 Webvirtualenv 环境下 Django + Nginx + Gunicorn+ Supervisor 搭建Python Webvirtualenv 环境下 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 工程师编译整理。OneAPM 是应用性能管理领域的新兴领军企业,能帮助企业用户和开发者轻松实现:缓慢的程序代码和 SQL 语句的实时抓取。想技术文章,请访问 OneAPM 官方博客。

转载于:https://my.oschina.net/oneapmofficial/blog/478831

这篇关于virtualenv 环境下 Flask + Nginx + Gunicorn+ Supervis的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/171347

相关文章

Nginx路由匹配规则及优先级详解

《Nginx路由匹配规则及优先级详解》Nginx作为一个高性能的Web服务器和反向代理服务器,广泛用于负载均衡、请求转发等场景,在配置Nginx时,路由匹配规则是非常重要的概念,本文将详细介绍Ngin... 目录引言一、 Nginx的路由匹配规则概述二、 Nginx的路由匹配规则类型2.1 精确匹配(=)2

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

Nginx 访问 /root/下 403 Forbidden问题解决

《Nginx访问/root/下403Forbidden问题解决》在使用Nginx作为Web服务器时,可能会遇到403Forbidden错误,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录解决 Nginx 访问 /root/test/1.html 403 Forbidden 问题问题复现Ng

Nginx部署React项目时重定向循环问题的解决方案

《Nginx部署React项目时重定向循环问题的解决方案》Nginx在处理React项目请求时出现重定向循环,通常是由于`try_files`配置错误或`root`路径配置不当导致的,本文给大家详细介... 目录问题原因1. try_files 配置错误2. root 路径错误解决方法1. 检查 try_f

史上最全nginx详细参数配置

《史上最全nginx详细参数配置》Nginx是一个轻量级高性能的HTTP和反向代理服务器,同时也是一个通用代理服务器(TCP/UDP/IMAP/POP3/SMTP),最初由俄罗斯人IgorSyso... 目录基本命令默认配置搭建站点根据文件类型设置过期时间禁止文件缓存防盗链静态文件压缩指定定错误页面跨域问题

Gradle下如何搭建SpringCloud分布式环境

《Gradle下如何搭建SpringCloud分布式环境》:本文主要介绍Gradle下如何搭建SpringCloud分布式环境问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Gradle下搭建SpringCloud分布式环境1.idea配置好gradle2.创建一个空的gr

nginx负载均衡及详细配置方法

《nginx负载均衡及详细配置方法》Nginx作为一种高效的Web服务器和反向代理服务器,广泛应用于网站的负载均衡中,:本文主要介绍nginx负载均衡及详细配置,需要的朋友可以参考下... 目录一、 nginx负载均衡策略1.1 基本负载均衡策略1.2 第三方策略1.3 策略对比二、 nginx配置2.1

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖