Nginx启动与关闭
1 Nginx启动
-
关闭防火墙命令
#关闭防火墙,系统重启后还会启动 [root@node1 ~]# systemctl stop firewalld [root@node1 ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1) #禁用防火墙,系统重启后 [root@node1 ~]# systemctl disable firewalld [root@node1 ~]# systemctl list-unit-files |grep firewalld firewalld.service disabled
-
启动Nginx:运行sbin目录下的nginx命令即可
[root@node1 ~]# cd /usr/local/nginx/sbin/ [root@node1 sbin]# ls nginx [root@node1 sbin]# ./nginx [root@node1 sbin]# ps aux|grep nginx root 46423 0.0 0.0 40884 800 ? Ss 16:13 0:00 nginx: master process ./nginx nobody 46424 0.0 0.5 73964 4244 ? S 16:13 0:00 nginx: worker process root 46426 0.0 0.1 12344 1152 pts/4 S+ 16:13 0:00 grep --color=auto nginx
-
测试:http://+端口号
2 关闭Nginx
-
立即停止服务
这种方法比较强硬,无论进程是否在工作,都会直接停止进程。
[root@node1 sbin]# ./nginx -s stop [root@node1 sbin]# ps aux|grep nginx root 25496 0.0 0.0 112824 980 pts/0 S+ 16:44 0:00 grep --color=auto nginx
-
从容停止服务
这种方法较stop相比就比较温和一些了,需要进程完成当前工作后再停止。
[root@node1 sbin]# ./nginx
[root@node1 sbin]# ps aux|grep nginx
root 25482 0.0 0.1 45996 1132 ? Ss 16:44 0:00 nginx: master process ./nginx
nobody 25483 0.0 0.1 46444 1872 ? S 16:44 0:00 nginx: worker process
root 25496 0.0 0.0 112824 980 pts/0 S+ 16:44 0:00 grep --color=auto nginx
[root@node1 sbin]# ./nginx -s quit
[root@node1 sbin]# ps aux|grep nginx
root 25496 0.0 0.0 112824 980 pts/0 S+ 16:44 0:00 grep --color=auto nginx
- killall 方法杀死进程
直接杀死进程,在上面无效的情况下使用,态度强硬,简单粗暴!
[root@node1 sbin]# ./nginx
[root@node1 sbin]# ps aux|grep nginx
root 25482 0.0 0.1 45996 1132 ? Ss 16:44 0:00 nginx: master process ./nginx
nobody 25483 0.0 0.1 46444 1872 ? S 16:44 0:00 nginx: worker process
root 25496 0.0 0.0 112824 980 pts/0 S+ 16:44 0:00 grep --color=auto nginx
[root@node1 sbin]# killall nginx
[root@node1 sbin]# ps aux|grep nginx
root 25574 0.0 0.0 112824 984 pts/0 S+ 16:46 0:00 grep --color=auto nginx
3 设置开机自启
-
进到系统服务添加路径:
[root@node1 sbin]# cd /usr/lib/systemd/system/
-
建立服务文件nginx.service
注意nginx的安装路径保持一致:/usr/local/nginx
[root@node1 system]# vim nginx.service [Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target
-
添加执行权限
以755的权限保存在目录:/usr/lib/systemd/system
[root@node1 system]# chmod +x nginx.service
-
设置开机自启
可在任意目录下执行
[root@node1 system]# systemctl enable nginx [root@node1 system]# systemctl list-unit-files |grep nginx nginx.service enabled
-
其他命令
启动nginx服务:systemctl start nginx 停止开机自启动:systemctl disable nginx 查看服务当前状态: systemctl status nginx 重新启动服务: systemctl restart nginx 查看所有已启动的服务: systemctl list-units --type=service