linux安裝Nginx並開機自啓

記錄一次在阿里雲服務器上安裝Nginx的過程,以及遇到的一些坑。這裏使用的是源碼安裝,建議不要使用yun install命令,這樣就沒辦法定製化安裝Nginx。


安裝環境

系統版本 :CentOS 6.9 x64位 

需求庫:pcre,zlib

安裝流程

1、先到nginx開源官網(http://nginx.org/en/download.html)下載nginx的安裝包,並上傳到服務中,並解壓。

tar -zxvf /opt/nginx/nginx-1.16.1.tar.gz -C /opt/nginx/
下載頁面
下載頁面
解壓後頁面

2、進入Nginx目錄,並使用(./configure  --prefix=/etc/nginx )命令將make需要的一些文件生成並存放到指定的目錄(/etc/nginx)中(如果要支持ssl,在後面添加--with-http_ssl_module)。

可能出現的問題:

①:報./configure: error: C compiler cc is not found錯誤

解決方法:使用(yum -y install gcc gcc-c++ autoconf automake make )安裝gcc庫

②:報./configure: error: the HTTP rewrite module requires the PCRE library.錯誤。

解決方法:使用(yum install pcre pcre-devel -y)安裝pcre庫。

③:報./configure: error: the HTTP gzip module requires the zlib library.錯誤。

解決方法:使用(yum install zlib-devel)安裝zlib庫

④:報./configure: error: SSL modules require the OpenSSL library。

解決方法:使用(yum -y install openssl openssl-devel)安裝openssl庫

編譯成功界面,會顯示Nginx運行時的特性和配置文件目錄:

3、執行make編譯,編譯成功後會生成大量的編譯文件和二進制文件:

4、使用make install安裝Nginx,安裝完成會在我們最開始prefix指定的目錄中生成對應的文件:

5、使用(/etc/nginx/sbin/nginx -c /etc/nginx/conf/nginx.conf)命令啓動nginx。在瀏覽器中輸入ip,如果啓動成功,則會顯示nginx歡迎界面。

nginx其他命令:

修改配置文件後熱部署:nginx -s reload

立刻停止服務:nginx -s stop

優雅的停止服務:nginx -s quit

6、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:     /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的文件位置
nginx="/etc/nginx/sbin/nginx"
prog=$(basename $nginx)
#nginx的配置文件位置
NGINX_CONF_FILE="/etc/nginx/conf/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 $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

②設置文件nginx的權限,讓linux可以執行

chmod +x /etc/init.d/nginx

 ③開啓服務自服務

chkconfig nginx on

 

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