Laravel 6.0 on Shared Hosting

Emeka Mbah
4 min readDec 22, 2019

Hello Artisans :)
Today I wish to share with you how to host your Laravel Application on shared hosting.

Yeah, you probably know VPS is highly recommended for Laravel applications because of numerous features they offer which includes reliability, flexibility capability, high performance and most importantly, security.

Wait a minute…
What if I have a very small Laravel application and do not wish to purchase a VPS. The cheapest VPS goes for about $4 monthly or higher depending on usage.
Good news! Laravel application can successfully run on shared host and I am going to walk you through the setup. We are using Cpanel which is used by most shared hosting, if you are not using Cpanel this should also serve as a guide.

Lets assume we are done developing our Laravel application called resume and we are ready to go live!

  1. Clear Laravel cache and views
    Since most shared hosting do not offer shell access, we are going to run the following artisan commands in the root of our application, resume, on our development server
php artisan view:clear
php artisan cache:clear

2. Upload files to shared hosting
The content of our resume application could look like the screenshot below

Laravel application files and directories

If you are not using an FTP client, you could compress resume to resume.zip, then use CPanel > File Manager to upload to your hosting server, then upzip to root of your hosting account.
Lets assume the root of our hosting account is /home/crazyin/

We are going to upload our resume application to shared hosting /home/crazyin/resume

Next, we move the contents of /home/crazyin/resume/public to /home/crazyin/public_html
Our /home/crazyin/public_html should look similar to screenshot below:

3. Create .htaccess in /home/crazyin/public_html
In order to make our application to use pretty urls and obey Laravel routes, we should create a .htaccess file in /home/crazyin/public_html

Hidden files are not showed by default in Cpanel File manager so be sure to select Show Hidden Files under settings

Our .htaccess should contain the following simple snippet, you are free to add more rules if you understand .htaccess

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^.*$ ./index.php

4. Laravel .env file

Ensure you have a .env with valid APP_KEY in /home/crazyin/resume

5. Set permissions in storage and bootstrap/cache
Laravel usually writes to /home/crazyin/resume/storage and /home/crazyin/resume/bootstrap/cache hence the need to set 775 permission on the directories.

You can easily do this from CPanel by right-clicking the directory

6. Update /home/crazyin/public_html/index.php
The changes to be made are in bold.

<?phpini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <taylor@laravel.com>
*/
define('LARAVEL_START', microtime(true));/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
require '/home/crazyin/resume/vendor/autoload.php';/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once '/home/crazyin/resume/bootstrap/app.php';/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();$kernel->terminate($request, $response);

NB: If run into any issue and wish to debug, you could add these lines at the very beginning of the index.php file.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

7. Woah! We made it.
Should I tell you what to do next?

--

--

Emeka Mbah

I am a seasoned software developer who loves writing code in PHP, Laravel, and Javascript