在 Ubuntu 系统中关闭 Nginx 服务有以下几种方法,根据需求选择适合的方式:
适用于 Ubuntu 16.04+(使用 systemd
的系统):
# 停止 Nginx 服务
sudo systemctl stop nginx
# 禁止开机自启(可选)
sudo systemctl disable nginx
# 验证状态
sudo systemctl status nginx
输出中应显示 inactive (dead)
或 disabled
。
sudo service nginx stop
如果上述命令无效,强制终止所有 Nginx 进程:
# 查找 Nginx 主进程
ps aux | grep nginx
# 强制终止(根据上一步输出的进程号)
sudo kill -9 <PID>
# 或终止所有 Nginx 进程
sudo pkill -9 nginx
# 检查端口占用(80/443 应无 Nginx 监听)
sudo netstat -tulnp | grep nginx
# 或尝试访问本地服务
curl -I http://localhost
若返回 Connection refused
或无法访问,则说明已关闭。
bash
sudo nginx -t
bash
sudo systemctl start nginx
根据你的系统版本和权限选择对应方法即可。