---
title: "CentOS 4.8 配置 nginx 和 fastcgi 运行 django 应用（上）"
author: "ezerg"
date: "2011-10-25T21:57:37Z"
tags: ["CentOS", "Django", "nginx"]
categories: ["操作系统"]
slug: "centos-4-8-nginx-fastcgi-django"
draft: false
lastmod: "2011-10-25T21:57:37Z"
id: 1214177
url: "https://ezerg.iteye.com/blog/1214177"
---


一、安装 nginx
为了确保能在 nginx 中使用正则表达式进行更灵活的配置，安装之前需要确定系统是否安装有 PCRE (Perl Compatible Regular Expressions) 包。
可以到 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的PCRE源码包
使用下面命令编译和安装：
# tar zxvf pcre-7.8.tar.gz
# cd pcre-7.8
# ./configure
# make
# make install

安装 Nginx，下面是把 Nginx 安装到 /usr/local/nginx 目录下：
# tar zxvf nginx-0.7.65.tar.gz
# cd nginx-0.7.65
去掉 nginx 默认使用 debug 模式编译
# vi auto/cc/gcc
……
CFLAGS="$CFLAGS -g" //注释掉这一行
……
可以优化的选项
--with-cc-opt=-O2
--with-cpu-opt=CPU  build for specified CPU, the valid values:pentium, pentiumpro, pentium3, pentium4,athlon, opteron, sparc32, sparc64, ppc64
# ./configure --with-http_stub_status_module
# make
# make install		

其中参数 --with-http_stub_status_module 是为了启用 nginx 的 NginxStatus 功能，用来监控 nginx 的当前状态。
其它可选参数： --with-md5=/usr/lib --with-sha1=/usr/lib --with-openssl=/usr/lib/ssl/ --with-http_ssl_module
安装成功后 /usr/local/nginx 目录下有四个子目录分别是：conf、html、logs、sbin 。其中配置文件位于conf/nginx.conf，程序文件位于 sbin/nginx。
确保系统的 80 端口没被其他程序占用，运行 sbin/nginx 命令来启动 nginx，打开浏览器访问此机器的IP，如果浏览器出现 Welcome to nginx! 则表示已经安装并运行成功。

二、安装 Python 和 MySQL-python
Django1.2.7 推荐 Python 是 2.5 以上的版本，首先要安装 Python 2.5.4
#t ar zxvf Python-2.5.4.tgz
# cd Python-2.5.4
# ./configure
# make
# make install

安装 Python 连接 MySQL 的模块，先需要安装 MySQL，安装路径：/usr/local/mysql5.1
# tar zxvf MySQL-python-1.2.2.tar.gz
# cd MySQL-python-1.2.2
设置 mysql_config 的路径
#  vi site.cfg 
......
mysql_config = /usr/local/mysql5.1/bin/mysql_config
......
# chmod +x setup.py
# ./setup.py build
# ./setup.py install
测试是否安装成功
# python
>>>import MySQLdb
......报错，libmysqlclient_r.so.16 没找到。
ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory
.....
解决方法
# updatedb
# locate libmysqlclient_r.so.16
/usr/local/mysql5.1/lib/mysql/libmysqlclient_r.so.16.0.0
/usr/local/mysql5.1/lib/mysql/libmysqlclient_r.so.16
#vi /etc/ld.so.conf
加入动态库文件的路径：/usr/local/mysql5.1/lib/mysql/
# ldconfig  -v 

再次测试，执行成功！

动态链接库的管理命令 ldconfig 说明：
1、ldconfig命令的用途：主要是在默认搜寻目录（/lib 和 /usr/lib）以及动态库配置文件 /etc/ld.so.conf 内所列的目录下，搜索出可共享的动态链接库（格式如前介绍，lib*.so*），
进而创建出动态装入程序（ld.so）所需的连接和缓存文件。缓存文件默认为 /etc/ld.so.cache，此文件保存已排好序的动态链接库名字列表。 
2、ldconfig通常在系统启动时运行，而当用户安装了一个新的动态链接库时，就需要手工运行这个命令。 

三、安装 Django
# tar zxvf Django-1.2.7.tar.gz
# cd Django-1.2.7
# python setup.py install

利用 Django 创建新的站点，假设在 /opt 下新建站点 mysite
# cd /opt
查看 Django 工具的帮助信息
# django-admin.py help

利用 startproject 选项创建站点 mysite
# django-admin.py startproject mysite
默认为新建目录 mysite ，该目录下有四个文件：__init__.py  manage.py  settings.py  urls.py

查看新站点管理工具的帮助信息
# cd mysite
# python manage.py help
启动测试服务器，验证是否安装成功
# python manage.py runserver
启动成功则出现如下信息：
Validating models...
0 errors found

Django version 1.0.2 final, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

注意测试服务器只能本机访问。


