Linux服务器配置nginx开机自启动

./configure --prefix=/usr/local/nginx-1.4.2/ \--with-http_ssl_module --with-http_spdy_module \--with-http_stub_status_module --with-pcre

由于本人部署时启动脚本一直有问题,官方的也文档修改了启动脚本也没法正常启动,总有报错,今天搞正常了所以记录下来。中途启动脚本报错的都可以看nginx状态或者日志处理。配置自启动的好处是服务器挂掉后服务不用自己手动一个个去重启。

1、vi /etc/init.d/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: /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
#nginx_pid=/usr/local/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

2、修改nginxd和nginx_config为你自己的nginx根目录和nginx.conf路径

3、为该文件添加所有者执行权限:

chmod a+x /etc/init.d/nginx //a表示所有,+表示添加,x表示可执行

/etc/init.d/nginx start #启动

/etc/init.d/nginx stop #停止

或者

service nginx start

service nginx stop

4、添加开机自启动:

chkconfig /etc/init.d/nginx on  添加到开机自启动

chkconfig --list  查看linux开机启动项,看到下图的nginx即可

如果345项不是on的状态可以手动修改:chkconfig --level 345 nginx on

Linux服务器配置nginx开机自启动_第1张图片

最后重新加载linux系统服务:systemctl daemon-reload

启动nginx如下图:service nginx start

service nginx status查看状态,

Linux服务器配置nginx开机自启动_第2张图片

 也可以用 ps -ef|grep nginx查看nginx进程

到这里结束,关机重启linux服务器,查看nginx是否自动启动。

你可能感兴趣的:(linux)