2013年9月25日星期三

web.py作为fast cgi部署到nginx上

部署方法都来自这个帖子:http://webpy.org/cookbook/fastcgi-nginx
我只是对一些细节进行补充说明

#perl 的正则表达式库
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
tar -zxvf pcre-8.33.tar.gz
cd pcre-8.33
./configure && make && make install
#openssl,注意,只有1.0.0才能和nginx一起编译通过
wget http://www.openssl.org/source/openssl-1.0.0.tar.gz
tar -zxvf openssl-1.0.0.tar.gz
#不用编译openssl
cd nginx-1.4.2
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --without-select_module --without-poll_module --with-http_ssl_module --with-http_spdy_module --with-http_gunzip_module --with-http_gzip_static_module --with-openssl=/data/temp/openssl-1.0.0
make && make install
#
wget http://www.saddi.com/software/flup/dist/flup-1.0.1.tar.gz
tar -zxvf flup-1.0.1.tar.gz
cd flup-1.0.1
python setup.py build && python setup.py install
#
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
tar -zxvf spawn-fcgi-1.6.3.tar.gz
cd spawn-fcgi-1.6.3 && make && make install

#下面配置nginx
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:9002;
}
#配置static目录
    location /static/ {
        root /path/to/www;
        if (-f $request_filename) {
           rewrite ^/static/(.*)$  /static/$1 break;
        }
    }

#启动spawn-fcgi
#vi my_web.py
# 加上 
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#注意文件本身不能是utf-8编码的

chmod +x my_web.py
chown -R user_0:users  my_web/

spawn-fcgi -d /data/my_web -f /data/my_web/my_web.py -a 127.0.0.1 -p 9002 -u user_0 -g users -F 5

换成nginx后,整个站点快了好多,爽多了!