centos系統LNMP環境配置(三) Nginx編譯安裝

安裝前提
1、安裝必備軟件

yum -y install zlib zlib-devel openssl openssl-devel

2、安裝pcre 支持nginx僞靜態(若已安裝直接跳過)

cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
mkdir /usr/local/pcre                  // 創建安裝目錄
tar  zxvf pcre-8.30.tar.gz
cd pcre-8.30
./configure  --prefix=/usr/local/pcre  // 配置
make & make install

開始安裝Nginx
1、#添加www組和用戶,設置不允許www用戶直接登錄系統

[root@admin local]# groupadd  www  
[root@admin local]# useradd -g  www www -s /bin/false 

2、下載源碼

 [root@admin local]# cd /usr/local/src 
 [root@admin local]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
 [root@admin local]# tar -zxvf nginx-1.6.2.tar.gz
 [root@admin local]# cd nginx-1.6.2/

3、創建安裝路徑

[root@admin local]# mkdir /usr/local/nginx

4、安裝

[root@admin nginx]# ./configure --prefix=/usr/local/nginx
[root@admin nginx]# make & make install

Nginx啓動腳本

#!/bin/bash
# nginx This shell script takes care of starting and stopping
# nginx
#
# chkconfig: - 13 68
# description: nginx is a web server
### BEGIN INIT INFO
# Provides: $named
# Short-Description: start|stop|status|restart|configtest
### END INIT INFO
#variables
NGINX_BIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
NETSTAT="/bin/netstat"
alter=$1
prog=nginx
#load system function
. /etc/rc.d/init.d/functions
#function:echo ok or error
function if_no {
    if [ $2 == 0 ]; then
        echo -n $"$1 ${prog}:" && success && echo
    else
        echo -n $"$1 ${prog}:" && failure && echo
    fi
}
#start nginx
function start {
    rm -f ${NGINX_PID} 2>/dev/null
    if [ -s ${NGINX_PID} ]; then
        echo "nginx already running"
    else
        if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
            rm -f ${NGINX_PID} 2>/dev/null
            ${NGINX_BIN} -c ${NGINX_CONF}
            if_no start $?
        else
            ${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1 > ${NGINX_PID}
            if_no start $?
        fi
    fi
}
#stp nginx
function stop {
    if [ -s ${NGINX_PID} ]; then
        cat ${NGINX_PID} | xargs kill -QUIT
        if_no stop $?
    else
        if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
            rm -f ${NGINX_PID} 2>/dev/null
            if_no stop 0
        else
            rm -f ${NGINX_PID} 2>/dev/null
            kill `${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1`
            if_no stop $?
        fi
    fi
}
function restart {
    if [ -s ${NGINX_PID} ]; then
        cat ${NGINX_PID} | xargs kill -HUP
        if_no restart $?
    else
        stop
        sleep 1
        start
    fi
}
function status {
    ${NETSTAT} -tnpl | grep nginx | grep LISTEN
    [ $? == 0 ] && echo "nginx is running" || echo "nginx is not running"
}
function configtest {
    ${NGINX_BIN} -t
}
case $alter in
start)
    start
;;
stop)
    stop
;;
restart)
    restart
;;
status)
    status
;;
configtest)
    configtest
;;
*)
    echo "use:${NGINX} {start|stop|restart|status|configtest}"
;;
esac

配置Nginx自啓動腳本

1、將啓動腳本保存至/etc/init.d/nginx 文件中
2、設置腳本啓動權限

[root@admin nginx]# chmod +x /etc/init.d/nginx

3、執行nginx命令

/etc/init.d/nginx start 或 service nginx start   //啓動
/etc/init.d/nginx stop 或 service nginx stop    //關閉nginx
/etc/init.d/nginx restart 或 service nginx restart  //重啓nginx

配置開機自動啓動

chkconfig --add nginx
chkconfig --level 2345 nginx on
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章