Python Web 程序使用 uWSGI 部署

Python Web 程序使用 uWSGI 部署

WSGI是什么?

WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口。自从 WSGI 被开发出来之后,许多其它语言中也出现了相似接口。html

WSGI 的官方定义是,the Python Web Server Gateway Interface。从名字就能够看出来,这东西是一个Gateway,也就是网关。网关的做用就是在协议之间进行转换。python

WSGI 是做为 Web 服务器与 Web 应用程序或应用框架之间的一种低级别的接口,以提高可移植 Web 应用开发的共同点。WSGI 是基于现存的 CGI 标准而设计的。nginx

不少框架都自带了 WSGI server ,好比 Flask,webpy,Django、CherryPy等等。固然性能都很差,自带的 web server 更多的是测试用途,发布时则使用生产环境的 WSGI server或者是联合 nginx 作 uwsgi 。也就是说,WSGI就像是一座桥梁,一边连着web服务器(如 nginx),另外一边连着Python的应用程序Application。可是呢,这个桥的功能很弱,有时候还须要别的桥来帮忙才能进行处理。web

uWSGI

uWSGI 是一个(巨大的) C 应用,因此你须要一个 C 编译器(好比 gcc 或者 clang)和 Python 开发版头文件。浏览器

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的做用是与uWSGI服务器进行交换。服务器

uWSGI 安装

环境

SystemOS: CentOS-7.5_x64
Python: 3.6
virtual: Anaconda3

安装

# 在虚拟环境中直接安装
$ pip install uwsgi

uWSGI hello world

hello_world.pyapp

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

启动框架

uwsgi --http 0.0.0.0:8000 --wsgi-file hello_world.pysocket

使用浏览器访问http://ip:8000性能

启动 uwsgi 能够添加多个参数

uwsgi --socket 127.0.0.1:3031 --wsgi-file uwsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:8001

将配置文件集中写在配置文件中

uwsgi.ini

[uwsgi]
socket = 127.0.0.1:3031
chdir = /home/foobar/myproject/
pythonpath = ..
processes = 4
threads = 2
stats = 127.0.0.1:9191
相关文章
相关标签/搜索