linux系统上nginx+python的web.py与django框架环境

1.编译nginx
在网上买了一本《实战nginx-取代apache的高性能服务器》,写的比较浅,主要是些配置方面的东西,不过却正是目前我所需要的。由于需要支持https和rewrite,所以除了nginx的源码之外,又下载了 openssl-0.9.8r.tar.gz 和 pcre-8.12.tar.gz,把他们和nginx-1.0.4.tar.gz放到同一个目录。
为了方便编译,笔者写了一个脚本,代码如下:

#!/bin/bash
#=============================================================================
#脚本所在绝对目录
abs_path(){
local path=$1
local basename=$( basename $path )
local dirname=$( dirname $path )
cd $dirname
if [ -h $basename ]; then
path=$( readlink $basename )
abs_path $path
else
pwd
fi
}
#=============================================================================
#依赖的目录
src_base_dir=$( abs_path $0 )
src_openssl_dir=$src_base_dir’/openssl-0.9.8r’
src_pcre_dir=$src_base_dir’/pcre-8.12′
src_nginx_dir=$src_base_dir’/nginx-1.0.4′
#=============================================================================
#目标的目录
dest_base_dir=$src_base_dir’/release’
dest_nginx_dir=$dest_base_dir’/nginx’
#=============================================================================
#把所有的tar.gz解压
find . -name “*.tar.gz” | xargs -ix tar zxvf x
#=============================================================================
#编译nginx
cd $src_nginx_dir
chmod u+x ./configure
./configure –with-http_stub_status_module –with-http_ssl_module –with-openssl=$src_openssl_dir –with-pcre=$src_pcre_dir –prefix=$dest_nginx_dir
make && make install

2.配置nginx
在server配置项下增加

location / {
#这两种方法都可以,只不过spawn-cgi启动的方法不同
#fastcgi_pass 127.0.0.1:9002;
fastcgi_pass unix:webpy.sock;
fastcgi_param request_method $request_method;
fastcgi_param query_string $query_string;
fastcgi_param content_type $content_type;
fastcgi_param content_length $content_length;
fastcgi_param gateway_interface cgi/1.1;
fastcgi_param server_software nginx/$nginx_version;
fastcgi_param remote_addr $remote_addr;
fastcgi_param remote_port $remote_port;
fastcgi_param server_addr $server_addr;
fastcgi_param server_port $server_port;
fastcgi_param server_name $server_name;
fastcgi_param server_protocol $server_protocol;
fastcgi_param script_filename $fastcgi_script_name;
fastcgi_param path_info $fastcgi_script_name;
}

这里的3个location配置分别解决了,与python进程通信、django后台管理端样式存放、网站样式存放的问题。对照着apache的配置来看,就很容易明白了

wsgipythoneggs /tmp

servername fuload.qq.com
wsgiscriptalias / /home/dantezhu/htdocs/fuload/conf/setting.wsgi

options followsymlinks
allowoverride
order allow,deny
allow from all

order deny,allow
deny from all

alias /admin_media “/usr/local/lib/python2.7/site-packages/django/contrib/admin/media”

order allow,deny
options indexes
allow from all
indexoptions fancyindexing

#aliasmatch /site_media/(.*\.(css|gif|png|jpg|jpeg)) /home/dantezhu/htdocs/fuload/media/$1
alias /site_media /home/dantezhu/htdocs/fuload/media/

order allow,deny
options indexes
allow from all
indexoptions fancyindexing

3.安装fastcgi依赖
需要到 http://trac.saddi.com/flup下载安装,之后fastcgi才能够正常启动。

4.启动django
创建django project的过程我们就不说了,只列出启动/停止的命令:
启动:

#python manage.py runfcgi daemonize=true pidfile=`pwd`/django.pid host=127.0.0.1 port=9001 maxrequests=1 &
python manage.py runfcgi daemonize=true pidfile=`pwd`/django.pid socket=/home/dantezhu/nginx/sbin/django.sock maxrequests=1 &

停止:

kill -9 `cat django.pid`

启动nginx
启动:

./nginx -p /home/dantezhu/nginx/

停止:

kill -quit `cat ../logs/nginx.pid`

重新载入配置:

./nginx -t -c `pwd`/../conf/nginx.conf
kill -hup `cat ../logs/nginx.pid`

成功显示了django的后台界面:
ppppppppppppppppppppp1

5.部署web.py版
安装依赖
spawn-cgi
flup
配置nginx
在server配置项下增加

location / {
#这两种方法都可以,只不过spawn-cgi启动的方法不同
#fastcgi_pass 127.0.0.1:9002;
fastcgi_pass unix:webpy.sock;
fastcgi_param request_method $request_method;
fastcgi_param query_string $query_string;
fastcgi_param content_type $content_type;
fastcgi_param content_length $content_length;
fastcgi_param gateway_interface cgi/1.1;
fastcgi_param server_software nginx/$nginx_version;
fastcgi_param remote_addr $remote_addr;
fastcgi_param remote_port $remote_port;
fastcgi_param server_addr $server_addr;
fastcgi_param server_port $server_port;
fastcgi_param server_name $server_name;
fastcgi_param server_protocol $server_protocol;
fastcgi_param script_filename $fastcgi_script_name;
fastcgi_param path_info $fastcgi_script_name;
}

一个简单的index.py

#!/usr/bin/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()

并执行:

chmod +x index.py

.启动web.py
启动:

#spawn-fcgi -p `pwd`/webpy.pid -f /home/dantezhu/htdocs/ngx_web/index.py -a 127.0.0.1 -p 9002 &
spawn-fcgi -p `pwd`/webpy.pid -f /home/dantezhu/htdocs/ngx_web/index.py -s /home/dantezhu/nginx/sbin/webpy.sock &

停止:

kill -9 `cat webpy.pid`

启动nginx
加入到rc.local中,自动启动

/home/dantezhu/nginx/sbin/start.sh
sudo -u dantezhu /home/dantezhu/htdocs/ngx_django/mysite/start.sh
sudo -u dantezhu /home/dantezhu/htdocs/ngx_web/start.sh

Posted in 未分类

发表评论