1. 准备相关资源: nginx的官方文档,详细内容可以参考http://wiki./NgxWSGIModule(英文), 1.1 安装好python 解析器, python 开发文件和Nginx 编译依赖的 pcre 开发文件, 在 Ubuntu 上可通过如下命令安装
| apt-get install python python-dev libxml2-dev libpcre3-dev
| 1.2 下载最新的 uwsgi 应用容器服务器:
| wget http://projects./downloads/uwsgi-0.9.6.8.tar.gz |
1.3 下载最近的 Nginx 1.0.4:
| wget http:///download/nginx-1.0.4.tar.gz
|
1.4 更新 Nginx 中预装的 uwsgi 应用容器服务器
| tar -xf uwsgi-0.9.6.8.tar.gz tar -xf nginx-1.0.4.tar.gz cp -vf uwsgi-0.9.6.8/nginx/ngx_http_uwsgi_module.c \ /src/http/modules/ngx_http_uwsgi_module.c
|
2.编译安装; 2.1 编译安装 Nginx :
| CFLAGS=' -O2 ' ./configure --prefix=/usr/local/bin/nginx \ --http-client-body-temp-path=/tmp/nginx/body \ --http-proxy-temp-path=/tmp/nginx/proxy \ --http-fastcgi-temp-path=/tmp/nginx/fastcgi \ --http-uwsgi-temp-path=/tmp/nginx/uwsgi \ --with-http_stub_status_module \ --with-http_flv_module \ --http-log-path=/var/log/nginx/access.log \ --error-log-path=/var/log/nginx/error.log \ --lock-path=/var/run/nginx.lock \ --pid-path=/var/run/nginx.pid \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-pcre \ --with-file-aio \ --with-md5-asm \ --with-sha1-asm \ --with-zlib-asm=pentiumpro --without-http_scgi_module
|
这样就安装了支持最新 uwsgi 的nginx( 注意这里只是为 )。
2.2 安装 uwsgi 到 nginx 安装目录下 1 | make cp -vf uwsgi /usr/local/bin/nginx/sbin |
可以根据实际情况调整安装路径。
3 配置,请阅读本文附加的官方配置文档(也可访问:http://projects./uwsgi/wiki/RunOnNginx )
3.1 配置nginx, 下面给出一个完整的虚拟主机配置: 01 | user www-data; worker_processes 10; events { use epoll; worker_connections 2048; } |
02 | http { include mime.types; |
03 | default_type application/octet-stream; sendfile on; |
04 | index index.html index.htm index.py; |
05 | root /var/www; upstream uwsgicluster { ip_hash; server 127.0.0.1:8090; }
|
10 | include uwsgi_params; uwsgi_pass uwsgicluster; |
14 | location = /favicon.ico { return 204; access_log off; log_not_found off; } |
4 让 Nginx + uWSGI 开始服务人民群众: 4.1 运行 uwsgi: 假定我们创建的 WSGI 应用都放在 /var/www/wsgi 目录下,且有一个应用为 testApp.py, 在这个应用文件中还用如下方式:
| import os import sys import web
curdir = os.path.dirname(__file__) sys.path.append(curdir)
web.config.debug = True
urls = ('/test', 'main',)
class main: def GET(self): return 'This is our test wSGI application\n'
a = web.application( urls, globals() ) session = web.session.Session( a, web.session.DiskStore( '/tmp/sessions' ) ) wsgiAppExample = a.wsgifunc() |
#### 注意*是*调用 web.application( ).wsgifunc( ) 创建了一个支持 WSGI 定义了
| UWSGI_BIN='/usr/local/bin/nginx/sbin/uwsgi'
APPS_HOME=/var/www/wsgi APP_MODULE=testApp #### 这里对应 testApp.py APP_MAIN=wsgiAppExample #### 这里对应 testApp.py 中由 a.wsgifunc() 创建的应用
#### 根据上面指定的参数运行 uwsgi 应用容器服务器: "${UWSGI_BIN}" --uid 1000 -s 127.0.0.1:8090 \ --chdir "${APPS_HOME}" -w "${APP_MODULE}" --callable "${APP_NAME}" \ --daemonize /var/log/nginx/uwsgi.log
#### 至于为什么要指定以上四个参数才最终运行 uWSGI 服务,请运行: #### uwsgi --help #### 获取详细信息.
|
4.2 运行 Nginx:
NGINX_BIN='/usr/local/bin/nginx/sbin/nginx' NGINX_CFG='/usr/local/bin/nginx/conf/nginx.conf
"${NGINX_BIN}" -c "${NGINX_CFG}"
|
5.检验一下劳动成果: 用浏览器访问: http://127.0.0.1/test 如果看到: This is our test wSGI example application 则说明完全配置成功.
6. 其他的按需要进行定制。 ======================================================================== 以下为 uWSGI 官方配置 Nginx 代理 uWSGI 见: http://projects./uwsgi/wiki/RunOnNginx =========================================================================
|