How to Solve Error 404 When Deploying Laravel Project to Cpanel

How to solve Error 404 when deploying laravel project to Cpanel?

Deploy laravel application in Cpanel

  • Setup 1 : - upload file to Cpanel the root directory – not the public_html.

  • Setup 2 : - Open the that folder and MOVE the CONTENTS of the public folder to your cpanel’s public_html .

  • Setup 3 : - Navigate to the public_html folder and locate the index.php file. Right click on it and select Code Editor from the menu.

and change this line

require __DIR__.'/../folderName/vendor/autoload.php';
$app = require_once __DIR__.'/../folderName/bootstrap/app.php';

NOTE : - folderName here is in root where you laravel application stay

that's it now all your request will come inside public_html folder index.php and this file will include require __DIR__.'/../folderName/vendor/autoload.php; and run laravel application



Folder structure will look like

/laravel
/public_html/index.php

indside index.php

require __DIR__.'/../laravel/vendor/autoload.php';;
$app = require_once __DIR__.'/../laravel/bootstrap/app.php'; // here laravel is folder name

Laravel Deployment on Shared Hosting - 404 Error

Deploying Laravel app in cPanel is quite simple(if you are deploying on add-on domain).

In cPanel, go on add-on domains and then create a new add-on domain.

By default cPanel generates document root for you in this manner:

public_html/mydomain.com

Change it to:

public_html/mydomain.com/MyLaravelApp/public

Now upload your Laravel project under public_html/mydomain.com directory.

It should look like this.

Sample Image

If you have already an add-on domain. Go to Modify add-on domain (just below Create an Addon Domain)

Click edit icon in document root column and change your domain's document root.

Sample Image

Error 404 in a shared hosting in Laravel 8

cPanel use Apache as Web server and Apache needs to know how to handle URL rewrites which Laravel requires. To achieve this Laravel comes with a .htaccess file. Here is the latest version from Laravel source code.

<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
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

You may not have been able to copy the .htaccess file from your application's public directory to public_html directory while copying the files.

To solve this you can create a .htacess file with specified content.

Each route is showing 404 message on shared hosting

Eisenheim, this is htaccess issue: get one .htaccess file in root folder of your web-project.

And put the following code inside it,

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Then try without index.php it should work perfectly.



Related Topics



Leave a reply



Submit