1. 程式人生 > >ubuntu 14.04編譯安裝nginx 1.14.2

ubuntu 14.04編譯安裝nginx 1.14.2

環境:Ubuntu 14.04+nginx 1.14.1+php

近期,nginx 1.14.1報了漏洞需要將nginx版本升級到1.14.2。apt-get支援的最新版就是1.14.1,所以需要編譯安裝1.14.2。

一、儲存nginx 1.14.1的配置檔案並解除安裝

        1、將/etc/nginx 備份到其他目錄

        2、檢視已安裝的nginx包:dpkg --get-selections|grep nginx

        3、解除安裝nginx:sudo apt-get --purge remove nginx & sudo apt-get --purge remove nginx-full & sudo apt-get --purge remove nginx-common

二、編譯安裝nginx 1.14.2

        1、下載nginx 1.14.2原始碼

        http://nginx.org/download/nginx-1.14.2.tar.gz

        2、解壓壓縮包並編譯

         ./configure --prefix=/etc/nginx

        make

        sudo make install

三、配置nginx

        1、將備份的nginx配置檔案 sites-enable 和 sites-available 複製到nginx1.14.2安裝目錄 /etc/nginx下

        2、修改/etc/nginx/conf/nginx.conf,檔案尾大括號前 新增行 include /etc/nginx/sites-enabled/*;

              

四、將nginx加入service命令

        1、sudo 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/bin/nginx
nginx_config=/etc/nginx/conf/nginx.conf
nginx_pid=/opt/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"

# Check that networking is up.
[ -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: "
   sudo $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ]
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
  echo -n $"Stopping $prog: "
  sudo $nginxd -s stop
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginx_pid
}
# reload nginx service functions.
reload() {
  echo -n $"Reloading $prog: "
  sudo kill -HUP `cat ${nginx_pid}`
  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

        2、sudo chmod a+x /etc/init.d/nginx & sudo update-rc.d nginx defaults

        3、建立nginx執行目錄,賦權:

              sudo mkdir -p /usr/local/nginx/logs

              sudo chmod 777 /usr/local/nginx/logs

              sudo chmod -R a+rw /usr/local/nginx

        4、nginx加入自啟動,sudo vi /etc/rc.local 加入行: /etc/init.d/nginx start

          

        5、啟動nginx
             sudo service nginx start