Linux 下 Nginx 启动失败的原因非常多,下面按最常见 → 较少见的顺序给你梳理,并给出排查方法。
启动失败时,一定先看错误信息:
sudo systemctl status nginx
或:
sudo nginx -t
或:
journalctl -xe
错误示例:
Address already in use
原因:
解决:
sudo netstat -tunlp | grep :80
sudo lsof -i:80
sudo systemctl stop apache2
sudo systemctl stop nginx
错误示例:
nginx: [emerg] unknown directive
解决:
sudo nginx -t
根据提示修改 /etc/nginx/nginx.conf 或 sites-enabled/*
常见于:
include 的路径不存在root 指向不存在的目录示例:
open() "/var/www/html" failed
解决: 确认目录存在:
ls /var/www/html
错误示例:
Permission denied
常见原因:
解决:
sudo chown -R www-data:www-data /var/www
sudo chmod -R 755 /var/www
错误示例:
SSL_CTX_use_certificate_chain_file failed
原因:
解决:
sudo nginx -t
ls -l /etc/nginx/ssl/
nginx -v
若无输出:
sudo apt install nginx
# 或
sudo yum install nginx
sudo systemctl daemon-reexec
sudo systemctl restart nginx
sudo nginx -t
sudo systemctl status nginx
sudo netstat -tunlp | grep nginx
sudo journalctl -xe
你可以直接把以下信息发我,我可以精准定位:
nginx -t 的输出systemctl status nginx 的输出我可以一步一步帮你修 ✅