栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux(CentOS)下设置Nginx开机自动启动和chkconfig管理

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux(CentOS)下设置Nginx开机自动启动和chkconfig管理

设置nginx开机自动启动

1、首先,在Linux系统的 /etc/init.d/目录下创建nginx文件,命令如下:

vim /etc/init.d/nginx

2、编辑改nginx文件,填入如下内容

该配置内容来源于Nginx官网:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

注意: 下面的 nginx 和 NGINX_CONF_FILE 需要替换成你环境的路径

#!/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="/usr/sbin/nginx" 
prog=$(basename $nginx)

# 修改成你环境中[配置文件]^(Profile)的路径。
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 $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、进行授权操作,保存脚本后设置文件的执行权限

chmod a+x /etc/init.d/nginx # a表示所有,+表示添加,x表示可执行。

然后,可以即可以通过该脚本对nginx服务进行管理了

/etc/init.d/nginx start #启动
/etc/init.d/nginx stop #停止

4、使用chkconfig进行管理

上面的方法完成了用脚本管理Nginx服务的功能,但是还是不太方便,比如要设置Nginx开机启动等。这时可以使用chkconfig来设置。

先将nginx服务加入chkconfig管理列表:

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

加完这个之后,就可以使用service对Nginx进行启动,重启等操作了

service nginx start
service nginx stop

5、设置终端模式开机启动

chkconfig nginx on

查看chkconfig配置:

chkconfig  # 查看所有服务配置
chkconfig --list 服务名  # 查看指定服务配置
chkconfig --list nginx

6、最后重启测试,查看开机终端后Nginx是否自动开启

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/333648.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号