1. 程式人生 > >Nginx伺服器新增sysyemctl和chkconfig管理

Nginx伺服器新增sysyemctl和chkconfig管理

將nginx伺服器安裝好後,每次啟動,停止nginx服務均要進入 安裝目錄sbin下進行操作,為了簡化步驟和增加開機自啟動,所以可以將其加入系統服務行列,以便於使用systemctl和chkconfig進行管理。

備註:下面文章中存在的“//”及後面的問你為備忘記錄,在實際應用前請刪除。

方法一:使用systemctl進行管理

1.建立nginx.service

vim /usr/lib/systemd/system/nginx.service    //此處位置為固定位置,無需改動

2.編輯如下內容

[Unit]

Description=nginx - high performance web server

Documentation=http://nginx.org/en/docs/

After=network.target remote-fs.target nss-lookup.target



[Service]

Type=forking

WorkingDirectory=/usr/local/nginx //此處位置為你的nginx安裝目錄,根據實際情況進行更改

ExecStart=/usr/local/nginx/sbin/nginx //此處位置為你的nginx安裝目錄中的nginx可執行檔案位置,根據實際位置進行更改

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true


[Install]

WantedBy=multi-user.target

3.使檔案生效

systemctl daemon-reload

4.啟動nginx

systemctl start nginx

5.關閉nginx

systemctl stop nginx

6.重啟nginx

systemctl restart nginx

7.開機啟動

systemctl enable nginx

8.問題請看最下備註

方法二:使用chkconfig進行管理

1.建立/etc/init.d/nginx檔案

touch /etc/init.d/nginx

2.為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"               //這裡需要更改為你的安裝位置的nginx可執行檔案的所在位置
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/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

3.更改/etc/init.d/nginx許可權

chmod a+x nginx

4.新增nginx服務為系統服務

chkconfif --add nginx

新增成功後就可以執行service nginx start、stop、restart、status等操作了。

5.問題請看下面的備註

備註:

當上面任意一種操作完成後,執行nginx服務的啟動,停止等可能會出現一下報錯:

錯誤1:

執行:systemctl status nginx.service 報錯如下:

● nginx.service - nginx - high performance web server

Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)

Active: failed (Result: exit-code) since Wed 2018-09-19 10:06:18 CST; 4min 47s ago

Docs: http://nginx.org/en/docs

Sep 19 10:06:17 lt nginx[32486]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)

Sep 19 10:06:17 lt nginx[32486]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

Sep 19 10:06:17 lt nginx[32486]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)

Sep 19 10:06:18 lt nginx[32486]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

Sep 19 10:06:18 lt nginx[32486]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)

Sep 19 10:06:18 lt systemd[1]: nginx.service: control process exited, code=exited status=1

Sep 19 10:06:18 lt nginx[32486]: nginx: [emerg] still could not bind()

Sep 19 10:06:18 lt systemd[1]: Failed to start nginx.service.

Sep 19 10:06:18 lt systemd[1]: Unit nginx.service entered failed state.

Sep 19 10:06:18 lt systemd[1]: nginx.service failed.

方案:

如果出現這種類似的錯誤,原因在於你的nginx伺服器已經通過 /sbin/nginx 進行了啟動,如果需要通過service nginx start....和 sytemctl statrt nginx.service 進行操作,需要你先通過手動/sbin/nginx -s stop進行停止,這時候 service nginx start....和 sytemctl statrt nginx.service 就可以操作nginx服務了。