Laravel新项目首次运行正常,二次运行出现文件无法打开报错
Hey there, let's figure out why your Laravel project is throwing that missing server.php error after working fine the first time. I've run into this a few times myself, so here are the go-to fixes that usually resolve it:
Check if
server.phpactually exists in your project root
Accidental deletions happen to the best of us! Head to your project folder (D:[path]as noted in your error) and double-check ifserver.phpis present. If it's gone, you have two quick fixes:- Create a new
server.phpfile in the root and paste this default Laravel content:<?php $uri = urldecode( parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ); // Emulate Apache's "mod_rewrite" for the built-in PHP server if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) { return false; } require_once __DIR__.'/public/index.php'; - Grab a fresh copy of
server.phpfrom a new Laravel project (created withlaravel new temp-project) and copy it into your existing project root.
- Create a new
Verify your server's document root is set correctly
Since your error mentions XAMPP, make sure your Apache virtual host or local server setup points to thepublicfolder of your Laravel project—not the root folder. Theserver.phpfile is only needed for the built-in PHP server (php artisan serve); pointing directly topublicavoids relying on it entirely and is the recommended setup for Apache/Nginx.Fix Windows file permissions (if applicable)
Windows can sometimes mess up file permissions after the first run. Right-click your project folder, go to Properties > Security, and ensure your user account has full read/write access to all files and subfolders.Clear Laravel's cache (just in case)
Odd behavior can sometimes stem from cached configs. Open your terminal, navigate to your project root, and run these commands:php artisan cache:clear php artisan config:clearMake sure you're running the server from the right directory
If you're usingphp artisan serve, double-check that you're executing the command from the root folder of your Laravel project—not thepublicfolder or any subdirectory. Running it from the wrong place will make PHP look forserver.phpin the wrong location.
If none of these work, check if your project path has special characters or spaces (Windows hates those!). For example, D:\My Cool Laravel Project might cause issues—try renaming it to D:\MyCoolLaravelProject if that's the case.
内容的提问来源于stack exchange,提问作者MrTindervox

