一、序言
  兔尾的时候,将 wubi 安装的 ubuntu 系统给卸载了,原因是容量不够了,捣鼓了下扩展也捣鼓不出来,而且总感觉系统运行很慢,所以一不做二不休就将系统给卸载了,重新整过,而且整成了独立得双系统,然而等我装好之后,就下班时间了,所以今年回来第一件事当然就是重新配置一份环境了。因为自接触 PHP 以来,一直用得就是 Apache 得服务器,这回就用上了Nginx,顺便试试源码安装得方式,虽然麻烦点。

二、准备工作
  本机子环境:64 位 Ubuntu 11.10
  1、下载 Nginx: http://nginx.org/en/download.html
  #我用的是 1.1.13 版
  2、下载 PCRE: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
  #我用的是 8.20 版本

三、安装及配置过程
3.1 安装 PCRE
因为我是用源码安装 Nginx,google 了下,安装这个是为了更好的在 Nginx 中使用正则相关



# tar zxvf pcre-8.20.tar.gz
# cd pcre-8.20
# ./configure –prefix=/usr/local/pcre-8.20
#安装到/usr/local/pcre-8.20 下
# make
# sudo make install



3.2 安装 Nginx



# tar zxvf nginx-1.1.13.tar.gz
# cd nginx-1.1.13
# ./configure –prefix=/usr/local/nginx –pid-path=/var/run/nginx.pid--with-http_stub_status_module –with-http_ssl_module



这个时候,发现出现错误:
缺少 pcre library
./configure: error: the HTTP rewrite module requires the PCRElibrary.You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.
可是我明明源码安装了 pcre 还是错误,百思不得其解,看提示难道需要—winth-pcre 指定?
于是我如下配置:



# ./configure –prefix=/usr/local/nginx –pid-path=/var/run/nginx.pid --with-http_stub_status_module –with-http_ssl_module –with-pcre=/usr/local/pcre-8.20/lib



然而还是错误,于是我打算查看下—with-pcre 选项:



# ./configure –help



结果发现如下:



--with-pcre  force PCRE library usage
--with-pcre=DIR  set path to PCRE library sources   #需要指向pcre的源码路径



需要指向源码??反正试试再说如下:



# ./configure –prefix=/usr/local/nginx –pid-path=/var/run/nginx.pid --with-http_stub_status_module –with-http_ssl_module –with-pcre=/home/xiaoxiao/Download/pcre-8.20



发现编译可以通过,难道它会重新需要编译 PCRE 么?不明~
然而接下来还出现了几个错误:

错误一: 缺少 openssl 库
./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. You can either disable the module by using --without-http-cache option, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-http_ssl_module --with-openssl=<path> options.
解决办法:安装 openssl:



# sudo apt-get install openssl



错误二:缺少 zlib 库
./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the  system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option.
解决办法:安装 libssl-dev



# sudo apt-get install libssl-dev



以上完了之后:



# ./configure –prefix=/usr/local/nginx –pid-path=/var/run/nginx.pid --with-http_stub_status_module –with-http_ssl_module –with-pcre=/home/xiaoxiao/Download/pcre-8.20
# make
# sudo make install



完成之后,需要将/usr/local/nginx/logs 目录设置为可写权限,里面是一系列得日志文件:



# sudo chmod 777 R /usr/local/nginx/logs
# nginx #启动 nginx
# curl -i http://localhost



得到如下:



HTTP/1.1 200 OK
Server: nginx/1.1.13
Date: Mon, 30 Jan 2012 09:44:28 GMT
Content-Type: text/html
Content-Length: 151
Last-Modified: Mon, 30 Jan 2012 08:57:55 GMT
Connection: keep-alive
Accept-Ranges: bytes
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>



也可打开网页然后运行 localhost 即可看到:“Welcome to nginx!”字样

3.3 注意事项
在源码安装的时候,需要安装编译环境,gcc ubuntu 下,可以直接 apt-get 安装:



# sudo apt-get install libtool
# sudo apt-get install gcc-c++



安装 gcc 出现如下错误:
安装 gcc-c++出错



Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Couldn't find package gcc-c



则先如下操作,再次安装:



# sudo apt-get install build-essential
# sudo apt-get update
# sudo apt-get upgrade
# sudo apt-get install gcc-c++



 

四、配置 Nginx 为系统服务
1、 配置到系统 path 中



# sudo vim /etc/bash.bashrc



在该文件最后添加:



if [ -d "/usr/local/nginx/sbin" ]; then
PATH="$PATH:/usr/local/nginx/sbin"
fi



2、添加启动管理文件并让 ubuntu 开机时自动运行 nginx 网页服务器



# wget http://nginx-init-ubuntu.googlecode.com/files/nginx-init-ubuntu_v2.0.0-RC2.tar.bz2
# tar jxvf nginx-init-ubuntu_v2.0.0-RC2.tar.bz2
# sudo vim nginx



更改 nginx 安装路径及 nginx 配置文件路径



PATH=/usr/local/nginx/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
PS="nginx"                
PIDNAME="nginx"            
PIDFILE=$PIDNAME.pid          
PIDSPATH=/var/run            
DESCRIPTION="Nginx Server..."    
RUNAS=root               
SCRIPT_OK=0              
SCRIPT_ERROR=1            
TRUE=1
FALSE=0

lockfile=/var/lock/subsys/nginx
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"



将该文件放到/usr/local/nginx 下并设为可执行,并且添加软连到/etc/init.d 执行:



# sudo mv nginx /usr/local/nginx
# chmod +x /usr/local/nginx/nginx
# sudo ln -s /usr/local/nginx/nginx /etc/init.d/nginx   #添加软连到/etc/init.d 目录下
# update-rc.d -f nginx defaults



启动和停止/重启命令:



#sudo /etc/init.d/nginx start   
#sudo /etc/init.d/nginx stop
#sudo /etc/init.d/nginx restart