首页 > 其他 > 详细

Nginx使用(配置开机启动)

时间:2014-04-30 16:34:51      阅读:560      评论:0      收藏:0      [点我收藏+]

环境:

  系统:CentOS 6.5 Final

  安装目录:/usr/local/nginx

Nginx开机自启:

 ①编写shell实现控制

bubuko.com,布布扣
vi /etc/init.d/nginx
bubuko.com,布布扣

添加内容:

bubuko.com,布布扣
#!/bin/bash
# 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=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/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 
bubuko.com,布布扣

更改访问权限:

bubuko.com,布布扣
chmod a+x /etc/init.d/nginx
bubuko.com,布布扣

测试效果:

bubuko.com,布布扣
[root@localhost ~]# /etc/init.d/nginx stop
停止 nginx:                                               [确定]
[root@localhost ~]# /etc/init.d/nginx start
正在启动 nginx:                                           [确定]
[root@localhost ~]# /etc/init.d/nginx restart
停止 nginx:                                               [确定]
正在启动 nginx:                                           [确定]
[root@localhost ~]# /etc/init.d/nginx stop
停止 nginx:                                               [确定]
[root@localhost ~]# /etc/init.d/nginx sto
Usage: nginx {start|stop|restart|reload|status|help}
[root@localhost ~]# /etc/init.d/nginx restart
停止 nginx:                                               [失败]
正在启动 nginx:                                           [确定]
bubuko.com,布布扣

②使用该shell配置开机启动

法一:

bubuko.com,布布扣
vi /etc/rc.local
bubuko.com,布布扣

添加一行

bubuko.com,布布扣
/etc/init.d/nginx start 
bubuko.com,布布扣

保存并退出,重启生效。

法二:

bubuko.com,布布扣
chkconfig --add nginx //添加索引
chkconfig --level 23 nginx on //开机启动
bubuko.com,布布扣

重启生效。

③添加自定义命令(使用nginx代替/etc/init.d/nginx,启动nginx再也不用输那么长命令了)

bubuko.com,布布扣
vi  /root/.bashrc
bubuko.com,布布扣

追加

bubuko.com,布布扣
alias nginx="/etc/init.d/nginx"
bubuko.com,布布扣

重启生效。

 

参考:

Linux下的Nginx安装(开机自启动):http://www.cnblogs.com/meteoric_cry/archive/2011/01/27/1945882.html

Linux下chkconfig命令详解:http://www.cnblogs.com/panjun-Donet/archive/2010/08/10/1796873.html

nginx documentation:http://nginx.org/en/docs/ 

 

 

 

 

Nginx使用(配置开机启动),布布扣,bubuko.com

Nginx使用(配置开机启动)

原文:http://www.cnblogs.com/hubery/p/3699350.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!