在 Debian 上部署 LNMP(Linux + Nginx + MySQL + PHP) 时出现 404 错误,通常不是 PHP 本身的问题,而是 Nginx 配置或站点路径不对。下面按最常见原因逐一排查。
404 表示:请求的文件在服务器上不存在
检查你的站点配置(如 /etc/nginx/sites-enabled/default):
server {
listen 80;
server_name yourdomain.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
}
✅ 确认:
/var/www/html 目录存在index.php测试:
ls -l /var/www/html
如果你访问的是:
http://ip/index.php
✅ 能访问
但访问:
http://ip/user/1
❌ 404
这是 try_files 没配置 PHP 路由
✅ 正确 PHP 配置示例:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
⚠️ 注意:
systemctl status php8.2-fpm
如果没运行:
systemctl start php8.2-fpm
systemctl enable php8.2-fpm
例如:
php8.2-fpm.sockphp8.1检查:
ls /run/php/
nginx -t
systemctl reload nginx
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
nginx -T | grep server_name
确认你访问的域名或 IP 命中了正确 server 块。
请依次确认:
你可以直接贴出:
nginx -T 配置我可以直接帮你改好配置。