当前位置: 代码网 > it编程>游戏开发>ar > Nginx启动、重启、关闭/Nginx开启启动配置实现方式

Nginx启动、重启、关闭/Nginx开启启动配置实现方式

2026年03月13日 ar 我要评论
1、方式一:手动启动方式1.1、启动》 cd usr/local/nginx/sbin》 ./nginx1.2、重启kill -hup 主进程号或进程号文件路径或者使用》 cd /usr/local/

1、方式一:手动启动方式

1.1、启动

》 cd usr/local/nginx/sbin
》 ./nginx

1.2、重启

kill -hup 主进程号或进程号文件路径
或者使用
》 cd /usr/local/nginx/sbin
》 ./nginx -s reload

1.3、检查配置文件路径

》 nginx -t -c /usr/local/nginx/conf/nginx.conf
或者
》 cd /usr/local/nginx/sbin
》 ./nginx -t

1.4、关闭

查询nginx主进程号

  ps -ef | grep nginx

  从容停止   kill -quit 主进程号

  快速停止   kill -term 主进程号

  强制停止   kill -9 nginx

  若nginx.conf配置了pid文件路径,如果没有,则在logs目录下

  kill -信号类型 '/usr/local/nginx/logs/nginx.pid'

1.5、检查nginx服务是否启动

ps -ef|grep nginx

2、方式二:通过配置/etc/init.d/nginx文件来控制

这里使用的是编写shell脚本的方式来处理

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
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

:wq  保存并退出

设置文件的访问权限

chmod a+x /etc/init.d/nginx   (a+x ==> all user can execute  所有用户可执行)

同样的修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,

配置开机自启动

方式一:在/etc/rc.local中配置

可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了

vi /etc/rc.local

加入一行  /etc/init.d/nginx start    保存并退出,下次重启会生效。

方式二:将nginx配置成自启动的服务

1.添加至服务管理列表,并让其开机自动启动

[root@nginx ~]# chkconfig --add nginx
[root@nginx ~]# chkconfig nginx on 
[root@nginx ~]# chkconfig nginx --list 
nginx              0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭

2、nginx启动、停止、无间断服务重启

[root@example ~]# service nginx start

[root@example ~]# service nginx stop

[root@example ~]# service nginx reload

3.查看一下端口

[root@nginx ~]# netstat -ntlp | grep :80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   listen      3889/nginx

对于其他服务也同样适用,比如mysql,php-fpm等等

这样在控制台就很容易的操作nginx了:查看nginx当前状态、启动nginx、停止nginx、重启nginx…

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com