服務器均為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文件的訪問請求變成一個請求;
- ...............