将Nginx添加到systemctl和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服务为系统服务

chkconfig --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服务了。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值