阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx + Web.py 和FastCGI搭建Web.py环境

127次阅读
没有评论

共计 2062 个字符,预计需要花费 6 分钟才能阅读完成。

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:

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

安装 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 配置文件

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

[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) 这一行代码是必须的。

#!/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

启动 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 访问出现如下图所示:

Nginx + Web.py 和 FastCGI 搭建 Web.py 环境

使用 Gunicorn 部署 web.py 应用

Nginx + uWSGI + web.py 搭建示例  http://www.linuxidc.com/Linux/2011-12/48889.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计2062字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中