20.2.2 配置Nginx服務(wù)

2023-05-11 10:06 更新

Nginx是一款相當(dāng)優(yōu)秀的用于部署動態(tài)網(wǎng)站的輕量級服務(wù)程序,它最初是為俄羅斯門戶站點而開發(fā)的,因其穩(wěn)定性、功能豐富、占用內(nèi)存少且并發(fā)能力強而備受用戶的信賴。目前國內(nèi)諸如新浪、網(wǎng)易、騰訊等門戶站點均已使用了此服務(wù)。

Nginx服務(wù)程序的穩(wěn)定性源自于采用了分階段的資源分配技術(shù),降低了CPU與內(nèi)存的占用率,所以使用Nginx程序部署的動態(tài)網(wǎng)站環(huán)境不僅十分穩(wěn)定、高效,而且消耗的系統(tǒng)資源也很少。此外,Nginx具備的模塊數(shù)量與Apache具備的模塊數(shù)量幾乎相同,而且現(xiàn)在已經(jīng)完全支持proxy、rewrite、mod_fcgi、ssl、vhosts等常用模塊。更重要的是,Nginx還支持熱部署技術(shù),可以7×24不間斷提供服務(wù),還可以在不暫停服務(wù)的情況下直接對Nginx服務(wù)程序進(jìn)行升級。

圖20-2 Nginx與Apache著名LOGO

坦白來講,雖然Nginx程序的代碼質(zhì)量非常高,代碼很規(guī)范,技術(shù)成熟,模塊擴展也很容易,但依然存在不少問題,比如是由俄羅斯人開發(fā)的,所以在資料文檔方面還并不完善,中文資料的質(zhì)量更是魚龍混雜。但是Nginx服務(wù)程序在近年來增長勢頭迅猛,相信會在輕量級Web服務(wù)器市場具有不錯的未來。

在正式安裝Nginx服務(wù)程序之前,我們還需要為其解決相關(guān)的軟件依賴關(guān)系,例如用于提供Perl語言兼容的正則表達(dá)式庫的軟件包pcre,就是Nginx服務(wù)程序用于實現(xiàn)偽靜態(tài)功能必不可少的依賴包。下面來解壓、編譯、生成、安裝Nginx服務(wù)程序的源碼文件:

    [root@linuxprobe ~]# cd /usr/local/src
    [root@linuxprobe src]# tar xzvf pcre-8.35.tar.gz 
    [root@linuxprobe src]# cd pcre-8.35
    [root@linuxprobe pcre-8.35]# ./configure --prefix=/usr/local/pcre
    [root@linuxprobe pcre-8.35]# make
    [root@linuxprobe pcre-8.35]# make install 

openssl軟件包是用于提供網(wǎng)站加密證書服務(wù)的程序文件,在安裝該程序時需要自定義服務(wù)程序的安裝目錄,以便于稍后調(diào)用它們的時候更可控。

    [root@linuxprobe pcre-8.35]# cd /usr/local/src
    [root@linuxprobe src]# tar xzvf openssl-1.0.1h.tar.gz
    [root@linuxprobe src]# cd openssl-1.0.1h
    [root@linuxprobe openssl-1.0.1h]# ./config --prefix=/usr/local/openssl
    [root@linuxprobe openssl-1.0.1h]# make
    [root@linuxprobe openssl-1.0.1h]# make install 

openssl軟件包安裝后默認(rèn)會在/usr/local/openssl/bin目錄中提供很多的可用命令,我們需要像前面的操作那樣,將這個目錄添加到PATH環(huán)境變量中,并寫入到配置文件中,最后執(zhí)行source命令以便讓新的PATH環(huán)境變量內(nèi)容可以立即生效:

    [root@linuxprobe pcre-8.35]# vim /etc/profile
    ………………省略部分輸出信息………………
     64 
     65 for i in /etc/profile.d/*.sh ; do
     66 if [ -r "$i" ]; then
     67 if [ "${-#*i}" != "$-" ]; then
     68 . "$i"
     69 else
     70 . "$i" >/dev/null
     71 fi
     72 fi
     73 done
     74 export PATH=$PATH:/usr/local/mysql/bin:/usr/local/openssl/bin
     75 unset i
     76 unset -f pathmunge
    [root@linuxprobe pcre-8.35]# source /etc/profile

zlib軟件包是用于提供壓縮功能的函數(shù)庫文件。其實Nginx服務(wù)程序調(diào)用的這些服務(wù)程序無需深入了解,只要大致了解其作用就已經(jīng)足夠了:

    [root@linuxprobe pcre-8.35]# cd /usr/local/src
    [root@linuxprobe src]# tar xzvf zlib-1.2.8.tar.gz 
    [root@linuxprobe src]# cd zlib-1.2.8
    [root@linuxprobe zlib-1.2.8]# ./configure --prefix=/usr/local/zlib
    [root@linuxprobe zlib-1.2.8]# make
    [root@linuxprobe zlib-1.2.8]# make install

在安裝部署好具有依賴關(guān)系的軟件包之后,創(chuàng)建一個用于執(zhí)行Nginx服務(wù)程序的賬戶。賬戶名稱可以自定義,但一定別忘記,因為在后續(xù)需要調(diào)用:

    [root@linuxprobe zlib-1.2.8]# cd ..
    [root@linuxprobe src]# useradd www -s /sbin/nologin

在使用命令編譯Nginx服務(wù)程序時,需要設(shè)置特別多的參數(shù),其中,--prefix參數(shù)用于定義服務(wù)程序稍后安裝到的位置,--user與--group參數(shù)用于指定執(zhí)行Nginx服務(wù)程序的用戶名和用戶組。在使用參數(shù)調(diào)用openssl、zlib、pcre軟件包時,請寫出軟件源碼包的解壓路徑,而不是程序的安裝路徑:

    [root@linuxprobe src]# tar xzvf nginx-1.6.0.tar.gz 
    [root@linuxprobe src]# cd nginx-1.6.0/
    [root@linuxprobe nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1h --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.35
    [root@linuxprobe nginx-1.6.0]# make
    [root@linuxprobe nginx-1.6.0]# make install

要想啟動Nginx服務(wù)程序以及將其加入到開機啟動項中,也需要有腳本文件??上У氖?,在安裝完Nginx軟件包之后默認(rèn)并沒有為用戶提供腳本文件,因此劉遄老師給各位讀者準(zhǔn)備了一份可用的啟動腳本文件,大家只需在/etc/rc.d/init.d目錄中創(chuàng)建腳本文件并直接復(fù)制下面的腳本內(nèi)容即可(相信各位讀者在掌握了第4章的內(nèi)容之后,應(yīng)該可以順利看懂這個腳本文件)。

    [root@linuxprobe nginx-1.6.0]# vim /etc/rc.d/init.d/nginx
    #!/bin/bash
    # nginx - this script starts and stops the nginx daemon
    # chkconfig: - 85 15
    # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
    # proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config: /etc/nginx/nginx.conf
    # config: /usr/local/nginx/conf/nginx.conf
    # pidfile: /usr/local/nginx/logs/nginx.pid
    # Source function library.
    . /etc/rc.d/init.d/functions
    # Source networking configuration.
    . /etc/sysconfig/network
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
    nginx="/usr/local/nginx/sbin/nginx"
    prog=$(basename $nginx)
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
    [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
    lockfile=/var/lock/subsys/nginx
    make_dirs() {
    # make required directories
    user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
            if [ -z "`grep $user /etc/passwd`" ]; then
                    useradd -M -s /bin/nologin $user
            fi
    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
    for opt in $options; do
            if [ `echo $opt | grep '.*-temp-path'` ]; then
                    value=`echo $opt | cut -d "=" -f 2`
                    if [ ! -d "$value" ]; then
                            # echo "creating" $value
                            mkdir -p $value && chown -R $user $value
                    fi
            fi
    done
    }
    start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
    }
    stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
    }
    restart() {
    #configtest || return $?
    stop
    sleep 1
    start
    }
    reload() {
    #configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
    }
    force_reload() {
    restart
    }
    configtest() {
    $nginx -t -c $NGINX_CONF_FILE
    }
    rh_status() {
    status $prog
    }
    rh_status_q() {
    rh_status >/dev/null 2>&1
    }
    case "$1" in
    start)
            rh_status_q && exit 0
            $1
            ;;
    stop)
            rh_status_q || exit 0
            $1
            ;;
    restart|configtest)
    $1
    ;;
    reload)
            rh_status_q || exit 7
            $1
            ;;
    force-reload)
            force_reload
            ;;
    status)
            rh_status
            ;;
    condrestart|try-restart)
            rh_status_q || exit 0
            ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
    exit 2
    esac

保存腳本文件后記得為其賦予755權(quán)限,以便能夠執(zhí)行這個腳本。然后以絕對路徑的方式執(zhí)行這個腳本,通過restart參數(shù)重啟Nginx服務(wù)程序,最后再使用chkconfig命令將Nginx服務(wù)程序添加至開機啟動項中。大功告成!

    [root@linuxprobe nginx-1.6.0]# chmod 755 /etc/rc.d/init.d/nginx
    [root@linuxprobe nginx-1.6.0]# /etc/rc.d/init.d/nginx restart
    Restarting nginx (via systemctl):                          [  OK  ]
    [root@linuxprobe nginx-1.6.0]# chkconfig nginx on

Nginx服務(wù)程序在啟動后就可以在瀏覽器中輸入服務(wù)器的IP地址來查看到默認(rèn)網(wǎng)頁了。相較于Apache服務(wù)程序的紅色默認(rèn)頁面,Nginx服務(wù)程序的默認(rèn)頁面顯得更加簡潔,如圖20-2所示。

圖20-3 Nginx服務(wù)程序的默認(rèn)頁面

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號