Appearance
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重载原理
- Master进程检查新配置
- 如果配置正确,启动新的Worker进程
- 旧的Worker进程继续处理现有连接
- 新的Worker进程处理新连接
- 旧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, INT | nginx -s stop | 快速停止 |
| QUIT | nginx -s quit | 优雅停止 |
| HUP | nginx -s reload | 重载配置 |
| USR1 | nginx -s reopen | 重新打开日志 |
| USR2 | - | 平滑升级 |
| WINCH | - | 优雅关闭Worker进程 |
平滑升级
在不中断服务的情况下升级Nginx版本。
升级步骤
- 备份旧版本
bash
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old- 安装新版本
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- 发送USR2信号
bash
kill -s USR2 <MASTER_PID>- 发送WINCH信号
bash
kill -s WINCH <OLD_MASTER_PID>- 退出旧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 start或systemctl start nginx - 停止:
nginx -s stop或systemctl stop nginx - 重启:
systemctl restart nginx - 重载:
nginx -s reload(推荐) - 测试:
nginx -t(重载前必做)
掌握这些操作,可以有效地管理Nginx服务。