在centos6.5上把nginx配置成系统服务并设为开机自动启动

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wx5040257/article/details/81275626

环境centos6.5  32位操作系统   nginx-1.10.3

1.  进入目录/etc/init.d

#cd  /etc/init.d

2.  建立文本文件nginx

[root@node1 init.d]#  vi   nginx

代码如下:

#!/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: /usr/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf

#nginx程序路径
nginxd=/usr/nginx/sbin/nginx

#nginx配置文件路径
nginx_config=/usr/nginx/conf/nginx.conf

#nginx pid文件的路径,可以在nginx的配置文件中找到
nginx_pid=/usr/nginx/logs/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

3. 给当前用户添加可执行权限

[root@node1 init.d]#  chmod  u+x  nginx

5.  设置为系统服务

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

6.  设置为开机启动

[root@node1 init.d]# chkconfig nginx on

这时候我们已经可以执行命令重启nginx了

[root@node1 init.d]# service nginx restart

7.  重启系统

[root@node1 init.d]# reboot

8. 重启完毕后,在浏览器中输入地址http://192.168.68.128/,看到如下界面,说明配置成功

猜你喜欢

转载自blog.csdn.net/wx5040257/article/details/81275626
今日推荐