CentOS7.3編譯安裝Nginx設置開機啟動


起因

最近想玩nginx了,本來用yum -y install nginx安裝也啟動好了,但是買了本《Nginx高性能Web服務器詳解》,我咋能辜負我的書費呢?於是我就直接ps -ef |grep nginx kill -QUIT master的pid,然后yum -y remove nginx了。沒錯,就是這么帥。

經過

下載nginx

當然是去nginx(http://nginx.org/en/download.html)主頁了,沒錯我現在安裝的就是穩定版1.14.2了。
進入放置nginx的目錄,我是/usr/local/tools/nginx
輸入命令: wget http://nginx.org/download/nginx-1.14.2.tar.gz,這兒就等着吧。

解壓

接下來解壓targz包,你要是喜歡看動畫呢,就輸入 tar -zxvf nginx-1.14.2.tar.gz ,你要是不看呢就輸入tar xf nginx-1.14.2.tar.gz

安裝nginx

安裝必要的工具:yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel gd-devel perl-devel perl-ExtUtils-Embed .
接下來未來保證源文件和二進制文件不重復,我進行了一波騷操作: mv nginx-1.14.2 nginx-1.14.2-installer mkdir nginx-1.14.2就是把源碼文件夾重命名,新建了一個nginx按照目錄,現在的源碼在/usr/local/tools/nginx\nginx-1.14.2-installer,我要安裝在/usr/local/tools/nginx/nginx-1.14.2目錄里。
進入源碼文件: cd nginx-1.14.2-installer,
編譯文件:./configure --prefix=/usr/local/tools/nginx/nginx-1.14.2,注意這兒改成你的目錄就行了,有兩點你的注意。其一、你的nginx.lock位置是在/var/lock/nginx.lock;其二、只安裝了標准模塊;其三、用戶不限制,任何人都能啟動nginx
我的完整的配置選項是這樣子的(如果你也是用的修改的配置,一定要把這個配置信息記下來,否則以后查錯,該配置你會哭的。):/configure --prefix=安裝目錄 --sbin-path=sbin/nginx --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --error-log-path=logs/error.log --http-log-path=logs/access.log --lock-path=logs/lock/subsys/nginx --user=你要啟動nginx的用戶 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-ld-opt="-Wl,-E" --with-http_image_filter_module--prefix是安裝目錄,一定要使用絕對路徑;我把lock文件也放在了安裝目錄下的logs下;啟用了幾個基本模塊
一定要注意這時候是不是報錯了。
然后就是 make && make install

測試安裝成功

進入安裝目錄:cd ../nginx-1.14.2,
執行啟動: ./sbin/nginx,如果啟動報錯的話,看下錯誤信息,一般情況下是啥也沒有。
查看服務: ps -ef |grep nginx

停止服務:kill -QUIT 7555,這個7555對應的是上圖master process的進程編號,為了后續啟動這兒一定要停了服務
查看服務: ps -ef |grep nginx

配置系統服務

配置系統服務:vim /etc/init.d/nginx
將下面這段復制進去(這是官網提供的加入系統服務腳本,鏈接 Red Hat NGINX Init Script ,找到nginx="/usr/local/tools/nginx/nginx-1.14.2/sbin/nginx" NGINX_CONF_FILE="/usr/local/tools/nginx/nginx-1.14.2/conf/nginx.conf"這兩句改成你自己的目錄):

#!/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:      /usr/local/tools/nginx/nginx-1.14.2/conf/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /usr/local/tools/nginx/nginx-1.14.2/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/tools/nginx/nginx-1.14.2/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/tools/nginx/nginx-1.14.2/conf/nginx.conf"

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

lockfile=/usr/local/tools/nginx/nginx-1.14.2/logs/lock/subsys/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
}

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

配置啟動腳本權限:chmod a+x /etc/init.d/nginx
啟動nginx: /etc/init.d/nginx start
停止nginx: /etc/init.d/nginx stop

加入系統服務:chkconfig --add /etc/init.d/nginx
使用systemctl啟動nginx: systemctl start nginx
使用systemctl停止nginx: systemctl stop nginx

開機啟動

配置開機啟動:vi /etc/rc.local在最后加一句/etc/init.d/nginx start

結尾

打完收功!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM