1. NGNIX 学习系列-控制,运行

1.Ngnix进程控制
nginx

    控制正在运行ngnix是经过发送信号来完成,主要有如下一些信号centos

    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
bash

    nginx -t                : Test Configuration File
ide

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

2. 将Nginx做为系统服务运行(System Service)this

为了让Ngnix随系统启动,须要将它设置为系统服务spa

  • System Vrest

System V中系统服务是由init进程来管理,系统服务分为不一样的级别(Runlevel State):orm


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













centos 6 /etc目录中有不一样级别服务对应的启动脚本,这些目录中脚本都是软连接到/etc/init.d目录server

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加入到系统默认启动级别中

[root@example.com ~]# chkconfig nginx on

3. 检查nginx 是否在对应启动级别中(在对应的rc3.d中有软连接)
[root@example.com ~]# 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进程也退出

相关文章
相关标签/搜索