我的系统环境是CentOS release 6.6(Final)
环境依赖软件包
1、python2(以下的web.py和Flup不兼容python2的版本)2、Nginx1.4(需要包含fastcgi和rewrite模块)
3、web.py 0.38(下载最新版本即可)
4、Spawn-fcgi 1.6.3
5、Flup(必须要安装的python2模块)
软件包安装
python2.7安装
[root@localhost home]# wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
[root@localhost home]# tar xJf Python-2.7.13.tar.xz
[root@localhost Python-2.7.13]# cd Python-2.7.13.tar.xz
[root@localhost Python-2.7.13]# ./configure && make && make install
nginx安装
可以参考CentOS6.6环境中安装Nginx详细过程笔记博客文章
安装spawn-fcgi
[root@localhost home]# wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
[root@localhost home]# tar zxvf spawn-fcgi-1.6.3.tar.gz
[root@localhost spawn-fcgi-1.6.3]# ./configure && make && make install
安装flup
[root@localhost ~]# pip install flup
安装web.py
[root@localhost ~]# pip install web.py
配置
Nginx配置文件如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name pw.com;
root /home/www/test;
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;
}
}
}
}
在/home/www/demo目录下创建index.py如下:
注意: 需要给代码设置执行权限,chmod +x index.py。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
urls = ("/.*", "hello")
app = web.application(urls, globals())
class hello:
def GET(self):
return 'Hello, world!'
if __name__ == "__main__":
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run()
软件启动
Nginx启动
[root@localhost ~]# /usr/local/nginx/nginx
[root@localhost ~]# /usr/local/nginx/nginx -s reload #重启nginx配置
[root@localhost ~]# /usr/local/nginx/nginx -s stop #停止nginx
Spawn-fcgi启动
注意:你可以随意填写地址和端口信息,但是一定需要和Nginx配置文件相匹配
[root@localhost ~]# spawn-fcgi -d /home/www/demo -f /home/www/demo/index.py -a 127.0.0.1 -p 9002
[root@localhost ~]# kill `pgrep -f "python /home/www/demo/index.py"` #关闭进程
测试
[root@localhost ~]# curl localhost
如果打印“hello world!”则表示环境搭建成功