I already wrote on how to deploy a Laravel 4 app on a shared server that doesn’t allow the structure that requires a public folder, and the rest of the application outside that folder. Let’s not get into reasons why you can’t pick a better hosting provider, sometimes you just have no choice 😉
Since a long time has passed and Laravel 5.5 is out, let’s give this another try. So let’s say you’re on a shared hosting server and you path looks something like this:
/home/site-name/public_html
Turns out the setup is really simple. Just copy your entire application into that public_html directory. You’ll get a structure looking something like this:
/home/site-name/public_html/app
/home/site-name/public_html/bootstrap
/home/site-name/public_html/config
/home/site-name/public_html/database
/home/site-name/public_html/public
/home/site-name/public_html/resources
/home/site-name/public_html/routes
etc.
No move all the contents from the directory: /home/site-name/public_html/public
And paste it into: /home/site-name/public_html
After that you can delete the directory: /home/site-name/public_html/public
Now just edit the file: /home/site-name/public_html/index.php
Change this:
require __DIR__.'/../vendor/autoload.php';
To this:
require __DIR__.'/vendor/autoload.php';
And also change this:
$app = require_once __DIR__.'/../bootstrap/app.php';
To this:
$app = require_once __DIR__.'/bootstrap/app.php';
Additional steps
Your JS and CSS assets will still be published to the public directory, which I personally don’t like. So to tackle this we need to make some small changes to our webpack.mix.js file. You need to add the following line of code in there:
mix.setPublicPath('dist/');
Just name the directory whatever you like, I just prefer using dist.
Also, you need to change the defaults from:
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
To:
mix.js('resources/assets/js/app.js', 'js')
.sass('resources/assets/sass/app.scss', 'css');
The last step is updating your calls to the the asset() helper. You might have something like this in your views or layouts:
{{ asset('css/app.css') }}
You need to change it to:
{{ asset('dist/css/app.css') }}
Since now the assets are published to the dist directory.
That’s it. I hope you managed to follow these simple steps, and if you have any problems simply leave a comment below.
Really simple and to the point tutorial. Thanks for sharing.
ErrorException (E_WARNING)
file_put_contents(/Library/WebServer/Documents/erpb/storage/framework/sessions/UFZYj69x06XF7cazCkRgiWbUBluF5eF1UeRm6JJM): failed to open stream: Permission denied
Check your permissions.
By using this method some files may be publicly accessible so be careful to check your server config. Try a simple thing like accessing a file from config folder http://www.example.com/config/app.php and if you see the content of that file then you have a problem.
It would be safer to put the project files and folders up one level from public_html (in the same dir as public_html) in a directory called laravel and then copy all the files from public to public_html then update the index.php to:
require __DIR__.’/../laravel/vendor/autoload.php’;
$app = require_once __DIR__.’/../laravel/bootstrap/app.php’;
That’s a good point. I tried making this as simple as possible, but I agree putting the project files 1 level up, or event putting them in a separate protected folder would be much better. I just might update the tutorial.
I simply use a subdomain and point the folder location to the public folder. The allows me to continue with the same work flow as my local machine.
or you can use .htaccess and not have to change anything!
I think it’s better to put app files in a parent folder of public_html and keep public_html the same as public.
One of shared hostings which I’m using had the same directory structure:
/home/my_user_name/some-site.dom/public_html
So I’m just:
– removing initial dir “public_html”
– installing laravel to “/home/my_user_name/some-site.dom”
– making the symbolic link with name “public_html” to “/home/my_user_name/some-site.dom/public”. It can be done through ssh or trough Filezilla.
– adding “public_html” to .gitignore
So I don’t need to change some framework’s files for such hostings
Nice, but an option on every hosting provider
Which hostings allows to run Laravel but doesn’t allows to make a symbolic links?
Lots of them you havent even heard of 😉
I did everything like you said, but i get blank page, any ideas how to fix it?
Not without any details 😉
I wouldn’t recommend shared hosting for your Laravel apps. I would recommend cloud or vps hosting. If you don’t know how to setup one, then used PaaS, like Cloudways to do it. DigitalOcean and Linode are not that expensive and they are lot better than shared servers.
That was really not the point of this article 😉
I did as you said and here is what I got?
Parse error: syntax error, unexpected ‘?’ in /home/maxwellimoveis/public_html/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 233
Any ideas
Check your PHP version
Hey thanks for the tutorial. But only thing is I’m getting the whoops message without any errors on the page. Here’s my index.php
require __DIR__.’/../watchshop/vendor/autoload.php’;
$app = require_once __DIR__.’/../watchshop/bootstrap/app.php’;
..and my route to my home page of my project ‘watchshop’.
/ ===== HOME ‘/’
Route::get(‘/’, function () {
return view(‘home/index’);
})->name(‘home’);
http://www.keithcarrillo.com is where the page is with the whoops generic message.
Turn on debugging and let me know which error are you getting. Can’t really help you without any details