分享交流
合作共赢!

CentOS6.8 添加nginx到系统服务,并设置开机自启动

1. 简介

nginx源码安装完成后默认不会注册为系统服务,所以需要手工添加系统服务脚本。在/etc/init.d目录下新建nginx文件,并更改权限其即可。

2. 新建nginx文件

vim /etc/init.d/nginx

填写一下内容(根据自己的实际目录修改):

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by caffreyxin at 2007.10.15.
# it is v.0.0.1 version.
# if you find any errors on this scripts, please contact caffreyxin.
# and send mail to xinyflove at sina dot com.
#
# 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=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid

RETVAL=0prog="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" instart)
        start
        ;;

stop)
        stop
        ;;

reload)
        reload
        ;;

restart)
        stop
        start
        ;;

status)
        status $prog
        RETVAL=$?
        ;;*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1esac

exit $RETVAL

根据自己实际安装目录,修改这两行:

nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf

3. 修改文件权限

chmod 755 /etc/init.d/nginx

4. 设置开机启动

chkconfig nginx on

5.查看开机启动的服务

chkconfig --list

附:

启动服务:service nginx start

停止服务:service nginx stop

重启服务:service nginx reload

温馨提示:

linux设置开启启动项参考琼杰笔记文档:1. Linux开机启动项的查看和设置方法总结    2. Centos7设置服务开机自启动方法

此内容查看价格0.1立即购买
赞(0) 打赏
未经允许不得转载:琼杰笔记 » CentOS6.8 添加nginx到系统服务,并设置开机自启动

评论 抢沙发

评论前必须登录!

 

分享交流,合作共赢!

联系我们加入QQ群

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册