Deploy Laravel Application on Shared Hosting (Hostgator)

laravel-deploy-shared-hosting

Deploy Laravel Application on Shared Hosting

In this tutorial, I will demonstrate the steps involved to deploy Laravel Application on Shared Hosting. I am demonstrating the steps on Hostgator.

The steps involved to Host Laravel Application on Shared Hosting will be pretty much same for any other Hosting as well.

Note: This tutorial will work well for Installing your Laravel Project to SiteGround, Bluehost, GoDaddy and other shared hosting servers.

Before moving forward make sure you have following Ready.

  • Shared Hosting Account Created (Sign Up for Hostgator)
  • You have Access to cPanel.
  • Laravel Application source code available via Git (GitHub / Bitbucket etc.) or any other SVN

Alright, Let’s dig into the steps to Deploy Laravel Application on Shared Hosting


 

PHP Version

Laravel 5.5 requires PHP version greater than 7.0. Hostgator comes with default of 5.4.

Login to your cPanel using credentials sent to your email on Hostgator account signup.

Choose  option Change PHP Version and change the current version to >7.1 

Note : You can ignore this step if your Laravel version requires PHP 5.4 or your hosing provider by defaults provides PHP v > 7.0 

Change PHP Version Hostgator for Laravel

 

SSH into Shared Hosting Box

As the first step, Open your terminal / command line and SSH into your Shared Hosting Server

Note: SSH instructions are usually included in the welcome email. Here is the general command to SSH into the Server

$ ssh [username]@[server-ip]    //Replace username with cPanel username and server-ip with Server IP Address.

Once it’s able to connect to the server, It will prompt you for the SSH password, which should be same as your cPanel account password

The home directory structure of shared server looks something like this.

Directory Structure hostgator user home

As you can see we have a directory public_html which is a public directory for keeping our projects. www is symlink to public_html. So basically, they both points to same stuff.


 

Clone Laravel Project

Let say in your hosting server (VPS, or shared hosting…whatever), you have current www/ directory, which is accessible publicly via web domain, for example:

/Users/username-blah/www/

Now, clone your project in a new directory, which contains all your application source code, at the same level as www/, for example:

/Users/username-blah/myLaravelProject/

You can use git clone command to clone your project. Something like

$ git clone https://username@bitbucket.org/username/myLaravelProject.git

 

Make public files available to www

Next step is to copy all contents inside the myLaravelProject/public directory to www directory. The easy example is that, with the fresh Laravel 5 installation application, the  myLaravelProject/public/index.php should be copied to the www/index.php

Makes Sense?

Basically you need to run this command, on your server home directory (/home/username-blah)

$ cp -a myLaravelProject/public/. www/

Now, your www should have following contents

Hostgator www directory structure

Note : Make sure you copy the public/.htaccess to the www/ also. Don’t forget this, it is very important for your app routes. Without it, your routes won’t be working and there will be a blank or empty page in every route.


 

Modify www/index.php to reflect new Structure

Now let’s modify the www/index.php to reflect the new structure. Don’t modify the project/public/index.php, okay? Only modify www/index.php, remember this!

Find the following line

require __DIR__.’/../bootstrap/autoload.php’;


$app = require_once __DIR__.’/../bootstrap/app.php’;

 

And update them to the correct paths as following

require __DIR__.’/../myLaravelProject/bootstrap/autoload.php’;

$app = require_once __DIR__.’/../myLaravelProject/bootstrap/app.php’;

 

We are almost there, just couple of more steps !


 

Modify Directory Permissions

It’s time to modify directory permissions of storage and bootstrap/cache directory so that it’s writable.

Move to your Laravel Project into the terminal and run the following commands.

$ sudo chmod -R 777 storage

$ sudo chmod -R 777 bootstrap/cache

.env Configuration File

There is an .env file in your project which contains all the project variables dedicated to the environment in which the project is working.

Since our .env file should not be committed to your application’s source control. Your application should contain a .env.example file available. You can go ahead and copy the same file and name it .env

Generate a new application encyption key

$ php artisan key:generate

Note: Make sure to clear cache via artisan command after generating the new key file (php artisan cache:clear)

Modify the .env file for other required parameters such as database variables, Mail Setting Variables. etc.

 

There you go !!! Everything should work now


 

Composer

Hostgator comes with composer (dependency manager tool) already installed and most other shared hosting server’s comes with composer installed.

Check by running composer command on your terminal.

If not, you can easily install composer with following commands.

$ cd /User/username-blah/project/

$ curl -sS https://getcomposer.org/installer | php — –filename=composer

Now you can execute composer to manage dependencies.

$ php composer install

$ php composer dumpautoload -o

$ php artisan config:cache

$ php artisan route:cache

 

That’s it ! Have fun with deployed Laravel Project.

4 comments On Deploy Laravel Application on Shared Hosting (Hostgator)

  • Very nice clear article. Unfortunately mine still doesn’t work. Everything proceeds fine until I get to “php artisan key:generate”. Output is “Parse error: syntax error, unexpected ‘?’ in /home1/sl1k7f3j/public_html/medaverter/bootstrap/app.php on line 15”. Get the same error if I run “php artisan config:cache” or “php artisan route:cache”. If I try to go to the web page in the browser, get error “Fatal error: Uncaught Error: Class ‘Illuminate\Foundation\Application’ not found in /home1/sl1k7f3j/public_html/medaverter/bootstrap/app.php:14 Stack trace: #0 /home1/sl1k7f3j/public_html/medaverter/index.php(38): require_once() #1 {main} thrown in /home1/sl1k7f3j/public_html/medaverter/bootstrap/app.php on line 14” I’m about ready to throw in the towel on laravel/homestead

  • I was wondering if you could explain the same except for hosts that have neither public_html or www in root. Also, if I am running my Laravel project in a sub-directory that also houses other demo sites. Thank you in advance.

  • Shared hosting is affordable, but they are not a good option for hosting a Laravel app. A better alternative is a vps. They are affordable and faster than shared. If someone doesn’t know how to setup a vps, they can use PaaS, like Cloudways Laravel hosting for that. All you have to do is just select the server size and storage and datacenter location, the platform will setup the server automatically for you. OS, stack, Laravel and workflow tools, like Git and Composer will be already installed on the server.

  • Thank you for this nice tutorial.
    However there is one important thing is missing and that is setting up the queue:work, so that laravel queued get executed.
    Do you have any idea how to do this effectively on shared host with jailed access only.

Leave a reply:

Your email address will not be published.

Site Footer