nginx開機自動啓動服務

生產服務器:

OS:CentOS 6.8

1.在該目錄下”/etc/init.d”新建一個nginx文件,添加下列代碼。

[root@localhost init.d]# vi  nginx

[root@localhost init.d]# cat nginx

#nginx Startup script for the Nginx HTTP Server

# it is v.0.0.2 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

# It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=/application/nginx/sbin/nginx

nginx_config=/application/nginx/conf/nginx.conf

nginx_pid=/var/run/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

./etc/rc.d/init.d/functions

# Source networking configuration.

./etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi

   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

   return $RETVAL

}

# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        killproc $nginxd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

}

# reload nginx service functions.

reload() {

    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo

}

# See how we were called.

case "$1" in

start)

        start

        ;;

stop)

        stop

        ;;

reload)

        reload

        ;;

restart)

        stop

        start

        ;;

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1

esac

exit $RETVAL

2.設置開機啓動服務,保存好 /etc/init.d/nginx文件後,執行以下命令:

[root@localhost init.d]# chmod 755 /etc/init.d/nginx #賦予文件執行權限

[root@localhost init.d]# chkconfig --add nginx

[root@localhost init.d]# chkconfig  --level 2345  nginx  on #設置開機啓動

3、nginx啓動、停止、無間斷服務重啓

[root@localhost /]# service nginx status

nginx (pid 1621 1619) 正在運行...

[root@localhost /]# service nginx stop

停止 nginx:                                               [確定]

[root@localhost /]# service nginx start

正在啓動 nginx:                                           [確定]

[root@localhost /]# service nginx reload

重新載入 nginx:                                           [確定]

 

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