1. 程式人生 > >liunx7下centos6.8編譯安裝nginx1.12.1

liunx7下centos6.8編譯安裝nginx1.12.1

正式開始前,編譯環境gcc g++ 開發庫之類的需要提前裝好。

如果是ububtu平臺初始安裝編譯安裝則使用如下指令:

apt-get install build-essential

apt-get installlibtool
如果是centos則如下:
當沒有make時:

安裝make:

yum -y install gcc automake autoconf libtool make
 
安裝c++編譯環境

yum -y install gcc-c++

下面正式開始
---------------------------------------------------------------------------
一般我們都需要先裝pcre, zlib,openssl,前者為了重寫rewrite,後者為了gzip壓縮,在後者是為了使用ssl
1,選擇原始碼包所存放的目錄,這裡為/usr/local/src
2,所有依賴的安裝目錄為/usr/local/related

安裝pcre:

cd/usr/local/src

wget https://sourceforge.net/projects/pcre/files/pcre/8.41/pcre-8.41.tar.gz/

tar-zxvfpcre-8.41.tar.gz

cd pcre-8.41

./configure --prefix=/usr/local/related/pcre

make

makeinstall
如果安裝的是bz2 ,提示tar (child): bzip2:無法 exec: 沒有那個檔案或目錄 tar (child): Error is not recoverable: exiting now

缺少bzip2包
yum install -y bzip2

然後

tar -jxf xxx.tar.bz2即可

安裝zlib
cd /usr/local/src

wget http://zlib.net/zlib-1.2.11.tar.gz  --這個玩意更新很快,舊版地址會被官方更新,所以只能開啟官網找最新的下載

tar -zxvf zlib-1.2.8.tar.gz

cdzlib-1.2.8

./configure --prefix=/usr/local/related/zlib

make

make install

安裝openssl
cd /usr/local/src

wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz

tar-zxvf openssl-1.0.1t.tar.gz

config --prefix=/usr/local/related/openssl
make
make install

安裝它的時候或者安裝mysql的依賴包bison 的時候都需要perl的支援所以:
cd /usr/local/src
wget http://www.cpan.org/src/5.0/perl-5.26.0.tar.gz
tar -zxf perl-5.26.0.tar.gz
./configure.gnu --prefix=/usr/local/related/pel
make && make install


最後安裝nginx

cd /usr/local/src

wget http://nginx.org/download/nginx-1.12.1.tar.gz

tar -zxvf nginx-1.12.1.tar.gz

cd nginx-1.12.1

./configure--sbin-path=/usr/local/nginx/nginx\   --設定nginx的執行目錄

--conf-path=/usr/local/nginx/nginx.conf\              --設定nginx的配置目錄

--pid-path=/usr/local/nginx/nginx.pid\                  --設定nginx的pid

--with-http_v2_module\                                     --開啟http2支援

--with-http_ssl_module\                                        --開啟ssl模組

--with-pcre=/usr/local/src/pcre-8.41\                  --pcre的原始碼包所在路徑  --可用新版本

--with-zlib=/usr/local/src/zlib-1.2.8\                      --zlib的原始碼包所在路徑  --可用新版本

--with-openssl=/usr/local/src/openssl-1.0.1t    --openssl的原始碼包所在路徑  --可用新版本

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-http_v2_module --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11 --with-openssl=/usr/local/src/openssl-1.0.2o

make

make install

至此,大功告成
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
如果編譯pcre報如下錯誤:

libtool:compile:unrecognizedoption`-DHAVE_CONFIG_H'

libtool: compile: Try `libtool --help'formoreinformation.

make[1]:***[pcrecpp.lo]Error1

make[1]:Leavingdirectory`/usr/local/src/pcre-8.34'

make:***[all]Error2

那麼需要g++,
yum -y install g++
安裝完畢後重新編譯pcre即可:
make clean
./configure --prefix=/usr/local/related/pcre
make && make install
__________________________________________________________________________________________________________________________

nginx的編譯選項:

make是用來編譯的,它從Makefile中讀取指令,然後編譯。

make install是用來安裝的,它也從Makefile中讀取指令,安裝到指定的位置。

configure命令是用來檢測你的安裝平臺的目標特徵的。它定義了系統的各個方面,包括nginx的被允許使用的連線處理的方法,比如它會檢測你是不是有CC或GCC,並不是需要CC或GCC,它是個shell指令碼,執行結束時,它會建立一個Makefile檔案。nginx的configure命令支援以下引數:

  • --prefix=path    定義一個目錄,存放伺服器上的檔案 ,也就是nginx的安裝目錄。預設使用 /usr/local/nginx。
  • --sbin-path=path 設定nginx的可執行檔案的路徑,預設為  prefix/sbin/nginx.
  • --conf-path=path  設定在nginx.conf配置檔案的路徑。nginx允許使用不同的配置檔案啟動,通過命令列中的-c選項。預設為prefix/conf/nginx.conf.
  • --pid-path=path  設定nginx.pid檔案,將儲存的主程序的程序號。安裝完成後,可以隨時改變的檔名 , 在nginx.conf配置檔案中使用 PID指令。預設情況下,檔名 為prefix/logs/nginx.pid.
  • --error-log-path=path 設定主錯誤,警告,和診斷檔案的名稱。安裝完成後,可以隨時改變的檔名 ,在nginx.conf配置檔案中 使用 的error_log指令。預設情況下,檔名 為prefix/logs/error.log.
  • --http-log-path=path  設定主請求的HTTP伺服器的日誌檔案的名稱。安裝完成後,可以隨時改變的檔名 ,在nginx.conf配置檔案中 使用 的access_log指令。預設情況下,檔名 為prefix/logs/access.log.
  • --user=name  設定nginx工作程序的使用者。安裝完成後,可以隨時更改的名稱在nginx.conf配置檔案中 使用的 user指令。預設的使用者名稱是nobody。
  • --group=name  設定nginx工作程序的使用者組。安裝完成後,可以隨時更改的名稱在nginx.conf配置檔案中 使用的 user指令。預設的為非特權使用者。
  • --with-select_module --without-select_module 啟用或禁用構建一個模組來允許伺服器使用select()方法。該模組將自動建立,如果平臺不支援的kqueue,epoll,rtsig或/dev/poll。
  • --with-poll_module --without-poll_module 啟用或禁用構建一個模組來允許伺服器使用poll()方法。該模組將自動建立,如果平臺不支援的kqueue,epoll,rtsig或/dev/poll。
  • --without-http_gzip_module — 不編譯壓縮的HTTP伺服器的響應模組。編譯並執行此模組需要zlib庫。
  • --without-http_rewrite_module  不編譯重寫模組。編譯並執行此模組需要PCRE庫支援。
  • --without-http_proxy_module — 不編譯http_proxy模組。
  • --with-http_ssl_module — 使用https協議模組。預設情況下,該模組沒有被構建。建立並執行此模組的OpenSSL庫是必需的。
  • --with-pcre=path — 設定PCRE庫的原始碼路徑。PCRE庫的原始碼(版本4.4 - 8.30)需要從PCRE網站下載並解壓。其餘的工作是Nginx的./ configure和make來完成。正則表示式使用在location指令和 ngx_http_rewrite_module 模組中。
  • --with-pcre-jit —編譯PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。
  • --with-zlib=path —設定的zlib庫的原始碼路徑。要下載從 zlib(版本1.1.3 - 1.2.5)的並解壓。其餘的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模組需要使用zlib 。
  • --with-cc-opt=parameters — 設定額外的引數將被新增到CFLAGS變數。例如,當你在FreeBSD上使用PCRE庫時需要使用:--with-cc-opt="-I /usr/local/include。.如需要需要增加 select()支援的檔案數量:--with-cc-opt="-D FD_SETSIZE=2048".
  • --with-ld-opt=parameters —設定附加的引數,將用於在連結期間。例如,當在FreeBSD下使用該系統的PCRE庫,應指定:--with-ld-opt="-L /usr/local/lib".

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

最後設定nginx的開機自啟動:

在/etc/init.d/ 下新建nginx檔案並寫入以下程式碼:


#!/bin/sh
#
# 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:      /etc/sysconfig/nginx
#pidfile:     /usr/local/nginx/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/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/usr/local/nginx/lock/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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
}
#!/bin/sh
#

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

注意修改#pidfile,nginx,NGINX_CONF_FILE這三個所指定的路徑為自己安裝nginx時指定的,還要注意lockfile所指定的lock目錄是否存在

寫好後新增可執行許可權

chmod a+x /etc/init.d/nginx

然後使用chkconfig --add nginx 來將nginx新增到系統管理中

新增完畢可使用chkconfig --list 來檢視是否add成功

最後使用chkconfig nginx on 來設定開機啟動

最後配置nginx.conf即可,請參閱下一篇博文