配置Laravel项目:将public/index.php设为根路径‘/’的部署问题
public/index.php Hey there! Let's get your Laravel site up and running properly. The core issue here is that your web server isn't set to use the public directory as the site root—this is a super common hiccup for first-time Laravel deployments, so we'll sort it out in no time.
Option 1: Update Your Web Server's Document Root (Recommended)
This is the cleanest approach, as it follows Laravel's intended structure. The steps vary a bit depending on your server setup:
For cPanel Users
- Log into your cPanel dashboard and navigate to Domains > Domains (or similar, depending on your host's interface).
- Find your domain, click Manage, then look for the Document Root setting.
- Change the document root path to point to your Laravel project's
publicfolder. For example, if your project is inpublic_html/my-laravel-app, set it topublic_html/my-laravel-app/public. - Save the changes and refresh your site.
For Manual Apache Configuration
- Open your Apache virtual host config file (usually located at
/etc/apache2/sites-available/your-domain.confor similar). - Update the
DocumentRootand<Directory>directives to target thepublicdirectory:<VirtualHost *:80> ServerName your-domain.com ServerAlias www.your-domain.com DocumentRoot /var/www/your-laravel-project/public <Directory /var/www/your-laravel-project/public> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/your-domain-error.log CustomLog ${APACHE_LOG_DIR}/your-domain-access.log combined </VirtualHost> - Save the file, restart Apache (
sudo systemctl restart apache2), and test your site.
Option 2: Use a Root .htaccess Redirect (If You Can't Modify Document Root)
If you're on a shared host that restricts changing the document root, you can add/modify the .htaccess file in your project's root directory to redirect all traffic to the public folder. Replace your existing root .htaccess with this:
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule>
This rule forwards every incoming request to the public directory, effectively making it act as the site root.
Verify the Public Directory's .htaccess
Make sure the .htaccess file inside your public directory is the default Laravel one (it should be included with the project, but sometimes gets lost during deployment). Here's what it should look like:
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] # Send Requests To Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
Quick Permission Check
Don't forget to ensure your Laravel storage and cache directories have proper write permissions. Run these commands from your project root:
chmod -R 755 storage bootstrap/cache chown -R www-data:www-data storage bootstrap/cache # For Apache on Debian/Ubuntu
That should do it! Refresh your site, and you should see the default Laravel welcome page (or your custom application) now.
内容的提问来源于stack exchange,提问作者Anna Jeanine

