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(){
localpath=$1
localbasename=$(basename$path)
localdirname=$(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-IXtarzxvfX
#=============================================================================
#编译nginx
cd$src_nginx_dir
chmodu+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&&makeinstall
2.配置nginx
在server配置项下增加
location/{
#这两种方法都可以,只不过spawn-cgi启动的方法不同
#fastcgi_pass127.0.0.1:9002;
fastcgi_passunix:webpy.sock;
fastcgi_paramREQUEST_METHOD$request_method;
fastcgi_paramQUERY_STRING$query_string;
fastcgi_paramCONTENT_TYPE$content_type;
fastcgi_paramCONTENT_LENGTH$content_length;
fastcgi_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWAREnginx/$nginx_version;
fastcgi_paramREMOTE_ADDR$remote_addr;
fastcgi_paramREMOTE_PORT$remote_port;
fastcgi_paramSERVER_ADDR$server_addr;
fastcgi_paramSERVER_PORT$server_port;
fastcgi_paramSERVER_NAME$server_name;
fastcgi_paramSERVER_PROTOCOL$server_protocol;
fastcgi_paramSCRIPT_FILENAME$fastcgi_script_name;
fastcgi_paramPATH_INFO$fastcgi_script_name;
}
这里的3个location配置分别解决了,与python进程通信、django后台管理端样式存放、网站样式存放的问题。对照着apache的配置来看,就很容易明白了
WSGIPythonEggs/tmp <VirtualHost*> ServerNamefuload.qq.com WSGIScriptAlias//home/dantezhu/htdocs/fuload/conf/setting.wsgi <Directory/> OptionsFollowSymLinks AllowOverride Orderallow,deny Allowfromall </Directory> <Directory"/home/dantezhu/htdocs/fuload/mysite"> OrderDeny,Allow Denyfromall </Directory> Alias/admin_media"/usr/local/lib/python2.7/site-packages/django/contrib/admin/media" <Directory"/usr/local/lib/python2.7/site-packages/django/contrib/admin/media"> Orderallow,deny OptionsIndexes Allowfromall IndexOptionsFancyIndexing </Directory> #AliasMatch/site_media/(.*\.(css|gif|png|jpg|jpeg))/home/dantezhu/htdocs/fuload/media/$1 Alias/site_media/home/dantezhu/htdocs/fuload/media/ <Directory"/home/dantezhu/htdocs/fuload/media/"> Orderallow,deny OptionsIndexes Allowfromall IndexOptionsFancyIndexing </Directory> </VirtualHost>
3.安装fastcgi依赖
需要到http://trac.saddi.com/flup下载安装,之后fastcgi才能够正常启动。
4.启动django
创建djangoproject的过程我们就不说了,只列出启动/停止的命令:
启动:
#pythonmanage.pyrunfcgidaemonize=truepidfile=`pwd`/django.pidhost=127.0.0.1port=9001maxrequests=1& pythonmanage.pyrunfcgidaemonize=truepidfile=`pwd`/django.pidsocket=/home/dantezhu/nginx/sbin/django.sockmaxrequests=1&
停止:
kill-9`catdjango.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_pass127.0.0.1:9002;
fastcgi_passunix:webpy.sock;
fastcgi_paramREQUEST_METHOD$request_method;
fastcgi_paramQUERY_STRING$query_string;
fastcgi_paramCONTENT_TYPE$content_type;
fastcgi_paramCONTENT_LENGTH$content_length;
fastcgi_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWAREnginx/$nginx_version;
fastcgi_paramREMOTE_ADDR$remote_addr;
fastcgi_paramREMOTE_PORT$remote_port;
fastcgi_paramSERVER_ADDR$server_addr;
fastcgi_paramSERVER_PORT$server_port;
fastcgi_paramSERVER_NAME$server_name;
fastcgi_paramSERVER_PROTOCOL$server_protocol;
fastcgi_paramSCRIPT_FILENAME$fastcgi_script_name;
fastcgi_paramPATH_INFO$fastcgi_script_name;
}
一个简单的index.py
#!/usr/bin/python
#-*-coding:utf-8-*-
importweb
urls=("/.*","hello")
app=web.application(urls,globals())
classhello:
defGET(self):
return'Hello,world!'
if__name__=="__main__":
web.wsgi.runwsgi=lambdafunc,addr=None:web.wsgi.runfcgi(func,addr)
app.run()
并执行:
chmod+xindex.py
.启动web.py
启动:
#spawn-fcgi-P`pwd`/webpy.pid-f/home/dantezhu/htdocs/ngx_web/index.py-a127.0.0.1-p9002& spawn-fcgi-P`pwd`/webpy.pid-f/home/dantezhu/htdocs/ngx_web/index.py-s/home/dantezhu/nginx/sbin/webpy.sock&
停止:
kill-9`catwebpy.pid`
启动nginx
加入到rc.local中,自动启动
/home/dantezhu/nginx/sbin/start.sh sudo-udantezhu/home/dantezhu/htdocs/ngx_django/mysite/start.sh sudo-udantezhu/home/dantezhu/htdocs/ngx_web/start.sh