nginx啟動、重啟、關閉


轉載自: https://blog.csdn.net/helloxiaozhe/article/details/80596138

方式一:傳統方法

啟動
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 (輸入下面的代碼)

#!/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 保存並退出

注意: nginxd和nginxconfig中寫自己nginx位置!!!

設置文件的訪問權限

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配置成自啟動的服務
添加至服務管理列表,並讓其開機自動啟動
[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
查看一下端口
[root@nginx ~]# netstat -ntlp | grep :80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3889/nginx

對於其他服務也同樣適用,比如Mysql,php-fpm等等

測試

在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM