linux nginx 注册服务

Posted 久天.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux nginx 注册服务相关的知识,希望对你有一定的参考价值。

一、创建服务脚本

vim /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

:set ff=unix

nginx="/usr/local/nginx/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

二、添加服务

chkconfig --add nginx

三、测试

service nginx start
service nginx stop
service nginx restart
service nginx reload

 若出现

env: /etc/init.d/nginx: Permission denied

出现这种情况说明了nginx的权限不够被拒绝,因此要修改权限,操作如下:

chmod a+x nginx

以上是关于linux nginx 注册服务的主要内容,如果未能解决你的问题,请参考以下文章

注册linux系统服务

源码安装的nginx注册到系统服务

linux学习:Nginx--常见功能配置片段与优化-06

将绿色版Tomcat/Nginx注册成服务,实现开机自启动

如何将Nginx注册为系统服务,开机自启动。

Linux服务器 离线安装 nginx-1.14.0,让你一路畅通无阻,3分钟就完成。