Skip to content

Nginx启动与停止

掌握Nginx的启动、停止和重启操作是管理Nginx服务的基础。

启动Nginx

直接启动

bash
# 使用绝对路径
/usr/local/nginx/sbin/nginx

# 或使用命令(如果已添加到PATH)
nginx

使用systemctl启动

bash
sudo systemctl start nginx

使用service启动

bash
sudo service nginx start

检查启动状态

bash
# 检查进程
ps aux | grep nginx

# 检查端口
netstat -tlnp | grep nginx

# 检查服务状态
sudo systemctl status nginx

停止Nginx

快速停止

bash
# 立即停止,不等待当前连接完成
nginx -s stop

# 或使用systemctl
sudo systemctl stop nginx

优雅停止

bash
# 等待当前连接处理完成后停止
nginx -s quit

# 或使用systemctl
sudo systemctl stop nginx

强制停止

bash
# 查找进程ID
ps aux | grep nginx

# 杀掉进程
sudo kill -9 <PID>

重启Nginx

重启服务

bash
# 使用systemctl
sudo systemctl restart nginx

# 使用service
sudo service nginx restart

先停止再启动

bash
# 停止
sudo systemctl stop nginx

# 启动
sudo systemctl start nginx

重载配置

修改配置文件后,无需重启即可生效。

重载配置

bash
# 平滑重载配置
nginx -s reload

# 或使用systemctl
sudo systemctl reload nginx

重载原理

  1. Master进程检查新配置
  2. 如果配置正确,启动新的Worker进程
  3. 旧的Worker进程继续处理现有连接
  4. 新的Worker进程处理新连接
  5. 旧Worker进程处理完连接后自动退出

何时使用重载

  • 修改了配置文件
  • 添加了新的虚拟主机
  • 修改了SSL证书
  • 调整了性能参数

重新打开日志

日志文件被移动或删除后,需要重新打开日志文件。

重新打开日志

bash
nginx -s reopen

使用场景

  • 日志轮转后
  • 移动日志文件后
  • 删除日志文件后

测试配置

在重载配置前,应该先测试配置文件是否正确。

测试配置

bash
# 测试配置文件
nginx -t

# 指定配置文件测试
nginx -t -c /etc/nginx/nginx.conf

测试结果

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

信号控制

Nginx支持通过信号控制进程。

发送信号

bash
# 查找Master进程ID
ps aux | grep "master process"

# 发送信号
kill -s <SIGNAL> <MASTER_PID>

常用信号

信号命令说明
TERM, INTnginx -s stop快速停止
QUITnginx -s quit优雅停止
HUPnginx -s reload重载配置
USR1nginx -s reopen重新打开日志
USR2-平滑升级
WINCH-优雅关闭Worker进程

平滑升级

在不中断服务的情况下升级Nginx版本。

升级步骤

  1. 备份旧版本
bash
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
  1. 安装新版本
bash
# 下载并编译新版本
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure --prefix=/usr/local/nginx
make
sudo make install
  1. 发送USR2信号
bash
kill -s USR2 <MASTER_PID>
  1. 发送WINCH信号
bash
kill -s WINCH <OLD_MASTER_PID>
  1. 退出旧Master进程
bash
kill -s QUIT <OLD_MASTER_PID>

开机自启

使用systemctl

bash
# 启用开机自启
sudo systemctl enable nginx

# 禁用开机自启
sudo systemctl disable nginx

# 查看是否开机自启
sudo systemctl is-enabled nginx

使用chkconfig

bash
# 添加到服务列表
sudo chkconfig --add nginx

# 启用开机自启
sudo chkconfig nginx on

# 禁用开机自启
sudo chkconfig nginx off

使用rc.local

bash
# 编辑rc.local
sudo vim /etc/rc.local

# 添加启动命令
/usr/local/nginx/sbin/nginx

# 添加执行权限
sudo chmod +x /etc/rc.local

常见问题

端口被占用

bash
# 查找占用端口的进程
sudo netstat -tlnp | grep :80

# 停止占用端口的进程
sudo kill -9 <PID>

# 或修改Nginx监听端口

配置文件错误

bash
# 测试配置文件
nginx -t

# 查看错误日志
tail -f /var/log/nginx/error.log

权限问题

bash
# 检查运行用户
ps aux | grep nginx

# 修改目录权限
sudo chown -R nginx:nginx /var/www/html

无法启动

bash
# 查看错误日志
sudo tail -f /var/log/nginx/error.log

# 检查配置文件
sudo nginx -t

# 检查端口占用
sudo netstat -tlnp | grep :80

总结

Nginx启动与停止的关键操作:

  • 启动nginx startsystemctl start nginx
  • 停止nginx -s stopsystemctl stop nginx
  • 重启systemctl restart nginx
  • 重载nginx -s reload(推荐)
  • 测试nginx -t(重载前必做)

掌握这些操作,可以有效地管理Nginx服务。