---
title: "CentOS 4.8 配置 nginx 和 fastcgi 运行 django 应用（下）"
author: "ezerg"
date: "2011-10-26T19:53:28Z"
tags: []
categories: ["操作系统"]
slug: "centos-4-8-nginx-fastcgi-django"
draft: false
lastmod: "2011-10-26T19:53:28Z"
id: 1217734
url: "https://ezerg.iteye.com/blog/1217734"
---

四、安装 flup 支持 FastCGI
nginx 默认已经整合了mod_fastcgi，所以我们只需要安装 flup
# tar zxvf flup-1.0.2.tar.gz
复制文件到 flup 目录
# cp flup-1.0.2-py2.5.egg flup-1.0.2/ 
# cd flup-1.0.2
# python setup.py install

打开 nginx 配置文件 conf/nginx.conf 修改为：
……
        location / {
            fastcgi_pass unix:/opt/mysite/mysite.sock; // 只能在 linux 或 unix 使用
			  fastcgi_pass 127.0.0.1:8501; // 通用，包括 windows 也可以使用
            include fastcgi_params; // 注释文件中的SCRIPT_NAME
        }
……

重启启动 nginx
# /usr/local/nginx/sbin/nginx -s reload
运行 FastCGI，在 /opt/mysite 项目下执行
# python manage.py runfcgi method=threaded host=127.0.0.1 port=8501

在系统中查看 python 进程
# ps -ef | grep python
root     22976     1  4 21:47         00:00:00 python manage.py runfcgi host=127.0.0.1 port=8501
root     22977 22976  0 21:47         00:00:00 python manage.py runfcgi host=127.0.0.1 port=8501
root     22978 22976  0 21:47         00:00:00 python manage.py runfcgi host=127.0.0.1 port=8501
root     22979 22976  0 21:47         00:00:00 python manage.py runfcgi host=127.0.0.1 port=8501
root     22980 22976  0 21:47         00:00:00 python manage.py runfcgi host=127.0.0.1 port=8501
root     22981 22976  0 21:47         00:00:00 python manage.py runfcgi host=127.0.0.1 port=8501

以上说明启动成功，再通过浏览器访问 nginx 即可出现 django 的主界面。

补充：也可以利用负载均衡的方式配置，修改 nginx.conf 文件，关键几点如下:
worker_processes 2;#开2个nginx进程，在任务管理器可以看到这2个子进程和一个nginx主进程，一般按CPU数配置
events {
    worker_connections 64;#每个进程链接队列长度，可按实际设置
}
http {
    ... # 省略其它配置
    # upstream 段要放在 http 段中
    # 和后端的2个 fastcgi 服务通讯，weight是权重
    upstream fastcgi {
        server 127.0.0.1:8051 weight=1;
        server 127.0.0.1:8052 weight=1;
    }
    server {
        listen 80;
        server_name localhost;
        ...# 省略其它配置
        location / {
            # 指定 fastcgi 的主机和端口，这里的 fastcgi 指的是上面的upstream
            fastcgi_pass fastcgi;
            fastcgi_index index;
            fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;
            include fastcgi_params;
        }
        ...# 省略其它配置
    }
    ...# 省略其它内容
}

运行2个fastcgi服务，开 2 个命令窗口，在 /opt/mysite 目录下运行：
# python manage.py runfcgi host=127.0.0.1 port=8501 method=threaded daemonize=false
# python manage.py runfcgi host=127.0.0.1 port=8502 method=threaded daemonize=false

开 2个 fastcgi 的命令窗口，终止其中一个访问看看，其实没有任何影响。再把运行的停止，停止的启动看看，还是能访问。若2个都终止，发现不能访问。
在 nginx 的集群配置中，nginx 使用最简单的平均分配规则给集群中的每个节点分配请求。一旦某个节点失效时，或者重新起效时，nginx 都会非常及时的处理状态的变化，以保证不会影响到用户的访问。


五、安装 uWSGI 代替 FastCGI
uwsgi 是一个用 c 语言开发的一个 wsgi 服务器，旨在提供专业的 Python web 应用发布和开发，网上测评性能高于 FastCGI
官方网站：http://projects.unbit.it/uwsgi/
下载最新的版本，编译安装
# tar zxvf uwsgi-0.9.6.8.tar.gz
# cd uwsgi-0.9.6.8
# make
将编译的二进制文件复制到系统路径
# cp uwsgi /usr/bin

修改 nginx 配置文件：
 ……
       location / {
            include uwsgi_params;
            uwsgi_pass 0.0.0.0:9000;
        }
……
修改完成后，重新启动 nginx 

与 fastcgi 不同的是它需要一个启动文件，位置 /opt/uwsgi/mysite.py
#!/usr/bin/env python
#encoding=utf-8
import os,sys
import django.core.handlers.wsgi

os.environ = 'mysite.settings'

path = '/opt'
if path not in sys.path:
    sys.path.append(path)
print sys.path

application = django.core.handlers.wsgi.WSGIHandler()

启动 uwsgi ，注意 mysite 不要扩展名 .py
# uwsgi -s :9000 -w /opt/uwsgi/mysite

其它启动参数：
并发4个线程
uwsgi -s :9000 -w myapp -p 4
主控制线程+4个线程
uwsgi -s :9000 -w myapp -M -p 4
执行超过30秒的client直接放弃
uwsgi -s :9000 -w myapp -M -p 4 -t 30
限制内存空间128M
uwsgi -s :9000 -w myapp -M -p 4 -t 30 --limit-as 128
服务超过10000个req自动respawn
uwsgi -s :9000 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000
后台运行等
uwsgi -s :9000 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000 -d uwsgi.log