python生产环境部署
Python部署web开发程序的几种方法
fastcgi ,通过flup模块来支持,在nginx里对应的配置指令是 fastcgi_pass
http,nginx使用proxy_pass转发,这个要求后端appplication必须内置一个能处理高并发的http server,在python的web框架当中,只能选择tornado.
uwsgi,包括4部分组成:
- uwsgi协议
- web server内置支持协议模块
- application服务器协议支持模块
- 进程控制程序
nginx从0.8.4开始内置支持uwsgi协议,uwsgi协议非常简单,一个4个字节header+一个body,body可以是很多协议的包,比如说http,cgi等(通过header里面字段标示)。
uwsgi的特点在于自带的进程控制程序.它是用c语言编写,使用natvie函数,其实和spawn-fcgi/php-fpm类似。所以uwsgi可以支持多种应用框架,包括(python,lua,ruby,erlang,go)等等
mod_python,这是apache内置的模块,很严重的依赖于mod_python编译使用的python版本,和apache配套使用,不推荐
cgi,这个太old,不推荐,而且nginx不支持cgi方式,只能用lighttpd或者apache
spawn-fcgi,这个是fastcgi多进程管理程序,lighttpd安装包附带的,和 flup效果一样,区别是flup是 python代码级引入,spawn-fcgi是外部程序。spawn-fcgi用途很广,可以支持任意语言开发的代码,php,python,perl,只要你代码实现了fastcgi接口,它都可以帮你管理你的进程
scgi,全名是Simple Common Gateway Interface,也是cgi的替代版本,scgi协议很简单,我觉得和fastcgi差不多,只是没有怎么推广开来,nginx对应的配置指令是scgi_pass,你想用就用,flup也支持。
Gunicorn,和uwsgi类似的工具,从rails的部署工具(Unicorn)移植过来的。但是它使用的协议是 WSGI,全称是Python Web Server Gateway Interface ,这是python2.5时定义的官方标准(PEP 333 ),根红苗正,而且部署比较简单,http://gunicorn.org/ 上有详细教程
mod_wsgi,apache的一个module,也是支持WSGI协议,https://code.google.com/p/modwsgi/
uwsgi
安装uwsgi
pip install uwsgi
配置uwsgi
uwsgi 有多种配置可用:
1,ini
2,xml
3,json
4,yaml
配置示例

$ cat etc/uwsgi.ini
[uwsgi]
socket = 127.0.0.1:9005
chdir = /Users/suoning/python_project/trunk/
wsgi-file = main.py
processes = 4
stats = 127.0.0.1:9000
daemonize = /tmp/uwsgiServer.log
pidfile = /tmp/uwsgi.pid
vacuum = true
log-maxsize = 50000000
disable-logging = true
callable = app
$

配置参数详解:
常用选项:
socket : 地址和端口号,例如:socket = 127.0.0.1:50000
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number of workers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
log-maxsize :以固定的文件大小(单位KB),切割日志文件。 例如:log-maxsize = 50000000 就是50M一个日志文件。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
disable-logging : 不记录请求信息的日志。只记录错误以及uWSGI内部消息到日志中。如果不开启这项,那么你的日志中会大量出现这种记录:
[pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul 7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)
配置nginx

$ cat etc/nginx/servers/tongbupan.conf
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9005;
}
location /webstatic/ {
expires 7d;
add_header Cache-Control public;
alias /Users/suoning/probject/python_project/webstatic/trunk/;
}
}
$
$ nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
$
$ nginx -s reload
$

配置application
flask 示例

...
app = Flask('pan')
... if __name__ == '__main__':
# app.run(host='0.0.0.0', port=5000)
app.run() # 注意:变量app对应uwsgi配置文件uwsgi.ini中 callable = app

启动uwsgi

$
$ uwsgi --ini /usr/local/etc/uwsgi.ini
[uWSGI] getting INI configuration from /usr/local/etc/uwsgi.ini
$
$ ps -ef|grep uwsgi
501 11428 1 0 11:40下午 ?? 0:01.23 uwsgi --ini /usr/local/etc/uwsgi.ini
501 11432 11428 0 11:40下午 ?? 0:00.00 uwsgi --ini /usr/local/etc/uwsgi.ini
501 11433 11428 0 11:40下午 ?? 0:00.00 uwsgi --ini /usr/local/etc/uwsgi.ini
501 11434 11428 0 11:40下午 ?? 0:00.00 uwsgi --ini /usr/local/etc/uwsgi.ini
501 11435 11428 0 11:40下午 ?? 0:00.00 uwsgi --ini /usr/local/etc/uwsgi.ini
501 11440 69240 0 11:40下午 ttys000 0:00.00 grep uwsgi
$
$ lsof -i tcp:9000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
uwsgi 11428 suoning 28u IPv4 0x5583e11534d24e73 0t0 TCP localhost:cslistener (LISTEN)
$
$ lsof -i tcp:9005
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
uwsgi 11428 suoning 6u IPv4 0x5583e11535699e73 0t0 TCP localhost:9005 (LISTEN)
uwsgi 11432 suoning 6u IPv4 0x5583e11535699e73 0t0 TCP localhost:9005 (LISTEN)
uwsgi 11433 suoning 6u IPv4 0x5583e11535699e73 0t0 TCP localhost:9005 (LISTEN)
uwsgi 11434 suoning 6u IPv4 0x5583e11535699e73 0t0 TCP localhost:9005 (LISTEN)
uwsgi 11435 suoning 6u IPv4 0x5583e11535699e73 0t0 TCP localhost:9005 (LISTEN)
$

FCGI
参考:http://webpy.org/cookbook/fastcgi-nginx
配置Nginx

$ cat etc/nginx/servers/pan.conf
server {
listen 80;
server_name localhost;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location / {
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass 127.0.0.1:9005;
}
location /webstatic/ {
expires 7d;
add_header Cache-Control public;
alias /Users/suoning/probject/python_project/webstatic/trunk/;
}
}
$

配置application
简单示例

from flup.server.fcgi import WSGIServer
from pan import app WSGIServer(
app,
bindAddress=(host, port),
maxThreads=threads
).run()

生产环境示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'suoning' import sys
import argparse
from flup.server.fcgi import WSGIServer
from lib.daemon import Daemon
from pan import app APP_NAME = 'pan_platform'
APP_INST_NAME = '20170501' parser = argparse.ArgumentParser(description=u'Run an pan FastCGI server')
parser.add_argument('command', type=str,
help=u'command [start|stop|restart]',
choices=['start', 'stop', 'restart'])
parser.add_argument('-p', '--port', type=int,
help=u'port of this server', required=True)
parser.add_argument('-t', '--threads', type=int, default=50,
help=u'max number of threads')
parser.add_argument('-host', '--host', default='0.0.0.0',
help=u'Listen to the main clause') class panPlatformDaemon(Daemon):
def run(self):
# 运行服务
try:
WSGIServer(
app,
bindAddress=(args.host, args.port),
maxThreads=args.threads,
umask=0111
).run()
except:
sys.stderr.write('oops') def gen_pidfile(port):
return '/var/run/%s_%s_%d.pid' % (APP_NAME, APP_INST_NAME, port) if __name__ == '__main__':
args = parser.parse_args()
daemon = panPlatformDaemon(gen_pidfile(args.port))
if 'start' == args.command:
daemon.start()
elif 'stop' == args.command:
daemon.stop()
elif 'restart' == args.command:
daemon.restart()
else:
print "Unknown command"
sys.exit(2)
sys.exit(0)

fastcgi协议和http协议在代码部署中的的优劣对比
fastcgi虽然是二进制协议,相对于http协议,并不节省资源。二进制协议,只能节省数字的表达,比如 1234567,用字符串表示需要7个Byte,用数字就是4个Byte,而字符串到哪里都一样
fastcgi在传输数据的时候,为了兼容cgi协议,还要带上一堆cgi的环境变量,所以和http协议相比,用fastcgi传输数据并不省,反而多一些
fastcgi 唯一的优点是,它是长连接的,用户并发1000个request,fastcgi可能就用10个 链接转发给后端的appplication,如果用http协议,那来多少给多少,会向后端appplication 发起1000个请求
http代理转发方式,在面对超高并发的情况下会出问题,因为,tcp协议栈当中,port是int16整型 你本地新建一个connect,需要消耗一个端口,最多能到65536。外部并发几十万个请求,port池耗干,你的服务器只能拒绝响应了
CGI, FCGI, SCGI, WSGI 区别
WIKI Links:
python生产环境部署的更多相关文章
- Python生产环境部署(fastcgi,uwsgi)
Python部署web开发程序的几种方法 fastcgi ,通过flup模块来支持,在nginx里对应的配置指令是 fastcgi_pass http,nginx使用proxy_pass转发,这个要求 ...
- 第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块
第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块 virtualenv简介 1.安装virtuale ...
- Django + Uwsgi + Nginx 的生产环境部署
使用runserver可以使我们的django项目很便捷的在本地运行起来,但这只能在局域网内访问,如果在生产环境部署django,就要多考虑一些问题了.比如静态文件处理,安全,效率等等,本篇文章总结归 ...
- 深度学习Tensorflow生产环境部署(上·环境准备篇)
最近在研究Tensorflow Serving生产环境部署,尤其是在做服务器GPU环境部署时,遇到了不少坑.特意总结一下,当做前车之鉴. 1 系统背景 系统是ubuntu16.04 ubuntu@ub ...
- 第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置
第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置 软件版本 uwsgi- ...
- 第四百节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装python3.5.1
第四百节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装python3.5.1 1.检查系统是否安装了python [root@192 ~]# rpm -qa ...
- 第三百九十八节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5系统环境设置
第三百九十八节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5系统环境设置 1.Linux安装配置 注意事项: 虚拟机网卡桥接模式 不要拨VPN 如果,网络怎么都 ...
- ubuntu Django + Uwsgi + Nginx 的生产环境部署
一.概述 使用runserver可以使我们的django项目很便捷的在本地运行起来,但这只能在局域网内访问,如果在生产环境部署django,就要多考虑一些问题了.比如静态文件处理,安全,效率等等,本篇 ...
- 10: Django + Uwsgi + Nginx 的生产环境部署
1.1 一些重要概念 1.Web协议介绍 Web协议出现顺序: CGI -> FCGI -> WSGI -> uwsgi 1. CGI: 最早的协议 2. FCGI: 比CGI快 ...
随机推荐
- C# 添加、读取Word脚注尾注
脚注和尾注是对文本的补充说明.脚注一般位于页面的底部,可以作为文档某处内容的注释:尾注一般位于文档的末尾,列出引文 的出处等.在本示例中将介绍如何来添加或删除Word脚注. 工具使用:Free Spi ...
- apt-get Ubuntu本地ISO镜像入源
转自http://blog.csdn.net/binchel/article/details/21486999 在没有网络的情况下,本地镜像源不实为一个上等的权宜之计! 目前linux的两大主流包管理 ...
- java并发之可见性与原子性:Syncronized和volatile
转载:http://blog.csdn.net/guyuealian/article/details/52525724 在说明Java多线程内存可见性之前,先来简单了解一下Java内存模型. ...
- 如何卸载Centos自带jdk
1.搜索安装的jdk: rpm -qa|grep jdk 结果如下: java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.x86_64 java-1.6.0-openjdk ...
- JMS详细的工作原理
如果手机只能进行实时通话,没有留言和短信功能会怎么样?一个电话打过来,正好没有来得及接上,那么这个电话要传递的信息肯定就收不到了.为什么不能先将信息存下来,当用户需要查看信息的时候再去获得信息呢?伴随 ...
- 基于Python的数据分析:数据库索引效率探究
索引在数据库中是一个很特殊的存在,它的目的就是为了提高数据查询得效率.同样,它也有弊端,更新一个带索引的表的时间比更新一个没有带索引的时间更长.有得有失.我希望做一些研究测试,搞清楚索引对于我们使用数 ...
- Coursera-AndrewNg(吴恩达)机器学习笔记——第二周编程作业
一.准备工作 从网站上将编程作业要求下载解压后,在Octave中使用cd命令将搜索目录移动到编程作业所在目录,然后使用ls命令检查是否移动正确.如: 提交作业:提交时候需要使用自己的登录邮箱和提交令牌 ...
- sql语句联表更新(从一个数据库中的一张表更新到另一个数据库的另一张表)
一.sql server数据库写法: update a set a.ksgmm=b.ksgmm,a.ksgm=b.ksgm,a.scztm=b.scztm,a.sczt=b.sczt from lan ...
- Django REST framework+Vue 打造生鲜超市(十三)
目录 生鲜超市(一) 生鲜超市(二) 生鲜超市(三) 生鲜超市(四) 生鲜超市(五) 生鲜超市(六) 生鲜超市(七) 生鲜超市(八) 生鲜超市(九) 生鲜超市(十) ...
- Thymeleaf中each标签遍历list如何获取index
<tr th:each="user,userStat:${users}">userStat是状态变量,有 index,count,size,current,even,o ...