版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/51223297
1. Nginx的启动
在Nginx服务器搭建成功后,要启动它,Nginx服务器才能正常运行。启动代码格式如下:
Nginx地址 -c Nginx配置文件地址
实例:
xiaosi@Qunar:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2. Nginx的停止
Nginx服务器启动之后,要想其停止运行,有三种方式,第一种是从容停止,第二种是快速停止,第三种是强制停止。
(1)从容停止
xiaosi@Qunar:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 7655 2151 0 13:19 ? 00:00:00 nginx: master process/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 7656 7655 0 13:19 ? 00:00:00 nginx: worker process
xiaosi 8466 7586 0 14:07 pts/24 00:00:00 grep --color=auto nginx
首先我们查看nginx的进程号,根据进程号来停止。
xiaosi@Qunar:/usr/local/nginx/sbin$ sudo kill -QUIT 7655
xiaosi@Qunar:/usr/local/nginx/sbin$ ps -ef | grep nginx
xiaosi 8505 7586 0 14:10 pts/24 00:00:00 grep --color=auto nginx
(2)快速停止
xiaosi@Qunar:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
xiaosi@Qunar:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 8805 2151 0 14:24 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 8806 8805 0 14:24 ? 00:00:00 nginx: worker process
xiaosi 8808 7586 0 14:24 pts/24 00:00:00 grep --color=auto nginx
xiaosi@Qunar:/usr/local/nginx/sbin$ sudo kill -TERM 8805
xiaosi@Qunar:/usr/local/nginx/sbin$ ps -ef | grep nginx
xiaosi 8859 7586 0 14:26 pts/24 00:00:00 grep --color=auto nginx
(3)强制停止
xiaosi@Qunar:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 8876 2151 0 14:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 8877 8876 0 14:27 ? 00:00:00 nginx: worker process
xiaosi 8901 7586 0 14:28 pts/24 00:00:00 grep --color=auto nginx
xiaosi@Qunar:/usr/local/nginx/sbin$ sudo pkill -9 nginx
xiaosi@Qunar:/usr/local/nginx/sbin$ ps -ef | grep nginx
xiaosi 8911 7586 0 14:28 pts/24 00:00:00 grep --color=auto nginx
3. Nginx的重启
Nginx服务器在运行的时候,若想对其进行重启启动,比如当我们修改了配置文件,需要让新配置的文件生效,就得重启等。
3.1 在重启之前我们需要验证配置文件是否是正确。
第一种方法:进入nginx的sbin/目录下:
xiaosi@Qunar:/usr/local/nginx/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
第二种方法:
xiaosi@Qunar:/usr/local/nginx/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
3.2 重启
xiaosi@Qunar:/usr/local/nginx/sbin$ sudo ./nginx -s reload
nginx: [alert] kill(8876, 1) failed (3: No such process)
我们可以看到我们重启失败,主要是因为我们已经关闭了nginx,只有nginx运行中我们才能重启,所以我们先启动nginx。
xiaosi@Qunar:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
xiaosi@Qunar:/usr/local/nginx/sbin$ sudo ./nginx -s reload
xiaosi@Qunar:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 9441 2151 0 14:45 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 9445 9441 0 14:45 ? 00:00:00 nginx: worker process
xiaosi 9447 7586 0 14:45 pts/24 00:00:00 grep --color=auto nginx
从上面可以看到我们先启动之后,再重启就可以了。
我们还可以根据发射信号的方式重启nginx:
xiaosi@Qunar:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 9441 2151 0 14:45 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 9445 9441 0 14:45 ? 00:00:00 nginx: worker process
xiaosi 9508 7586 0 14:48 pts/24 00:00:00 grep --color=auto nginx
xiaosi@Qunar:/usr/local/nginx/sbin$ sudo kill -HUP 9441