How to solve Error 404 when deploying laravel project to Cpanel?
Asked Answered
S

3

2

I'm trying to deploy my laravel (Laravel Framework 7.28.3) to Cpanel, but got a 404 error. I uploaded my project into /public_html, modified the index.php file to point to the correct files (as below). I think there must be some mistake in the index.php file but couldn't figure it out. This is my first time asking my question (after searching for it several times), so hope that I will get the answer!

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

this is my file structure in File Manager:

file structure in File Manager

Sylphid answered 13/10, 2020 at 10:27 Comment(8)
check your .htaccess file in public folder, if it was not there then upload it.Soelch
as per your file structure it will work dmaiin.com/public will be the url you need to change your folder structureDumb
checked, it's still inside the public folderSylphid
what si your url .? i can show you thisDumb
my url is emglabvn.com.vn,Sylphid
i created test.html file inside public folder, and somehow it worked when i go to emglabvn.com.vn/test.htmlSylphid
do you have permission to point the root directory for a domain??Heterocercal
You can access with example.com/public ?Adigun
D
3

Update (2023) easy way

if you have access to terminal you can run this command

ln -s /laravel/public/* public_html

This is easy way i found in 2023


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
Dumb answered 13/10, 2020 at 10:46 Comment(1)
Comments are not for extended discussion; this conversation has been moved to chat.Tayler
G
3

In your public/.htaccess file replaces the code given below..make sure that you uploaded your project in the root public_html folder.

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

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
Gilda answered 16/7, 2022 at 10:41 Comment(1)
provide detailed solution for problem, so other user can understand what going on in each line or why we use it.Mclemore
G
1

You need to make sure your application is in a folder outside of your public_html.

Then you need to make a symbolic link to everything in your public directory inside your application. This symbolic link should be placed in your public_html.

This way your business logic is not available from the outside, only from your own application.

Actually it is advisable to clone your application using git and then install it following the steps in de docs. (https://laravel.com/docs/7.x/installation)

Gusta answered 13/10, 2020 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.