Deploying Laravel on Shared Hosting

Deploying Laravel on Shared Hosting

There are many guides available on the internet concerning deploying Laravel on shared hosting, but they are not very precise or out of date in respect of the latest changes in Laravel.

The good news is it’s very easy to publish Laravel based sites on cPanel hosting or any other type of shared hosting.

Even better is that we are going to show you how to install Laravel on shared hosting. So let’s dive in and have a look at how we can do that.

Backups

First thing first, make sure you backup your existing site source if you have one before pushing your locally tested Laravel application live. If it all goes wrong rollback.

Server Environment

It’s most likely that your shared hosting provider will not give you necessary permissions, command line access or freedom to deploy your Laravel app.

So we are going to assume that command line tools line Composer, Node or NPM are not available to compile your assets or install composer packages.

This means you have to compile all your assets locally and install all composer packages locally still you will be able to FTP in using an FTP Client and make changes to PHP source code or Blade templates. As shared hosting is a low-cost option so you should expect show restrictions.

Start Here

Let’s assume you are working in a LAMP environment locally running Apache Web Server. We are going to work with your ready to go live application copy as we will need to rearrange folders and files and make some changes to source files.

Before proceeding, let do some application clean up. Run below command (We recommend you to use Git Bash for running below commands.)

php artisan cache:clean
php artisan view:clear
rm -rf storage/sessions

Create a copy of .env file with named .env.production with the local development configs (at this stage you probably better off change all config values to shared hosting ones in this file like a database). Copy the whole application in a new folder named deploy.

Exclude the node_modules folder as its very huge and you won’t need it on the server. If you want a cleaner code base on the server then you can remove composer.json, webpack.mix.js, yarn.lock, composer.lock, gulpfile.js etc.

Earlier we created a file .env.production which should contain shared hosting configs. Let’s make this default environment within deploy folder.

cd deploy
rm .env
mv .env.production .env

Change Laravel Paths

As on your shared hosting public_html is your public directory from where your website will be loaded.

Therefore within deploy folder copy all files and folders from public folder to deploy and delete your public folder. So all content of public folder like index.php , CSS folder , js folder etc. will be copied directly into deploy folder.

With that in mind, we need to change Laravel paths.

In /deploy, edit the file index.php and make the following changes.

Change:
require __DIR__.'/../bootstrap/autoload.php';
to:
require __DIR__.'/bootstrap/autoload.php';
Change:
$app = require_once __DIR__.'/../bootstrap/app.php';
to:
$app = require_once __DIR__.'/bootstrap/app.php';

Move to Server

Finally, we need to upload the whole folder to the shared hosting server using an FTP Client. Make sure you upload the whole folder to root folder.

Final Step

If your Laravel application use a database, export your database using phpMyAdmin and import it back in your cpanel phpMyAdmin.

Ensure your production .env file has the correct configuration/credentials for accessing MySQL on the hosting server.

Point your browser at your domain and vola your Laravel application will appear.

8 comments on “Deploying Laravel on Shared Hosting

  1. Good tutorial. Can you with deploying to shared hosting with SSL. Which files will be edited and what will the details look like?
    Thanks in advance

  2. In /deploy, edit the file index.php and make the following changes.

    should probably be

    In /deploy/public, edit the file index.php and make the following changes.

  3. Just wanted to say a big thank you for this guide, it is really simple, gave me hope and helped me a lot.
    The other guides I’ve found are using some weird and very complicated methods, and even advising other hostings/VPS, but with your guide I managed to make my first Laravel training project live on my cheapest ovh Perso plan that doesn’t support SSH – and even in a subfolder of my main blog!

    If someone also wants to do such thing (putting in a subfolder, instead of the main folder), remember about using a helper method route() for generating proper url addresses in your links. (check the documentation for route() and route naming )

    If someone also uses ovh Perso a tip: it might be easier for you to use a SQLite database instead of MySQL for showing your test Laravel projects live.

  4. this doesnt work for laravel 5.8
    what should come here

    require __DIR__.’/../vendor/autoload.php’;
    $app = require_once __DIR__.’/../bootstrap/app.php’;

    1. the issue is with the trailing dots
      change from :
      require __DIR__.’/../vendor/autoload.php’;
      $app = require_once __DIR__.’/../bootstrap/app.php’;

      to :
      require __DIR__.’/NAMEFOLDER OF YOUR PROJECT/vendor/autoload.php’;
      $app = require_once __DIR__.’/NAME FOLDER OF YOUR PROJECT/bootstrap/app.php’;

Leave a Reply

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

*

When sharing a code snippet please wrap you code with pre tag and add a class code-block to it like below.
<pre class="code-block">you code here</pre>

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Your little help will keep this site alive and help us to produce quality content for you.