环境介绍:
系统: CentOS 5.8 64位
Python版本:2.7.3
nginx:1.2.4
首先安装python 2.7.3
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
tar jxvf Python-2.7.3.tar.bz2
cd Python-2.7.3
./configure --prefix=/usr/local/python27 --enable-unicode=ucs4
vim Modules/Setup
#去掉注释 463行:
zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
然后接着编译
make && make install
安装完成后我们想更方面的使用我们新安装的Python我们做如下更改
mv /usr/bin/python /usr/bin/python24
ln -s /usr/local/python27/bin/python /usr/bin/python
ln -s /usr/local/python27/bin/python2.7 /usr/bin/python2.7
vim /usr/bin/yum
将/#!/usr/bin/python改为#!/usr/bin/python2.4即可正常工作
安装python的setuptools,配备easy_install。easy_install用于安装Python第三方扩展包而且只要一个命令即可完成:
wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg
安装好之后我们做一个软链接方便我们使用:
ln -s /usr/local/python27/bin/easy_install* /usr/bin/
然后我们来使用easy_install来安装Python第三方扩展:
easy_install web.py
easy_install flup
easy_install MySQLdb
然后我们打开Python shell输入
import web
如果没有报错则说明我们安装成功
安装spawn-fcgi :
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
tar zxvf spawn-fcgi-1.6.3.tar.gz
cd spawn-fcgi-1.6.3
./configure --prefix=/usr/local/spawn-fcgi
make && make install
ln -s /usr/local/spawn-fcgi/bin/spawn-fcgi /usr/bin/
到这里我们就完成了Python的所有包安装,下面我们来安装nginx:
yum -y install pcre*
tar zxvf nginx-1.2.4.tar.gz
cd nginx-1.2.4
./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module
make && make install
vim/usr/local/nginx/conf/nginx.conf
location / {
root html;
#index index.html index.htm;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass unix:/tmp/pyweb.sock;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
}
然后创建一个web.py程序:
vim /usr/local/nginx/html/index.py
#!/usr/bin/env python
#-*-coding:utf8-*-
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()
然后赋予其执行权限:
chmod +x /usr/local/nginx/html/index.py
通过命令创建spawn-fcgi进程:
spawn-fcgi -d /usr/local/nginx/html/ -f /usr/local/nginx/html/index.py -s /tmp/pyweb.sock -u nobody -g nobody
我们使用unix socket,现在我们访问http://ip/
就可以看到:Hello, world!