Laravel 5.5 On Shared Hosting

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.

17 thoughts on “Laravel 5.5 On Shared Hosting

  • ErrorException (E_WARNING)
    file_put_contents(/Library/WebServer/Documents/erpb/storage/framework/sessions/UFZYj69x06XF7cazCkRgiWbUBluF5eF1UeRm6JJM): failed to open stream: Permission denied

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

  • 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

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

Leave a Reply

Your email address will not be published. Required fields are marked *