nginx啓動、重啓、關閉

nginx啓動、重啓、關閉

 

方式一:傳統方法php

 

1、啓動  html

cd usr/local/nginx/sbin
./nginx

2、重啓nginx

  更改配置重啓nginx  web

kill -HUP 主進程號或進程號文件路徑
或者使用
cd /usr/local/nginx/sbin
./nginx -s reload

    判斷配置文件是否正確 sql

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

3、關閉shell

  查詢nginx主進程號bash

  ps -ef | grep nginxtcp

  從容中止   kill -QUIT 主進程號ide

  快速中止   kill -TERM 主進程號php-fpm

  強制中止   kill -9 nginx

  若nginx.conf配置了pid文件路徑,若是沒有,則在logs目錄下

  kill -信號類型 '/usr/local/nginx/logs/nginx.pid'

 

方式二:經過配置/etc/init.d/nginx文件來控制

這裏使用的是編寫shell腳本的方式來處理

 

vi /etc/init.d/nginx  (輸入下面的代碼)

[html]  view plain  copy
 
  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:關閉

二、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等等

相關文章
相關標籤/搜索