服务器均为CentOS release 5.6 (Final)
一、安装
1.yum安装
yum安装比较简单,配置好源以后直接yum install即可。
①配置第三方yum源(CentOS的默认源里是没有nginx软件包的)
yum install wget #安装下载工具wget wget http://www.atomicorp.com/installers/atomic #下载atomic yum源 sh ./atomic #安装 yum check-update #更新yum软件包
②安装配置
1 yum install nginx #安装nginx,根据提示,输入Y安装即可成功安装 2 service nginx start #启动 3 chkconfig nginx on #设为开机启动 4 /etc/init.d/nginx restart #重启 5 rm -rf /usr/share/nginx/html/* #删除ngin默认测试页
2.源码编译安装
源码安装稍微复杂一点,有一些相关的依赖包需要单独编译
源码安装既可以选择原版的nginx也可以使用淘宝修改过的Tengine,这里推荐使用Tengin,Tengine完全兼容nginx并且加入了许多的新特性,适合日益复杂的业务扩展。
Tengine官网:http://tengine.taobao.org/
①先安装pcre,用于支持nginx的伪静态
# cd /usr/local/src
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz # tar zxvf pcre-8.21.tar.gz # mkdir /usr/local/pcre #创建安装目录 # cd pcre-8.21 # ./configure --prefix=/usr/local/pcre #配置 # make # make install
②安装Tengine
# cd /usr/local/src
# wget http://tengine.taobao.org/download/tengine-1.4.2.tar.gz # tar zxvf tengine-1.4.2.tar.gz # cd tengine # ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.21
# make
# make install
# /usr/local/nginx/sbin/nginx #启动nginx
# chown nobody.nobody -R /usr/local/nginx/html
# chmod 700 -R /usr/local/nginx/html
注意:--with-pcre=/usr/local/src/pcre-8.21指向的是源码包解压的路径,而不是安装的路径,否则会报错。
③设置Tengine开机启动
# vi /etc/rc.d/init.d/nginx #编辑启动文件添加下面内容
#!/bin/bash # Tengine Startup script# processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/usr/local/nginx/logs/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "tengine already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid } reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
保存退出
# chmod 775 /etc/rc.d/init.d/nginx #赋予文件执行权限 # chkconfig nginx on #设置开机启动 # /etc/rc.d/init.d/nginx restart
至此,nginx安装结束。这里再说一下为什么要使用淘宝的Tengine:
- 继承Nginx-1.2.5的所有特性,100%兼容Nginx的配置;
- 动态模块加载(DSO)支持。加入一个模块不再需要重新编译整个Tengine;
- 输入过滤器机制支持。通过使用这种机制Web应用防火墙的编写更为方便;
- 动态脚本语言Lua支持。扩展功能非常高效简单;
- 支持管道(pipe)和syslog(本地和远端)形式的日志以及日志抽样;
- 组合多个CSS、JavaScript文件的访问请求变成一个请求;
- ...............