I wanna deploy my application on shared hosting on Cpanel where the primary document root has public_html
but Laravel project public
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 :)
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'; });
Open
server.php
you can see this codeif ($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
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__;
});
/* ********************** */
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
update for laravel 10
:))) –
Reservoir 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 :)
Just Rename the "public" folder to "public_html" and it will work. No changes are required in the code. Tested in Laravel 8.
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.
© 2022 - 2024 — McMap. All rights reserved.