四、安装flup支持FastCGI

nginx默认已经整合了mod_fastcgi,所以我们只需要安装flup

#tarzxvfflup-1.0.2.tar.gz

复制文件到flup目录

#cpflup-1.0.2-py2.5.eggflup-1.0.2/

#cdflup-1.0.2

#pythonsetup.pyinstall

打开nginx配置文件conf/nginx.conf修改为:

……

location/{

fastcgi_passunix:/opt/mysite/mysite.sock;//只能在linux或unix使用

fastcgi_pass127.0.0.1:8501;//通用,包括windows也可以使用

includefastcgi_params;//注释文件中的SCRIPT_NAME

}

……

重启启动nginx

#/usr/local/nginx/sbin/nginx-sreload

运行FastCGI,在/opt/mysite项目下执行

#pythonmanage.pyrunfcgimethod=threadedhost=127.0.0.1port=8501

在系统中查看python进程

#ps-ef|greppython

root229761421:4700:00:00pythonmanage.pyrunfcgihost=127.0.0.1port=8501

root2297722976021:4700:00:00pythonmanage.pyrunfcgihost=127.0.0.1port=8501

root2297822976021:4700:00:00pythonmanage.pyrunfcgihost=127.0.0.1port=8501

root2297922976021:4700:00:00pythonmanage.pyrunfcgihost=127.0.0.1port=8501

root2298022976021:4700:00:00pythonmanage.pyrunfcgihost=127.0.0.1port=8501

root2298122976021:4700:00:00pythonmanage.pyrunfcgihost=127.0.0.1port=8501

以上说明启动成功,再通过浏览器访问nginx即可出现django的主界面。

补充:也可以利用负载均衡的方式配置,修改nginx.conf文件,关键几点如下:

worker_processes2;#开2个nginx进程,在任务管理器可以看到这2个子进程和一个nginx主进程,一般按CPU数配置

events{

worker_connections64;#每个进程链接队列长度,可按实际设置

}

http{

...#省略其它配置

#upstream段要放在http段中

#和后端的2个fastcgi服务通讯,weight是权重

upstreamfastcgi{

server127.0.0.1:8051weight=1;

server127.0.0.1:8052weight=1;

}

server{

listen80;

server_namelocalhost;

...#省略其它配置

location/{

#指定fastcgi的主机和端口,这里的fastcgi指的是上面的upstream

fastcgi_passfastcgi;

fastcgi_indexindex;

fastcgi_paramSCRIPT_FILENAME/script$fastcgi_script_name;

includefastcgi_params;

}

...#省略其它配置

}

...#省略其它内容

}

运行2个fastcgi服务,开2个命令窗口,在/opt/mysite目录下运行:

#pythonmanage.pyrunfcgihost=127.0.0.1port=8501method=threadeddaemonize=false

#pythonmanage.pyrunfcgihost=127.0.0.1port=8502method=threadeddaemonize=false

开2个fastcgi的命令窗口,终止其中一个访问看看,其实没有任何影响。再把运行的停止,停止的启动看看,还是能访问。若2个都终止,发现不能访问。

在nginx的集群配置中,nginx使用最简单的平均分配规则给集群中的每个节点分配请求。一旦某个节点失效时,或者重新起效时,nginx都会非常及时的处理状态的变化,以保证不会影响到用户的访问。

五、安装uWSGI代替FastCGI

uwsgi是一个用c语言开发的一个wsgi服务器,旨在提供专业的Pythonweb应用发布和开发,网上测评性能高于FastCGI

官方网站:http://projects.unbit.it/uwsgi/

下载最新的版本,编译安装

#tarzxvfuwsgi-0.9.6.8.tar.gz

#cduwsgi-0.9.6.8

#make

将编译的二进制文件复制到系统路径

#cpuwsgi/usr/bin

修改nginx配置文件:

……

location/{

includeuwsgi_params;

uwsgi_pass0.0.0.0:9000;

}

……

修改完成后,重新启动nginx

与fastcgi不同的是它需要一个启动文件,位置/opt/uwsgi/mysite.py

#!/usr/bin/envpython

#encoding=utf-8

importos,sys

importdjango.core.handlers.wsgi

os.environ['DJANGO_SETTINGS_MODULE']='mysite.settings'

path='/opt'

ifpathnotinsys.path:

sys.path.append(path)

printsys.path

application=django.core.handlers.wsgi.WSGIHandler()

启动uwsgi,注意mysite不要扩展名.py

#uwsgi-s:9000-w/opt/uwsgi/mysite

其它启动参数:

并发4个线程

uwsgi-s:9000-wmyapp-p4

主控制线程+4个线程

uwsgi-s:9000-wmyapp-M-p4

执行超过30秒的client直接放弃

uwsgi-s:9000-wmyapp-M-p4-t30

限制内存空间128M

uwsgi-s:9000-wmyapp-M-p4-t30--limit-as128

服务超过10000个req自动respawn

uwsgi-s:9000-wmyapp-M-p4-t30--limit-as128-R10000

后台运行等

uwsgi-s:9000-wmyapp-M-p4-t30--limit-as128-R10000-duwsgi.log