您现在的位置是:首页 > 文章详情

linux下nginx启动、重启、关闭

日期:2018-06-07点击:750

方式一:传统方法

一、启动

cd usr/local/nginx/sbin ./nginx

二、重启

更改配置重启nginx

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

判断配置文件是否正确

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

三、关闭

查询nginx主进程号

ps -ef | grep nginx

从容停止   kill -QUIT 主进程号

快速停止   kill -TERM 主进程号

强制停止   kill -9 nginx

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

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

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

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

vi /etc/init.d/nginx  (输入下面的代码)

  1. #!/bin/bash
  2. # nginx Startup script for the Nginx HTTP Server
  3. # it is v.0.0.2 version.
  4. # chkconfig: - 85 15
  5. # description: Nginx is a high-performance web and proxy server.
  6. #              It has a lot of features, but it's not for everyone.
  7. # processname: nginx
  8. # pidfile: /var/run/nginx.pid
  9. # config: /usr/local/nginx/conf/nginx.conf
  10. nginxd=/usr/local/nginx/sbin/nginx
  11. nginx_config=/usr/local/nginx/conf/nginx.conf
  12. nginx_pid=/var/run/nginx.pid
  13. RETVAL=0
  14. prog="nginx"
  15. # Source function library.
  16. . /etc/rc.d/init.d/functions
  17. # Source networking configuration.
  18. . /etc/sysconfig/network
  19. # Check that networking is up.
  20. [ ${NETWORKING} = "no" ] && exit 0
  21. [ -x $nginxd ] || exit 0
  22. # Start nginx daemons functions.
  23. start() {
  24. if [ -e $nginx_pid ];then
  25.    echo "nginx already running...."
  26.    exit 1
  27. fi
  28.    echo -n $"Starting $prog: "
  29.    daemon $nginxd -c ${nginx_config}
  30.    RETVAL=$?
  31.    echo
  32.    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
  33.    return $RETVAL
  34. }
  35. # Stop nginx daemons functions.
  36. stop() {
  37.         echo -n $"Stopping $prog: "
  38.         killproc $nginxd
  39.         RETVAL=$?
  40.         echo
  41.         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
  42. }
  43. # reload nginx service functions.
  44. reload() {
  45.     echo -n $"Reloading $prog: "
  46.     #kill -HUP `cat ${nginx_pid}`
  47.     killproc $nginxd -HUP
  48.     RETVAL=$?
  49.     echo
  50. }
  51. # See how we were called.
  52. case "$1" in
  53. start)
  54.         start
  55.         ;;
  56. stop)
  57.         stop
  58.         ;;
  59. reload)
  60.         reload
  61.         ;;
  62. restart)
  63.         stop
  64.         start
  65.         ;;
  66. status)
  67.         status $prog
  68.         RETVAL=$?
  69.         ;;
  70. *)
  71.         echo $"Usage: $prog {start|stop|restart|reload|status|help}"
  72.         exit 1
  73. esac
  74. exit $RETVAL

 

:wq  保存并退出

设置文件的访问权限

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

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

同样的修改了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等等

10.测试一下

原文链接:https://www.centoschina.cn/server/test/nginx/10784.html
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章