1. 程式人生 > >linux 下 nginx 服務安裝及配置,開機自動啟動

linux 下 nginx 服務安裝及配置,開機自動啟動

最近經常需要安裝linux伺服器,經過網上查詢整理資料,以備後用。

模組依賴性Nginx需要依賴下面3個包

1. gzip 模組需要 zlib 庫 ( 下載: http://www.zlib.net/ )

2. rewrite 模組需要 pcre 庫 ( 下載: http://www.pcre.org/ )

3. ssl 功能需要 openssl 庫 ( 下載: 

http://www.openssl.org/ )

Nginx包下載: http://nginx.org/en/download.html

依賴包安裝順序依次為:openssl、zlib、pcre, 然後安裝Nginx包.

第一步: 下載安裝所需包

openssl-fips-2.0.2.tar.gz

zlib-1.2.7.tar.gz

pcre-8.21.tar.gz

nginx-1.2.6.tar.gz

第二步:依次安裝openssl-fips-2.0.2.tar.gz, zlib-1.2.7.tar.gz, pcre-8.21.tar.gz, nginx-1.2.6.tar.gz

1.安裝openssl-fips-2.0.2.tar.gz

[[email protected] mrms]# tar -zxvf openssl-fips-2.0.2.tar.gz

 

[[email protected] mrms]# cd openssl-fips-2.0.2

 

[[email protected] openssl-fips-2.0.2]# ./config

 

[[email protected] openssl-fips-2.0.2]# make

 

[[email protected]

openssl-fips-2.0.2]# make install

2.安裝zlib-1.2.7.tar.gz

[[email protected] mrms]# tar -zxvf zlib-1.2.7.tar.gz

 

[[email protected] mrms]# cd zlib-1.2.7

 

[[email protected] zlib-1.2.7]# ./configure

 

[[email protected] zlib-1.2.7]# make

 

[[email protected] zlib-1.2.7]# make install

3.安裝pcre-8.21.tar.gz

[[email protected] mrms]# tar -zxvf pcre-8.21.tar.gz

 

[[email protected] mrms]# cd pcre-8.21

 

[[email protected] pcre-8.21]# ./configure

 

[[email protected] pcre-8.21]# make

 

[[email protected] pcre-8.21]# make install

 4.安裝 nginx-1.2.6.tar.gz

[[email protected] mrms]# tar -zxvf nginx-1.2.6.tar.gz

 

[[email protected] mrms]# cd nginx-1.2.

[[email protected] nginx-1.2.6]# ./configure --with-pcre=../pcre-8.21 --with-zlib=../zlib-1.2.7 --with-openssl=../openssl-fips-2.0.2

我主要使用上面的命令,進行簡單安裝。根據載入不同 下面四條命令同上面,列出來更參考,主要是啟用的功能不同,同時你還要有相應的元件哦,我目前只用到pcre,zlib,openssl 這三個。

./configure --with-http_stub_status_module --with-http_realip_module  --with-pcre=../pcre-8.38 --add-module=../ngx_cache_purge-2.3 --prefix=/usr/local/nginx --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-fips-2.0.13   --add-module=../nginx_upstream_check_module-master/

./configure --with-pcre=../pcre-8.39 --add-module=../ngx_cache_purge-2.1 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-fips-2.0.13

  ./configure --with-pcre=../pcre-8.39 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-fips-2.0.13

 ./configure --with-pcre=../pcre-8.39 --add-module=../ngx_cache_purge-2.3 --prefix=/usr/local/nginx --with-http_stub_status_module  --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-fips-2.0.13

[[email protected] nginx-1.2.6]# make

[[email protected] nginx-1.2.6]# make install

至此Nginx的安裝完成!

第三步:檢測是否安裝成功

[[email protected] nginx-1.2.6]# cd  /usr/local/nginx/sbin

[[email protected] sbin]# ./nginx –t

出現如下所示提示,表示安裝成功

 

啟動nginx

[[email protected] sbin]# ./nginx

檢視埠

[[email protected] sbin]# netstat -ntlp

結果如下

第四步:設定開機自動啟動

首先,在linux系統的/etc/init.d/目錄下建立nginx檔案,使用如下命令:

vi /etc/init.d/nginx

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

 

注意修改其中相關個性化路徑設定,調整成自己的相應的路徑:
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid

接著,設定檔案的訪問許可權:

chmod a+x /etc/init.d/nginx    ;(a+x引數表示 ==> all user can execute  所有使用者可執行)
 最後將ngix加入到rc.local檔案中,這樣開機的時候nginx就預設啟動了
vi /etc/rc.local

在指令碼後面新增:

/etc/init.d/nginx start  

儲存,退出,可以重啟測試了。