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 reopen :Reopens 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
0 |
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
編寫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進程也退出