You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置Laravel项目:将public/index.php设为根路径‘/’的部署问题

Fixing Laravel Deployment: Pointing Your Site to 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.

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 public folder. For example, if your project is in public_html/my-laravel-app, set it to public_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.conf or similar).
  • Update the DocumentRoot and <Directory> directives to target the public directory:
    <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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:23:47