Detailed description of Nginx startup/restart scripts and nginx restart scripts
Nginx Manual start
Stop operation
The stop operation is performed by sending a signal to the nginx process (for more information about the signal, see the linux chapter ).
Step 1: query the nginx master process number
Ps-ef | grep nginx
Find the master process in the process list. Its number is the master process number.
Step 2: send signals
Stop Nginx with ease:
Kill-QUIT master process number
Stop Nginx quickly:
Kill-TERM master process number
Force stop Nginx:
Pkill-9 nginx
In addition, if the pid file storage path is configured in nginx. conf, the file stores the Nginx main process number. If no pid file is specified, the file is placed in the nginx logs directory. With the pid file, we do not need to first query the main process Number of Nginx, but directly send a signal to Nginx. The command is as follows:
Kill-signal type '/usr/nginx/logs/nginx. pid'
Command:/usr/local/nginx/sbin/nginx
If:
[Root @ kangxiaowei ~] #/Usr/local/nginx/sbin/nginx
[Emerg]: bind () to 0.0.0.0: 80 failed (98: Address already in use)
[Emerg]: bind () to 0.0.0.0: 80 failed (98: Address already in use)
[Emerg]: bind () to 0.0.0.0: 80 failed (98: Address already in use)
[Emerg]: bind () to 0.0.0.0: 80 failed (98: Address already in use)
[Emerg]: bind () to 0.0.0.0: 80 failed (98: Address already in use)
[Emerg]: still cocould not bind ()
Run/root/lnmp stop again to disable lnmp.
Nginx boot script
Start nginx automatically at startup,
If you need to start the service, after saving the/etc/init. d/nginx file,
Run the following command:
The Code is as follows:
Chkconfig -- add ningx
Chkconfig -- level nginx 2345 on
Automatic startup script
The Code is as follows:
#! /Bin/sh
# Chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in/etc/init. d and
# Run 'Update-rc. d-f nginx defaults', or use the appropriate command on your
# Distro. For CentOS/Redhat run: 'chkconfig -- add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $ all
# Required-Stop: $ all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Descri (www.111cn.net) ption: starts nginx using start-stop-daemon
### END INIT INFO
PATH =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC = "nginx daemon"
NAME = nginx
DAEMON =/usr/local/platform/nginx/sbin/$ NAME
CONFIGFILE =/usr/local/platform/nginx/conf/$ NAME. conf
PIDFILE =/usr/local/platform/nginx/logs/$ NAME. pid
SCRIPTNAME =/etc/init. d/$ NAME
Set-e
[-X "$ DAEMON"] | exit 0
Do_start (){
$ DAEMON-c $ CONFIGFILE | echo-n "nginx already running"
}
Do_stop (){
Kill-INT 'cat $ pidfile' | echo-n "nginx not running"
}
Do_reload (){
Kill-HUP 'cat $ pidfile' | echo-n "nginx can't reload"
}
Case "$1" in
Start)
Echo-n "Starting $ DESC: $ NAME"
Do_start
Echo "."
;;
Stop)
Echo-n "Stopping $ DESC: $ NAME"
Do_stop
Echo "."
;;
Reload | graceful)
Echo-n "Reloading $ DESC configuration ..."
Do_reload
Echo "."
;;
Restart)
Echo-n "Restarting $ DESC: $ NAME"
Do_stop
Do_start
Echo "."
;;
*)
Echo "Usage: $ SCRIPTNAME {start | stop | reload | restart}"> & 2
Exit 3
;;
Esac
Exit 0
The configurations you need to modify include
The Code is as follows:
#! /Bin/sh
PATH =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME = nginx
DAEMON =/usr/local/nginx/sbin/$ NAME
CONFIGFILE =/usr/local/nginx/conf/$ NAME. conf
PIDFILE =/usr/local/nginx/logs/$ NAME. pid
Save the edited file and run the following command:
The Code is as follows:
1 chmod + x/etc/init. d/nginx
Now add Nginx to chkconfig and set it to start upon startup.
The Code is as follows:
12 chkconfig -- add nginx chkconfig nginx on
# Check
The Code is as follows:
1 chkconfig -- list nginx
Nginx 0: off 1: off 2: on 3: on 4: on 5: on 6: off
From: http://www.111cn.net/sys/nginx/52908.htm
How to enable and disable nginx after a smooth restart
Assume that your nginx directory is/usr/opt/nginx/sbin/nginx. Assume that the nginx directory is/usr/opt/nginx/conf/www4.conf. the restart method is as follows: the/usr/opt/nginx/sbin/nginx-c/usr/opt/nginx/conf/www4.conf parameter-c specifies the path of the configuration file. If this parameter is not specified, nginx loads nginx under the conf directory of the directory by default. conf. smooth restart: 1. Check whether the modified configuration file is correct: /usr/opt/nginx/sbin/nginx-t-c/usr/opt/nginx/conf/www4.conf 2. kill-HUP Nginx master process number. Example: kill-HUP 'cat/usr/local/webserver/nginx/logs/nginx. pid:-c specifies a configuration file for Nginx to replace the default one. -T does not run, but only tests the configuration file.
How to restart nginx in linux
In earlier versions of nginx, the kill command is used to send a signal to nginx to restart nginx.
However, the-s option is added to nginx to stop and reload nginx.
1. For a smooth restart of nginx, you can use the./nginx-s reload command to achieve a smooth restart of nginx.
2. If it is not a smooth restart, you can stop nginx and then start it:
./Nginx-s stop &./nginx
After we modify the nginx configuration, we want to restart nginx to make nginx take effect. In this case, to ensure that nginx can provide normal services during the restart phase, we usually use a smooth restart (reload) method) restart nginx. In this case, nginx loads the new configuration and fork the new worker process. At the same time, the master process sends a signal to the old worker process to tell the current situation of the old worker process. After the old worker process receives the signal from the master process, if the request is not processed at the time, it will exit. If the request is being processed, the old worker process will finish processing the request and then exit. Nginx reload the new configuration in this way, so that the service can still be provided during the restart process.