nginx fastcgi python_Nginx + webpy 和FastCGI搭建webpy环境
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 fastcgi python_Nginx + webpy 和FastCGI搭建webpy环境的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光立方原理讲解_90%人不理解什么是防眩
- 下一篇: python figure函数 gui_