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 make

yum install gcc gcc-c++   python-setuptools  python-pip

pip  install  web.py

 

2.安装

2.1 zlib安装

复制代码
tar zxvf zlib-1.2.8.tar.gz

cd zlib-1.2.8

./configure

make && make  install
复制代码

 

2.2 pcre安装

复制代码
tar zxvf pcre-8.34.tar.gz

cd 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.1c

make && 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.gz

cd spawn-fcgi-1.6.4

./configure

make && make install
复制代码

 

 

tar zxvf flup-1.0.2.tar.gz

cd flup-1.0.2

python 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

1
2
3
if  __name__ = =  "__main__" :
  <span style = "color: #ff0000;" >web.wsgi.runwsgi =  lambda  func, addr = None : web.wsgi.runfcgi(func, addr)< / span>
  app.run()

 

同时配置一个启动脚本  

[root@roddy www]# vim start.sh 
#!/bin/sh
spawn-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]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值