Laravel 5.5 On Shared Hosting

Published in Laravel, Development on Sep 19, 2017

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</code></pre>

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.

Now 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.