1. NGNIX 學習系列-控制,運行

1.Ngnix進程控制

    控制正在運行ngnix是通過發送信號來完成,主要有以下一些信號

    nginx –s stop     :Stops the daemon immediately (using the TERM signal).
    nginx –s quit      :Stops the daemon gracefully (using the QUIT signal).
    nginx –s reopenReopens the log files.
    nginx –s reload  :Reloads the configuration

    nginx -t                : Test Configuration File

    nginx –g "timer_resolution 200ms";  指定新的配置項

2. 將Nginx作爲系統服務運行(System Service)

爲了讓Ngnix隨系統啓動,需要將它設置爲系統服務

  • System V

System V中系統服務是由init進程來管理,系統服務分爲不同的級別(Runlevel State):


System is halted
1Single-user mode (rescue mode)
2Multiuser mode, without NFS support(Debian and Ubuntu
默認級別)
3Full multiuser mode(Red Hat,Fedora,CentOS6 
默認級別)
4Not used
5
Graphical interface mode(Red Hat and Fedora
默認級別)
6System reboot 













centos 6 /etc目錄中有不同級別服務對應的啓動腳本,這些目錄中腳本都是軟鏈接到/etc/init.d目錄

image.png

編寫sysv script ,系統服務啓動腳本通常在系統啓動,service httpd start ,/etc/init.d/httpd start 時被調用

將Nginx做成System V需要3個步驟

1.編寫Sysv 腳本:  /etc/init.d/nginx 

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Make sure to properly indicate the full path of your Nginx binary and conf. file here
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
pid_file="/usr/local/nginx/logs/nginx.pid"

get_pid(){
    cat "$pid_file"
}

is_running(){
    [ -f "$pid_file" ] && ps $(get_pid) > /dev/null 2>&1
}


start() {
    if [ ! -x $nginx ];then
        echo "$nginx is not executable.."
        exit 3
    fi

    if [ ! -f $NGINX_CONF_FILE ];then
        echo "$NGINX_CONF_FILE is not exist"
        exit 4
    fi

    if is_running;then
        echo "$prog is Already Running..."
    else
        echo -n $"Starting $prog: "
        $nginx -c $NGINX_CONF_FILE
        retval=$?
        if [ $retval -ne 0 ];then
            echo "Unable to start $prog"
            exit 5
        fi
        echo "$prog Started"
    fi
}

stop() {
    if is_running;then
        echo -n $"Stopping $prog: "
        kill -s QUIT "$(get_pid)"
        retval=$?
        if [ $retval -ne 0 ];then
            echo "Unable to stop $prog"
        fi
        echo "$prog Stopped"
    fi
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

status() {
    if is_running;then
        echo "Running"
    else
        echo "Stopped"
        exit 1
    fi
}


case "$1" in
    start)
        $1
        ;;
    stop)
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        $1
        ;;
    status)
        $1
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"
        exit 2
esac


2.將nginx加入到系統默認啓動級別中

[[email protected] ~]# chkconfig nginx on

3. 檢查nginx 是否在對應啓動級別中(在對應的rc3.d中有軟鏈接)
[[email protected] ~]# chkconfig --list nginx
Nginx 0:off 1:off 2:on 3:off 4:on 5:on 6:off
 

  • Systemd

    CentOS 7的服務systemctl腳本存放在:/usr/lib/systemd/,有系統(system)和用戶(user)之分,即:/usr/lib/systemd/system ,/usr/lib/systemd/user

每一個服務以.service結尾,一般會分爲3部分:[Unit]、[Service]和[Install],具體內容如下:

[Unit]
Description=nginx
Documentation=http://nginx.org/en/docs/After=network.target  remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true


[Install]
WantedBy=multi-user.target


2. 優雅地升級Ngnix(Upgrading Nginx gracefully )

  • 複製新版本nginx到/usr/local/nginx/sbin/中替換舊文件

  • 查找正在運行舊版本nginx master進程的進程號 ps x | grep nginx | grep master

  • 向舊進程發送USR2信號:kill –USR2 pid, 此信號會讓舊nginx rename /usr/local/nginx/logs/nginx.pid成/usr/local/nginx/logs/nginx.pid.old 並啓動新nginx二進制文件。此時將會有2個nginx實例運行,一起處理請求

  • kill –WINCH oldPid 使舊nginx的worker進程優雅退出(舊nginx master進程還在)

  • kill –QUIT  oldPid   使舊nginx的master進程也退出


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章