`
youjin
  • 浏览: 21887 次
  • 来自: ...
社区版块
存档分类
最新评论

Ubuntu Nginx 开机启动设置

阅读更多
转载:http://jinbaoshare.com/archives/522.html
Ubuntu开机自动启动Nginx
如果您的nginx需要开机启动服务,则应在/etc/init.d/nginx文件中添加nginx开机启动脚本,以下是脚本内容,红色字体需要您修改(修改为您的对应文件的path,找对对应的文件,才有接下来的戏):
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0/usr/local/nginx/nginx
# 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/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"

# Check that networking is up.
[ -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: "
   $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ]
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        $nginxd -s stop
        RETVAL=$?
        echo
        [ $RETVAL = 0 ]
}
# 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}"
        exit 1
esac
exit $RETVAL
保存好/etc/init.d/nginx文件后,执行命令
update-rc.d –f nginx defaults
这样我们就为Ubuntu设置了开机自动启动nginx.
update-rc.d命令,是用来自动的升级System V类型初始化脚本,简单的讲就是,哪些东西是你想要系统在引导初始化的时候运行的,哪些是希望在关机或重启时停止的,可以用它来帮你设置。
如果想取消Nginx开机自动启动,执行命令
update-rc.d -f nginx remove
/etc/init.d/里存放了所有的启动脚本,我们可以通过/etc/init.d/服务名(脚本) 启动模式 来启动,停止或重新启动某个应用。 例如输入命令
/etc/init.d/nginx start 命令启动nginx
/etc/init.d/nginx stop 命令停止nginx
/etc/init.d/nginx restart 命令重启nginx
如果是在RedHat系统下,还可以通过service 服务名 start来启动某个服务。
尽管/etc/init.d目录中的脚本可以启动和停止各个服务,但在系统引导时,init并不是直接在/etc/init.d目录下找各个服务的启动脚本,而是在/etc/rc.d/目录下查找,该目录包含rc0.d、rc1.d等分别代表不同的init启动级别的子目录。
你可以用runlevel命令查看当前你的系统是在那个运行级
#runlevel N 2 注:我当前的系统是2级
接着来看看这些rcx.d的目录下所包含的内容,内容全是一些符号链接,链接到上一级init目录中的脚本上。 这些符号链接名称都以S或K开头,后面跟一个数字以及该脚本所控制的服务名,例如S10network。 当init从低的运行级向高的运行级过渡时,它按照数字递增的顺序运行所有以S开头的脚本,S=start,即启动脚本对应的服务;K=kill,即杀死脚本对应服务。
前面介绍到rcx.d目录下指向启动脚本的符号链接是由K或S+数字+服务名 所组成,中间这个数字十分重要,系统启动时按照这个数字递增执行所有S开头的脚本,系统关闭时按照这个数字递减执行所有K开头的脚本。
如何自定义符号链接? 启动脚本: 因为我刚才的运行级是2级 所以在/etc/rc.d/rc2.d 目录下运行
//重新定义nginx服务的脚本启动顺序
#ln -s /etc/init.d/nginx S99nginx
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics