最近一个小项目用到了FLASK,部署就要用到wsgi,我选用了uWSGI,uWSGI的主要特点如下:

◆超快的性能。

◆低内存占用(实测为apache2的mod_wsgi的一半左右)。

◆多app管理。

◆详尽的日志功能(可以用来分析app性能和瓶颈)。

◆高度可定制(内存大小限制,服务一定次数后重启等)。


一.安装flask相关
1. 安装pip
python get-pip.py
2.安装uWSGI
pip install uwsgi
3.安装flask
pip install flask
建立测试app: vim www/app.py

#!/usr/bin/env python
# encoding: utf-8
from flask import Flask
app = Flask(__name__)
@app.route("/")
def helloworld():
return "Hello World!"



######################################################################

二. 安装nginx


1.安装依赖库
zlib:        提供gzip模块,需要zlib库支持
openssl: 提供ssl功能
pcre:      正则表达式相关的类库, 如果缺少此库,nginx无法支持HTTP中的URL重写功能。如果你不需要此功能,可以在执行编译配置脚本时加入“--without-http_rewrite_module”。

或yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

安装编译环境:yum -y install gcc automake autoconf libtool make

2.建立用户
groupadd -r nginx
useradd -s /sbin/nologin -g nginx -r nginx
3.编译nginx
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--with-http_stub_status_module \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi
4.建立虚拟主机

UWSGI:
vim /etc/nginx/nginx.conf
location /client {  
 include uwsgi_params  
 uwsgi_pass 127.0.0.1:9090  
access_log   off;
}
启动方式:
 A:    uwsgi --socket 127.0.0.1:9090 --wsgi-file __init__.py --callable app  --master  --processes 4 --threads 2 --stats 127.0.0.1:9191   -t 30  
--limit-as 128 (内存)
telnet 127.0.0.1 9191 可以查看uwsgi信息
B  uwsgi -x /home/uwsgi/uwsgi.xml:
需要 vim uwsgi.xml
<uwsgi>  
 <socket>127.0.0.1:9090</socket>  
 <listen>200</listen>  
 <master>true</master>  
 <pidfile>/var/run/nginx/uwsgi.pid</pidfile>  
 <processes>8</processes>  
 <!-- 可以多个pythonpath-->
 <pythonpath>/var/www/client_info</pythonpath>  
 <profiler>true</profiler>  
 <memory-report>true</memory-report>  
 <enable-threads>true</enable-threads>  
 <logdate>true</logdate>  
 <limit-as>2048</limit-as>  
 <daemonize>/opt/www/logs/django.log</daemonize>  

</uwsgi>


问题解决:
/usr/bin/uwsgi -x /home/uwsgi/uwsgi.xml
uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决方法:
64位系统 # ln -s /usr/local/lib/libpcre.so.1 /lib64

32位系统 # ln -s /usr/local/lib/libpcre.so.1 /lib


5.启动nginx即可
nginx -s reload