在成功安装了Nginx以后,本文介绍Nginx的几个基本操做:启动、中止、重启、信号控制nginx
Nginx的启动:shell
命令格式: nginx地址 -c nginx配置文件地址
浏览器
下面是个人电脑上的操做过程。注意启动须要su权限。bash
[neil@neilhost ~]$ cd /usr/local/nginx/sbin [neil@neilhost sbin]$ ll 总用量 3260 -rwxr-xr-x. 1 root root 3334713 3月 3 13:36 nginx [neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf [sudo] password for neil: [neil@neilhost sbin]$
有没有启动成功呢?验证方法有三种:服务器
验证方法1:打开浏览器,访问127.0.0.1,如过你是远程访问的服务器或者虚拟机,使用对应的IP地址。nginx默认的端口号设置就是80。app
验证方法2:查看nginx的进程是否存在tcp
[neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(3675)---nginx(3676) [neil@neilhost sbin]$
能够看到,系统的的PID为3675的进程就是nginx,说明nginx已经成功启动。测试
(本文出自oschina和HappyBKs 文章:http://my.oschina.net/happyBKs/blog/632771)spa
验证方法3:查看80号端口被什么程序占用,是否是nginx.net
[neil@neilhost sbin]$ sudo netstat -tunpl | grep 80 [sudo] password for neil: tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3675/nginx: master udp 0 0 0.0.0.0:59251 0.0.0.0:* 3180/dhclient udp 0 0 0.0.0.0:68 0.0.0.0:* 3180/dhclient udp6 0 0 :::58294 :::* 3180/dhclient [neil@neilhost sbin]$
能够看到,80端口的监听中的程序正式nginx
Nginx的中止:
Nginx的中止有三种不一样的中止方法:从容中止,快速中止,强制中止
(1)从容中止:首先,须要获取nginx的主进程master process的ID号;而后,用-QUIT参数做为从容中止的类型标注来中止
[neil@neilhost sbin]$ ps -ef | grep nginx root 3675 1 0 12:08 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 3676 3675 0 12:08 ? 00:00:00 nginx: worker process neil 3855 3598 0 12:41 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$ kill -QUIT 3675 bash: kill: (3675) - 不容许的操做 [neil@neilhost sbin]$ sudo kill -QUIT 3675 [sudo] password for neil: [neil@neilhost sbin]$ [neil@neilhost sbin]$ ps -ef | grep nginx neil 3877 3598 0 12:42 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$
这里能够看到,原先的nginx主进程已经关闭,剩余的那个3877进程是nginx的开机自动启动服务,它不隶属于nginx主进程,因此依然存在,可是nginx服务器自己已经成功关闭。
不信,请再访问一下浏览器:
(2)快速中止:步骤与从容中止相似,只是用于中止的信号量参数不一样。而且,快速中止的信号量参数有两个,-TERM和-INT,两个都能表示快速中止,用哪一个均可以。
[neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf [sudo] password for neil: [neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(4242)---nginx(4243) [neil@neilhost sbin]$ ps -ef | grep nginx root 4242 1 0 13:18 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 4243 4242 0 13:18 ? 00:00:00 nginx: worker process neil 4283 3598 0 13:22 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$ sudo kill -TERM 4242 [sudo] password for neil: [neil@neilhost sbin]$ ps -ef | grep nginx neil 4307 3598 0 13:25 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$
(3) 强制中止:所谓强制中止,就是无论如今的nginx是否真的容许被中止掉,咱们都要强制将其中止。
用的命令是pkill -9
这里须要注意的是,这里的命令是pkill,而不是kill,效果上的不一样我会在下面的实际操做中作个比较。-9参数不用我多说了,那是“绝杀”
[neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf [neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(4331)---nginx(4332) [neil@neilhost sbin]$ ps -ef | grep nginx root 4331 1 0 13:28 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 4332 4331 0 13:28 ? 00:00:00 nginx: worker process neil 4342 3598 0 13:28 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$ sudo kill -9 nginx [neil@neilhost sbin]$ ps -ef | grep nginx nobody 4332 1 0 13:28 ? 00:00:00 nginx: worker process neil 4353 3598 0 13:29 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$ [neil@neilhost sbin]$ [neil@neilhost sbin]$ [neil@neilhost sbin]$ [neil@neilhost sbin]$ [neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] still could not bind() [neil@neilhost sbin]$ sudo pkill -9 nginx [neil@neilhost sbin]$ ps -ef | grep nginx neil 4387 3598 0 13:30 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$
从上面的例子能够看到,kill -9 只能将nginx的主进程杀死,但没法杀死其工做进程。而且,此时,若是想开启nginx也是不被容许的;因此正确的强制中止的命令应该是pkill -9 nginx
Nginx的重启:
何时咱们须要重启Nginx?不少时候都有重启Nginx的必要。好比,当Nginx的配置文件被更改,须要经过重启Nginx来使得配置更改生效。这一点到和Tomcat十分类似。
这里,咱们就假设咱们的应用场景是在修改过配置文件以后须要重启nginx。这时候,咱们正确的操做步骤不是当即执行重启命令,而是应该对配置文件进行测试,看其是否符合语法经过检测。命令以下:哪种均可以。
[neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -t [sudo] password for neil: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [neil@neilhost sbin]$ [neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [neil@neilhost sbin]$ [neil@neilhost sbin]$ sudo ./nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [neil@neilhost sbin]$
下面,咱们开始重启nginx。命令是./nginx -s reload
可是,这里有几个地方须要注意,先看看个人操做示例。
[neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -s reload nginx: [alert] kill(4331, 1) failed (3: No such process) [neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf [neil@neilhost sbin]$ ps -ef | grep nginx root 4531 1 0 13:53 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 4532 4531 0 13:53 ? 00:00:00 nginx: worker process neil 4537 3598 0 13:53 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -s reload [neil@neilhost sbin]$ [neil@neilhost sbin]$ ps -ef | grep nginx root 4531 1 0 13:53 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 4544 4531 0 13:53 ? 00:00:00 nginx: worker process neil 4552 3598 0 13:54 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$ [neil@neilhost sbin]$ [neil@neilhost sbin]$ [neil@neilhost sbin]$ [neil@neilhost sbin]$ [neil@neilhost sbin]$ [neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(4531)---nginx(4544) [neil@neilhost sbin]$ [neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -s reload [neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(4531)---nginx(4587) [neil@neilhost sbin]$ [neil@neilhost sbin]$ [neil@neilhost sbin]$ sudo /usr/local/nginx/sbin/nginx -s reload [neil@neilhost sbin]$ [neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(4531)---nginx(4604) [neil@neilhost sbin]$ [neil@neilhost sbin]$
(a)重启nginx,必须是在nginx已经启动的状况写下进行的。不然,会重启失败。
(b)重启 不等于 关闭+启动 !!!!
经过上面的例子就能够清楚地看到:虽然重启了屡次,可是nginx的主进程号在第一次启动后就没有改变过,不管后面重启了的多少次,主进程ID都是4531,重启改变的只有工做进程的ID。着说明了什么本身细细回味吧。
若是是本身“关闭+启动”n次,那么nginx的主进程和工做进程的ID号必定都在变化。
重启有两种方法,除了上面的方法外,还有一种利用信号的方式。kill -HUP
[neil@neilhost sbin]$ sudo kill -HUP 4531 [sudo] password for neil: [neil@neilhost sbin]$ [neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(4531)---nginx(4867) [neil@neilhost sbin]$ ps -ef | grep nginx root 4531 1 0 13:53 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 4867 4531 0 14:08 ? 00:00:00 nginx: worker process neil 4880 3598 0 14:08 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$ [neil@neilhost sbin]$ sudo kill -HUP 4531 [neil@neilhost sbin]$ [neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(4531)---nginx(4889) [neil@neilhost sbin]$ [neil@neilhost sbin]$ sudo kill -HUP 4531 [neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(4531)---nginx(4906) [neil@neilhost sbin]$
一样,这种方法下,nginx重启后主进程ID依然不变,改变的唯有工做进程ID。
Nginx的信号控制
好,以上就是nginx的启动、关闭、重启的操做方法。咱们能够可看到,在关闭的三种方式和重启的最后一种方式中,咱们使用的都是信号控制的方法。这里,本文会对信号控制的相关内容作一个简单的集中介绍。
信号量 | 含义 |
HUP | 重启 |
QUIT | 从容中止 |
TERM | 快速中止 |
INT | 快速中止 |
USR1 | 切换日志文件 |
USR2 | 平滑升级可执行进程 |
WINCH | 从容关闭工做进程 |
这里,我分别说一下,好比INT和TERM两个快速关闭的含义。
INT(快速关闭)----是当用户键入<Control-C>时由终端驱动程序发送的信号。这是一个终止当前操做的请求,若是捕获了这个信号,一些简单的程序应该退出,或者容许自给被终止,这也是程序没有捕获到这个信号时的默认处理方法。拥有命令行或者输入模式的那些程序应该中止它们在作的事情,清除状态,并等待用户的再次输入。
TERM(快速关闭)----是请求完全终止某项执行操做,它指望接收进程清除自给的状态并退出。
HUP: 平滑启动。若是想要更改配置而不需中止并从新启动服务,请使用该命令。在对配置文件做必要的更改后,发出该命令以动态更新服务配置。
QUIT:从容关闭。
由原以上几个信号已经在前面的关闭和重启操做重介绍过,这里再也不累述。
USER1和USER2,命令调用的方式就是 kill -USER1 进程号和 kill -USER2 进程号。具体在实际状况怎么使用,咱们放到后面的文章中介绍。这里,本文只介绍WINCH。
kill -WINCH的功能是从容关闭工做进程。可是,kill -WINCH后面须要跟的nginx的主进程号。它所作的是关闭特定进程号的主进程的下属的工做进程。具体看例子:
[neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(4531)---nginx(4906) [neil@neilhost sbin]$ ps -ef | grep nginx root 4531 1 0 13:53 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 4906 4531 0 14:08 ? 00:00:00 nginx: worker process neil 5058 3598 0 14:32 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$ [neil@neilhost sbin]$ sudo kill -WINCH 4531 [sudo] password for neil: [neil@neilhost sbin]$ [neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(4531) [neil@neilhost sbin]$ ps -ef | grep nginx root 4531 1 0 13:53 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf neil 5092 3598 0 14:34 pts/0 00:00:00 grep --color=auto nginx [neil@neilhost sbin]$