在 Debian 上搭建 LNMP(Linux + Nginx + MySQL + PHP) 环境时,访问页面出现 404,通常不是 PHP 本身的问题,而是 Nginx 配置或路径不对。下面按最常见原因逐一排查。
你访问的 URL 在服务器上根本不存在。
检查:
ls -l /var/www/html/
Nginx 配置示例(正确):
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
✅ 确认:
root 指向真实存在的目录index.php)URL 是:
http://ip/index.php
结果 404
原因:Nginx 没有 PHP 解析配置
✅ 必须有这一段:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
⚠️ 注意:
检查:
systemctl status php8.2-fpm
nginx -t
systemctl reload nginx
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
例如 Laravel / WordPress:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
少了这句,除首页外全是 404
nginx -T | grep server_name
确保你访问的域名/IP 命中正确 server
# 1. 文件存在?
ls /var/www/html/index.php
# 2. Nginx 配置正确?
nginx -t
# 3. PHP-FPM 在跑?
systemctl status php8.2-fpm
# 4. 错误日志
tail -n 50 /var/log/nginx/error.log
你可以直接贴:
nginx -T 输出我可以一行一行帮你改到不 404。