Nginx启动与关闭

本文档详细介绍了如何在Linux系统中关闭防火墙并启动、关闭及设置Nginx服务。包括使用systemctl命令禁用防火墙,启动Nginx的两种方式(立即停止和从容停止),以及通过systemd设置Nginx开机自启的步骤。此外,还提供了检查Nginx服务状态和强制杀死进程的方法。
摘要由CSDN通过智能技术生成

Nginx启动与关闭

1 Nginx启动

  1. 关闭防火墙命令

    #关闭防火墙,系统重启后还会启动
    [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 
    
  2. 启动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
    
  3. 测试:http://+端口号

2 关闭Nginx

  1. 立即停止服务

    这种方法比较强硬,无论进程是否在工作,都会直接停止进程。

    [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
    
  2. 从容停止服务

这种方法较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
  1. 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 设置开机自启

  1. 进到系统服务添加路径:

    [root@node1 sbin]# cd /usr/lib/systemd/system/ 
    
  2. 建立服务文件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
    
  3. 添加执行权限

    以755的权限保存在目录:/usr/lib/systemd/system

    [root@node1 system]# chmod +x nginx.service 
    
  4. 设置开机自启

    可在任意目录下执行

    [root@node1 system]# systemctl enable nginx  
    [root@node1 system]# systemctl list-unit-files |grep nginx
    nginx.service                               enabled
    
  5. 其他命令

    启动nginx服务:systemctl start nginx
    停止开机自启动:systemctl disable nginx
    查看服务当前状态: systemctl status nginx
    重新启动服务: systemctl restart nginx
    查看所有已启动的服务: systemctl list-units --type=service
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值