1. 程式人生 > >Linux(九)Linux下安裝NGINX

Linux(九)Linux下安裝NGINX

1.安裝環境

    nginx是C語言開發,建議在linux上執行,本教程使用Centos6.5作為安裝環境。

2.NGINX依賴庫

    2.1gcc

        安裝nginx需要先將官網下載的原始碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc

        命令:yum install gcc-c++ 

    2.2PCRE

         PCRE(PerlCompatible Regular Expressions)是一個Perl庫,包括 perl 相容的正則表示式庫。nginx的http模組使用pcre來解析正則表示式,所以需要在linux上安裝pcre庫。

命令:yum install -y pcrepcre-devel

    2.3zlib

         zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。

命令:yum install -y zlibzlib-devel
    2.4openssl

         OpenSSL是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的金鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程式供測試或其它目的使用。

         nginx不僅支援http協議,還支援https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。

命令:yuminstall -y openssl openssl-devel

3.下載nginx

    官方下載:http://nginx.org/en/download.html

    

4.編譯安裝    

    將nginx-1.8.0.tar.gz拷貝至linux伺服器。

    解壓:tar -zxvf nginx-1.8.0.tar.gz

    cd nginx-1.8.0

    4.1configure 

        ./configure --help查詢詳細引數

引數設定如下:
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注意:上邊將臨時檔案目錄指定為/var/temp/nginx,需要在/var下建立tempnginx目錄 

  4.2 編譯安裝

    make

    make install

    安裝成功檢視安裝目錄 :

    

5.啟動nginx

 cd /usr/local/nginx/sbin/

命令:./nginx

查詢nginx程序:命令:ps aux|grep nginx

    

 4429是nginx主程序的程序id,4430是nginx工作程序的程序id    

 注意:執行./nginx啟動nginx,這裡可以-c指定載入的nginx配置檔案,如下:

 ./nginx-c /usr/local/nginx/conf/nginx.conf

 如果不指定-c,nginx在啟動時預設載入conf/nginx.conf檔案,此檔案的地址也可以在編譯安裝nginx時指定./configure的引數(--conf-path= 指向配置檔案(nginx.conf))

6.停止nginx

    方式1,快速停止:

    cd /usr/local/nginx/sbin

    ./nginx -s stop

    此方式相當於先查出nginx程序id再使用kill命令強制殺掉程序。 

    方式2,完整停止(建議使用):

    cd /usr/local/nginx/sbin

    ./nginx -s quit

    此方式停止步驟是待nginx程序處理任務完畢進行停止。

7.重啟nginx

    方式1,先停止再啟動(建議使用):

    對nginx進行重啟相當於先停止nginx再啟動nginx,即先執行停止命令再執行啟動命令。

    如下:

    ./nginx -s quit

    ./nginx

    方式2,重新載入配置檔案:

    當nginx的配置檔案nginx.conf修改後,要想讓配置生效需要重啟nginx,使用-s reload不用先停止nginx再啟動nginx即可將配置資訊在nginx中生效,如下:

    ./nginx -s reload

6.測試

    nginx安裝成功,啟動nginx,即可訪問虛擬機器上的nginx:

    

7.開機自啟動

    7.1編寫shell指令碼 

    這裡使用的是編寫shell指令碼的方式來處理

    vi /etc/init.d/nginx  (輸入下面的程式碼)

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# 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=/var/run/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 "nginx 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 /var/run/nginx.pid
}
# reload nginx service functions.
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

:wq  儲存並退出

    7.2設定檔案訪問許可權   

    chmod a+x /etc/init.d/nginx   (a+x ==> all user can execute  所有使用者可執行) 

    這樣在控制檯就很容易的操作nginx了:檢視Nginx當前狀態、啟動Nginx、停止Nginx、重啟Nginx…

    

    如果修改了nginx的配置檔案nginx.conf,也可以使用上面的命令重新載入新的配置檔案並執行,可以將此命令加入到rc.local檔案中,這樣開機的時候nginx就預設啟動了

    7.3加入到rc.local檔案中

        vi /etc/rc.local

        加入一行  /etc/init.d/nginx start    儲存並退出,下次重啟會生效。