首页 > 代码库 > webpy + nginx + fastcgi 构建python应用

webpy + nginx + fastcgi 构建python应用

1.准备环境

CentOs  6.3

nginx-1.4.2.tar.gz            http://nginx.org/download/nginx-1.4.2.tar.gz

openssl-1.0.1c.tar.gz       http://www.openssl.org/source/openssl-1.0.1c.tar.gz

pcre-8.34.tar.gz              ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz 

spawn-fcgi-1.6.4.tar.gz    http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.4.tar.gz

zlib-1.2.8.tar.gz              http://zlib.net/zlib-1.2.8.tar.gz

flup-1.0.2.tar.gz              http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz

 

yum -y install gcc automake autoconf libtool makeyum install gcc gcc-c++   python-setuptools  python-pippip  install  web.py

 

2.安装

2.1 zlib安装

tar zxvf zlib-1.2.8.tar.gzcd zlib-1.2.8./configuremake && make  install

 

2.2 pcre安装

tar zxvf pcre-8.34.tar.gzcd pcre-8.34./configure 
make && make install

 

2.3 nginx安装

tar zxvf nginx-1.4.2.tar.gz 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 --with-http_ssl_module  --with-pcre=../pcre-8.34 --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-1.0.1cmake && make install

 

 

3.测试nginx

启动:/usr/local/nginx/nginx

访问: http://x.x.x.x  

出现如下表示nginx安装正常

暂时先关闭nginx,以下是关闭nginx脚本

#!/bin/sh # # nginx - this script starts and stops the nginx daemin # # chkconfig:   - 85 15 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \ #               proxy and IMAP/POP3 proxy server # processname: nginx # config:      /usr/local/nginx/conf/nginx.conf # pidfile:     /usr/local/nginx/logs/nginx.pid  # Source function library. . /etc/rc.d/init.d/functions  # Source networking configuration. . /etc/sysconfig/network  # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0  nginx="/usr/local/nginx/nginx" prog=$(basename $nginx)  NGINX_CONF_FILE="/usr/local/nginx/nginx.conf"  lockfile=/var/lock/subsys/nginx  start() {     [ -x $nginx ] || exit 5     [ -f $NGINX_CONF_FILE ] || exit 6     echo -n $"Starting $prog: "     daemon $nginx -c $NGINX_CONF_FILE     retval=$?     echo     [ $retval -eq 0 ] && touch $lockfile     return $retval }  stop() {     echo -n $"Stopping $prog: "     killproc $prog -QUIT     retval=$?     echo     [ $retval -eq 0 ] && rm -f $lockfile     return $retval }  restart() {     configtest || return $?     stop     start }  reload() {     configtest || return $?     echo -n $"Reloading $prog: "     killproc $nginx -HUP     RETVAL=$?     echo }  force_reload() {     restart }  configtest() {   $nginx -t -c $NGINX_CONF_FILE }  rh_status() {     status $prog }  rh_status_q() {     rh_status >/dev/null 2>&1 }  case "$1" in     start)         rh_status_q && exit 0         $1         ;;     stop)         rh_status_q || exit 0         $1         ;;     restart|configtest)         $1         ;;     reload)         rh_status_q || exit 7         $1         ;;     force-reload)         force_reload         ;;     status)         rh_status         ;;     condrestart|try-restart)         rh_status_q || exit 0             ;;     *)         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"         exit 2 esac 

 

 

 

4.安装fcgi及flup

 

tar zxvf spawn-fcgi-1.6.4.tar.gzcd spawn-fcgi-1.6.4./configuremake && make install

 

 

tar zxvf flup-1.0.2.tar.gzcd flup-1.0.2python setup.py  install

 

5.配置nginx及webpy应用

vim  /usr/local/nginx/nginx.conf

worker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    server {        listen       80;        server_name  localhost;        root /data/www/;        access_log  /data/log/test.access.log;        location / {         include fastcgi_params;             fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;  # [1]             fastcgi_param PATH_INFO $fastcgi_script_name;        # [2]         fastcgi_pass 127.0.0.1:9002;        }       location /static/ {                              #配置静态文件路径访问        if (-f $request_filename){            rewrite ^/static/(.*)$ /static/$1 break;        }           }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}

 

web.py应用加上以下内容server.py

if __name__ == "__main__":  web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)  app.run()

 

同时配置一个启动脚本  

[root@roddy www]# vim start.sh #!/bin/shspawn-fcgi -d /data/www -f /data/www/server.py -a 127.0.0.1 -p 9002

 

启动fcgi及nginx

[root@roddy www]# sh start.sh [root@roddy www]# /etc/init.d/nginx  start

 

检查监听端口

 

[root@roddy www]# netstat -nlpt | egrep "9002|80" tcp        0      0 127.0.0.1:9002              0.0.0.0:*                   LISTEN      54799/python        tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      54816/nginx         [root@roddy www]# 

 

现在即可输入http://x.x.x.x访问你的应用吧

 

webpy + nginx + fastcgi 构建python应用