Nginx + webpy 和FastCGI搭建webpy环境

本文介绍如何使用Nginx、web.py、Spawn-fcgi和Flup搭建一个简单的Python Web应用,并详细说明了各组件的安装配置过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

web.py 是一个轻量级Python web框架,它简单而且功能大。web.py是一个开源项目。


1、所需要的软件:

Nginx nginx-1.4.7.tar.gz (需要包含fastcgi和rewrite模块)。

Webpy 0.32

Spawn-fcgi 1.6.2

Flup

注意:Flup是最常见的忘记装的软件,需要安装

更老的版本应该也可以工作,但是没有测试过,最新的是可以工作的

2、安装软件

安装nginx:

1
2
3
4
5
6
wget http://nginx.org/download/nginx-1.4.7.tar.gz
tar zxvf nginx-1.4.7.tar.gz
cd nginx-1.4.7
yum -y install pcre pcre-devel
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_stub_status_module
make && make install

安装web.py、Spawn-fcgi 、Flup

1
2
3
4
5
6
7
8
9
10
11
安装spawn-fcgi
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
tar zxvf spawn-fcgi-1.6.3.tar.gz
./configure
make && make install
                                 
安装flup
pip install flup
                                 
安装web.py
pip install web.py

nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
      listen       80;
      server_name  localhost;
      root /usr/local/nginx/html/webpy;
      location / {
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
          fastcgi_param PATH_INFO $fastcgi_script_name;   
          fastcgi_pass 127.0.0.1:9002;
          }
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
       location /static/ {
          if (-f $request_filename) {
          rewrite ^/static/(.*)$  /static/$1 break;
          }
       }
                         
}

检查配置文件并启动nginx

1
2
3
4
[root@test controllers]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@test controllers]# /usr/local/nginx/sbin/nginx

在web跟目录创建一个python文件

将下面的代码保存为index.py(或者任何你喜欢的),注意,使用Nginx配置的话,web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)这一行代码是必须的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python
# -*- coding: utf-8 -*-
                
import web
                
urls = ("/.*", "hello")
app = web.application(urls, globals())
                
class hello:
     def GET(self):
         return 'Hello, world! 3305'
                
if __name__ == "__main__":
     web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
     app.run()

注意: 同样需要给代码设置权限,代码如下chmod +x index.py。

启动和关闭Spawn-fcgi

1
2
3
4
5
启动spawn-fcgi
spawn-fcgi -d /path/to/www -f /path/to/www/index.py -a 127.0.0.1 -p 9002
            
关闭Spawn-fcgi
kill `pgrep -f "python /path/to/www/index.py"`

在浏览器上输入 IP访问出现如下图所示:

wKiom1NbT2Wja1qLAABzWZvZQXY081.jpg


本文转自1594cqb 51CTO博客,原文链接:http://blog.51cto.com/wolfchen/1403377,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值