Nginx的启动、关闭和重启

Nginx的启动关闭和重启

nginx可以通过信号指令控制进程,常用的信号有:

QUIT:处理完当前请求后关闭进程

HUP:重新加载配置,也就是关闭原有的进程,开启新的进程,此操作不会中断用户的访问请求,所以又称为平滑重启nginx

USR1:用于nginx的日志切换,也就是重新打开一个日志文件,每天要生成一个新的日志文件时,可以使用该信号控制。

USR2:平滑升级可执行程序

WINCH :从容关闭工作进程

常用的方法控制有:

①  kill –XXX主进程号

 

[root@localhost run]# ps -ef |grep nginx

root     2771     1  0 10:34 ?        00:00:00 nginx: master process/opt/nginx/sbin/nginx

503      2772  2771  0 10:34 ?        00:00:00 nginx: worker process

503      2773  2771  0 10:34 ?        00:00:00 nginx: worker process

503       2774 2771  0 10:34 ?        00:00:00 nginx: worker process

503      2775  2771  0 10:34 ?        00:00:00 nginx: worker process

root     3575  2730  0 12:08 pts/1    00:00:00 grep nginx

[root@localhost run]# kill -QUIT2771

[root@localhost run]# ps -ef |grep nginx

root     3578  2730  0 12:09 pts/1    00:00:00 grep nginx

[root@localhost run]#/opt/nginx/sbin/nginx

[root@localhost run]# ps -ef |grep nginx

root     3580     1  0 12:09 ?        00:00:00 nginx: master process/opt/nginx/sbin/nginx

503      3581  3580  0 12:09 ?        00:00:00 nginx: worker process

503      3582  3580  0 12:09 ?        00:00:00 nginx: worker process

503      3583  3580  0 12:09 ?        00:00:00 nginx: worker process

503      3584  3580  0 12:09 ?        00:00:00 nginx: worker process

root     3588  2730  0 12:10 pts/1    00:00:00 grep nginx

[root@localhost run]# kill -HUP3580

[root@localhost run]# ps -ef |grep nginx

root     3580     1  0 12:09 ?        00:00:00 nginx: master process/opt/nginx/sbin/nginx

503      3590  3580  0 12:10 ?        00:00:00 nginx: worker process

503      3591  3580  0 12:10 ?        00:00:00 nginx: worker process

503      3592  3580  0 12:10 ?        00:00:00 nginx: worker process

503      3593  3580  0 12:10 ?        00:00:00 nginx: worker process

root     3596  2730  0 12:10 pts/1    00:00:00 grep nginx

[root@localhost run]# kill -WINCH3580

[root@localhost run]# ps -ef |grep nginx

root     3580     1  0 12:09 ?        00:00:00 nginx: master process/opt/nginx/sbin/nginx

 

② kill -信号类型`cat /安装目录/logs/nginx.pid`

使用该方法时需要nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginxlogs目录下。有了pid件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:

kill -信号类型`cat /安装目录/logs/nginx.pid` ,本人将nginx安装于/opt/nginx目录下 

[root@localhost conf]# cd /opt/nginx/logs/

[root@localhost logs]# ls

access.log error.log  nginx.pid

[root@localhost logs]# ps -ef |grep nginx

root     3580     1  0 12:09 ?        00:00:00 nginx: master process/opt/nginx/sbin/nginx

503      3680  3580  0 12:28 ?        00:00:00 nginx: worker process

503      3681  3580  0 12:28 ?        00:00:00 nginx: worker process

503      3682  3580  0 12:28 ?        00:00:00 nginx: worker process

503      3683  3580  0 12:28 ?        00:00:00 nginx: worker process

root     3687  2730  0 12:28 pts/1    00:00:00 grep nginx

[root@localhost logs]# kill -HUP`cat /opt/nginx/logs/nginx.pid`

[root@localhost logs]# ps -ef |grep nginx

root     3580     1  0 12:09 ?        00:00:00 nginx: master process/opt/nginx/sbin/nginx

503      3689  3580  0 12:28 ?        00:00:00 nginx: worker process

503      3690  3580  0 12:28 ?        00:00:00 nginx: worker process

503      3691  3580  0 12:28 ?        00:00:00 nginx: worker process

503      3692  3580  0 12:28 ?        00:00:00 nginx: worker process

root     3694  2730  0 12:28 pts/1    00:00:00 grep nginx

[root@localhost logs]#

 

### 在 Rocky Linux 9 中管理 Nginx 服务的命令 在 Rocky Linux 9 中,`systemctl` 是用于管理系统服务的主要工具。以下是启动关闭重启重新加载 Nginx 服务的具体命令: #### 启动 Nginx 服务 使用以下命令启动 Nginx 服务: ```bash systemctl start nginx ``` 此命令会启动 Nginx 服务[^3]。 #### 关闭 Nginx 服务 使用以下命令停止 Nginx 服务: ```bash systemctl stop nginx ``` 此命令会停止正在运行的 Nginx 服务[^3]。 #### 重启 Nginx 服务 如果需要重启 Nginx 服务,可以使用以下命令: ```bash systemctl restart nginx ``` 此命令会先停止当前的 Nginx 服务,然后重新启动它[^3]。 #### 重新加载 Nginx 配置 当修改了 Nginx 的配置文件时,可以使用以下命令重新加载配置而不中断现有连接: ```bash systemctl reload nginx ``` 此命令会在不中断服务的情况下应用新的配置文件更改[^2]。 #### 检查 Nginx 服务状态 为了确认 Nginx 服务的状态,可以使用以下命令: ```bash systemctl status nginx ``` 此命令将显示 Nginx 服务的当前状态,包括是否正在运行以及最近的日志信息。 #### 设置 Nginx 开机自启 如果希望 Nginx 在系统启动时自动运行,可以启用开机自启功能: ```bash systemctl enable nginx ``` 此命令会在系统启动时自动启动 Nginx 服务。 #### 禁用 Nginx 开机自启 如果不需要 Nginx 在系统启动时自动运行,可以禁用开机自启功能: ```bash systemctl disable nginx ``` 此命令将禁用 Nginx 的开机自启功能。 ### 注意事项 - 确保 Nginx 服务已正确安装并配置。如果未安装,可以通过包管理器进行安装,例如 `dnf install nginx`。 - 在执行上述命令前,建议检查 Nginx 的配置文件是否有语法错误。可以使用以下命令测试配置文件: ```bash nginx -t ``` 此命令会验证 Nginx 配置文件的语法正确性[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值