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 reopen :Reopens the log files.
nginx –s reload :Reloads the configuration
nginx -t
nginx –g "timer_resolution 200ms"; 指定新的配置項
2. 將Nginx作為系統服務運行(System Service)
為了讓Ngnix隨系統啟動,需要將它設置為系統服務
System V
System V中系統服務是由init進程來管理,系統服務分為不同的級別(Runlevel State):
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目錄
編寫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
1. NGNIX 學習系列-基礎