超級詳細的centos6.8下安裝nginx教程

centos6.8下安裝nginx

centos6.8 安裝nginx,沒ubuntu這麼方便,
雖然同是linux系統,且本質原理都是一的,centos6.8下安裝需要編譯nginx源碼

下面開始安裝

使用yum依賴

yum安裝很方便(類似ubuntu下的 apt),不用yum的話,那幾個依賴都需要源碼編譯安裝

yum install -y gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

ps 贈送一篇依賴源碼編譯安裝的教程 https://www.cnblogs.com/javage/p/10654104.html

下載 nginx源碼
cd ~
wget http://nginx.org/download/nginx-1.14.2.tar.gz
開始安裝nginx
tar nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre
make && make install

nginx則安裝到 /usr/local/nginx目錄下,查看一下該目錄

ls /usr/local/nginx
conf  html  logs  sbin

ps 這安裝方法很原生,雖然不方便,但是對理解nginx的配置文件組成很有幫助。
安裝之後,是沒有/etc/nginx 的,使用service nginx start 也是不管用的,都需要自己處理,見下文配置自動啓動

手動啓動nginx
cd /usr/local/nginx/sbin
./nginx

關閉 ./nginx -s stop
重啓 ./nginx -s reload

默認會從nginx/conf下讀取nginx.conf文件

如果需要制定conf文件,可使用nginx -c ~/youSite.conf

修改配置文件後重新加載nginx前,需最好檢測一下配置文件,可使用命令nginx -t -c ~/youSite.conf

配置nginx開機自動啓動

step.1 複製nginx.conf文件到/etc/nginx/

cd /nginx/conf
cp nginx.conf /etc/nginx/

ps: /etc下主要都是配置文件,放這裏方便,好找
注意: nginx.conf中可能還用到了其他包含文件,需要一起拷貝過去
如:mime.types

step.2 創建init.d控制命令

vi /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:     /var/run/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/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

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

lockfile=/var/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 $prog -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

step.3 添加完成後,需要注意修改兩出

nginx="/usr/local/nginx/sbin/nginx" #修改成nginx執行程序的路徑。
NGINX_CONF_FILE="/etc/nginx/nginx.conf" #修改成nginx.conf文件的路徑。

step.4 設置權限

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

完成後,就可以愉快的使用下面熟悉的操作命令了

/etc/init.d/nginx start 
/etc/init.d/nginx stop
/etc/init.d/nginx restart

step.5 添加到service,使用service對nginx控制

chkconfig --add /etc/init.d/nginx

於是,就可以愉快的用service對nginx進行控制了

step.6 最後一步,設置開機自啓動

chkconfig nginx on

到這裏結束

chkconfig 命令用於檢查,設置系統的各種服務。相關命令結束看這裏

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章