How to change public folder to public_html in laravel 8? [duplicate]
Asked Answered
J

6

6

I wanna deploy my application on shared hosting on Cpanel where the primary document root has public_html but Laravel project public

Jotter answered 30/4, 2021 at 5:25 Comment(2)
i would recommend to use this method in cpanel #64333928Fetishist
Renaming public folder to public_html worked for me without any additional changes.Mauro
J
8

You have to follow 2 steps to change your application's public folder to public_html then your can deploy it or anything you can do :)

  1. Edit \App\Providers\AppServiceProvider register() method & add this code .

     // set the public path to this directory
     $this->app->bind('path.public', function() {
         return base_path().'/public_html';
     });
    
  2. Open server.php you can see this code

    if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
       return false;
    }
    
    require_once __DIR__.'/public/index.php';
    

Just Replace it with :

  if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
   return false;
  }

  require_once __DIR__.'/public_html/index.php';

Then serve your application with php artisan serve, you also can deploy it on your Cpanel shared hosting where primary document root public_html

Jotter answered 30/4, 2021 at 5:25 Comment(2)
After this change,I still get a 404 when opening the site. What could be the reason for this?Bonina
I am unable to find server.php, only one within vendor which shouldn't be changed.Patronymic
K
7

You can rename your public directory to whatever you want and tell Laravel to use the current directory path as the public path. Just navigate to the index.php inside the public folder (or whatever you renamed it to) and add the following code after the $app definition:

$app = require_once __DIR__.'/../bootstrap/app.php';

/* *** Add this code: *** */
$app->bind('path.public', function() {
    return __DIR__;
});
/* ********************** */
Katinakatine answered 15/7, 2021 at 6:6 Comment(2)
this one's cleaner for me ^_^Whoever
For shared hosting like godaddy.com , this works smoothly. You just got upvoted.Trough
A
6

I came across this article that explains how to go about it. In a nutshell, go to public_html/index.php and add $app->bind('path.public', function() { return __DIR__; }); just after $app is created.

To fix the path for scripts used in CLI mode or Artisan script, you should add the code below to the file /bootstrap/app.php

$app->bind('path.public', function() { return base_path().'/public_html'; });

Make sure you add this line after $app is created.

UPDATE In laravel 10 you should instead update your code to invoke the usePublicPathhave method offered by the Illuminate\Foundation\Application object.Add this line to your public_html/index.php file

app()->usePublicPath(__DIR__);

You can view details of the update here

Abirritate answered 6/4, 2023 at 7:39 Comment(1)
You saved me by the update for laravel 10 :)))Reservoir
U
3

there, idk if you have same problem like me, my cases is i using shared hosting, and i deploy it in my main domain. i place all my files in the root, my problem is the storage:link keep between public, not public_html (because its default by the hosting) so what i need to do i changhe the link using this code :

Before :

'links' => [
        public_path('storage') => storage_path('app/public'),
    ],

After :

'links' => [
    app()->basePath('public_html/storage') => storage_path('app/public'),
],

I hope it can help few people :)

Unesco answered 16/11, 2021 at 12:0 Comment(0)
P
0

Just Rename the "public" folder to "public_html" and it will work. No changes are required in the code. Tested in Laravel 8.

Pteridophyte answered 3/10, 2021 at 10:21 Comment(0)
C
0

my two cents :) What helped to me was:

Open LaravelService/vendor/laravel/framework/src/Illuminate/Foundation/Application.php and change publicPath() method to return public_html.

public function publicPath()
{
    return $this->basePath.DIRECTORY_SEPARATOR.'public_html';
}

Then if you are using webpack also change the output folder:

const output = 'public_html';

mix.ts('resources/js/web/App.ts', output + '/js/web').setPublicPath(output).react();

This helped to me. Only issue is that it is probably not recommended to change Application.php as it is part of Laravel framework and after updating it, it will be probably erased so you have to put it back.

Cocks answered 2/2, 2023 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.