如何无需localhost:8000及php artisan serve运行Laravel 5.5项目?
lms_manager via localhost/lms_manager Got it, let's get your Laravel project lms_manager accessible through localhost/lms_manager instead of relying on php artisan serve or the default localhost:8000 port. I’ll walk you through setup steps for both Apache and Nginx—the two most common web servers.
Apache Configuration
If you’re using XAMPP, WAMP, or a standard Apache setup:
Move your project to Apache’s root directory
- For XAMPP: Copy the
lms_managerfolder intohtdocs - For WAMP: Drop it into
www - For Linux (Ubuntu/Debian): Place it in
/var/www/html
- For XAMPP: Copy the
Enable mod_rewrite (critical for Laravel routing)
- Open Apache’s
httpd.conffile (location varies: XAMPP/WAMP have it in their Apache config folders; Linux uses/etc/apache2/apache2.conf) - Find the line
#LoadModule rewrite_module modules/mod_rewrite.soand remove the#to uncomment it - On Linux, run
sudo a2enmod rewriteto enable the module, then restart Apache withsudo systemctl restart apache2
- Open Apache’s
Allow .htaccess overrides
- In the same
httpd.conffile, locate the<Directory>block for your root folder (e.g.,<Directory "C:/xampp/htdocs">or<Directory /var/www/html>) - Change
AllowOverride NonetoAllowOverride All—this lets Laravel’s.htaccessfile handle routing
- In the same
Route traffic to the
publicfolder (no need forlocalhost/lms_manager/public)- Create a new
.htaccessfile in the root of yourlms_managerfolder (not insidepublic) with this content:<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule> - This redirects all requests for
localhost/lms_managerto the project’spublicdirectory (Laravel’s actual entry point)
- Create a new
Fix file permissions (Linux only)
- Run these commands to give Apache write access to critical Laravel folders:
sudo chown -R www-data:www-data /var/www/html/lms_manager/storage sudo chown -R www-data:www-data /var/www/html/lms_manager/bootstrap/cache sudo chmod -R 775 /var/www/html/lms_manager/storage sudo chmod -R 775 /var/www/html/lms_manager/bootstrap/cache
- Run these commands to give Apache write access to critical Laravel folders:
Restart Apache
- XAMPP/WAMP: Use the control panel to restart Apache
- Linux:
sudo systemctl restart apache2
Nginx Configuration
If you’re using Nginx (common on Linux servers or local setups):
Move your project to Nginx’s root directory
- Linux: Place
lms_managerin/usr/share/nginx/html - Windows: Drop it into
nginx/html
- Linux: Place
Update Nginx’s site configuration
- Open your Nginx config file (Linux:
/etc/nginx/sites-available/default; Windows:conf/nginx.conf) - Add this
locationblock inside the existingserverblock:location /lms_manager { alias /path/to/your/lms_manager/public; # Replace with your actual path, e.g., /usr/share/nginx/html/lms_manager/public try_files $uri $uri/ /lms_manager/index.php?$query_string; # Handle PHP files location ~ \.php$ { fastcgi_pass unix:/run/php/php8.2-fpm.sock; # Adjust PHP version (e.g., php8.1-fpm.sock) # For Windows: use fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } }
- Open your Nginx config file (Linux:
Test and restart Nginx
- Linux: Run
sudo nginx -tto check for config errors, thensudo systemctl restart nginx - Windows: Use the command prompt to restart Nginx (e.g.,
nginx -s reload)
- Linux: Run
Fix permissions (Linux only)
- Same as the Apache steps: grant write access to
storageandbootstrap/cachefolders using thechownandchmodcommands
- Same as the Apache steps: grant write access to
Final Check
- Open your Laravel project’s
.envfile and setAPP_URL=http://localhost/lms_manager—this ensures all generated links (like assets or routes) point to the correct URL - Now visit
http://localhost/lms_managerin your browser—it should load your project without issues!
If you hit snags with permissions, config syntax, or routing, feel free to share details—I’m happy to troubleshoot further.
内容的提问来源于stack exchange,提问作者Harsha

