linux中Nginx开机自启动

1.Nginx开机自启动脚本

[root@localhost ~]# 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: /usr/local/nginx/logs/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=/usr/local/nginx/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 /usr/local/nginx/logs/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

⚠️注意:以下内容为自己nginx的路径
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/nginx.pid

查看nginx_pid路径

[root@localhost ~]# whereis nginx.pid

2.设置文件的访问权限

[root@localhost]# chmod a+x /etc/init.d/nginx

3.编辑rc.local

[root@localhost]# vi /etc/rc.local

加入以下内容

/etc/init.d/nginx start

linux中Nginx开机自启动_第1张图片
⚠️注意:如果开机后发现自启动脚本没有执行,你要去确认一下rc.local这个文件的访问权限是否是可执行的,因为rc.local默认是不可执行的。修改rc.local访问权限,增加可执行权限

[root@localhost]# chmod +x /etc/rc.d/rc.local

3.查看nginx是否已经自启动

出现如下内容说明已经自启动

[root@localhost ~]# ps aux | grep nginx
root      1101  0.0  0.0  20584   644 ?        Ss   11:15   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    1103  0.0  0.0  21024  1332 ?        S    11:15   0:00 nginx: worker process
root      2627  0.0  0.0 112824   984 pts/0    S+   11:17   0:00 grep --color=auto nginx

在这里插入图片描述

⚠️注意:如果重启后还是无法自启动

先查看 /etc/init.d/nginx 脚本是否有权限,然后添加到服务列表

[root@localhost]# chkconfig --add /etc/init.d/nginx

再设置开机自启动

[root@localhost]# chkconfig nginx on

你可能感兴趣的:(nginx,linux,运维)