How to host laravel on shared hosting

How to host laravel on shared hosting
31 December 2016

Setup Laravel on shared hosting

Laravel is one of the best frameworks for php weather you want a simple blog or you want an enterprise application. I am posting this blog post to show how to host a laravel application on a shared hosting in simple step.

Step 1:- Install Laravel
First step is to install laravel and for that we will use composer from terminal. First navigate to your public_html directory through terminal and than run following command:-

~/server $ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
~/server $ php composer-setup.php
~/server $ php -r "unlink('composer-setup.php');"
~/server $ php composer.phar create-project --prefer-dist laravel/laravel blog

Installation will take some time so be patient.If it got stucks like it stucked with me install laravel locally and create a zip file of it and upload it on the public_html directory.Use FTP client instead of cpanel upload as it blocks some files like .env to be uploaded through cpanel.

Once its done create a folder name laravel and move all files except public folder to it. Than move all files from public folder to public_html folder and delete public folder as we will not be needing it anymore. Now lets move to step 2.

Step2:- Edit index.php file
Now we have successfully installed laravel we have to tell our application where laravel files are situated so we have to change some code in index file. Without commented lines it should like this:-

<?php
        require __DIR__ . '/../bootstrap/autoload.php';
        $app = require_once __DIR__ . '/../bootstrap/app.php';
        $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
        $response = $kernel->handle(
                $request = Illuminate\Http\Request::capture()
                );
        $response->send();
        $kernel->terminate($request, $response);

to make it look like this.

<?php
        require 'laravel/bootstrap/autoload.php';
        $app = require_once 'laravel/bootstrap/app.php';
        $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
        $response = $kernel->handle(
                $request = Illuminate\Http\Request::capture()
                );
        $response->send();
        $kernel->terminate($request, $response);

Now we have to edit laravel/server.php . Without comments it should like this :-

<?php
        $uri = urldecode(
        parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
        );
        if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
                return false;
        }
        require_once __DIR__.'/public/index.php';

to make it look like this.

<?php
        $uri = urldecode(
        parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
        );
        if ($uri !== '/' && file_exists(__DIR__.'../'.$uri)) {
                return false;
        }
        require_once __DIR__.'../index.php';

Now we are all set. If we visit our domain we will see laravel homepage. We will recommend clearing cache before visting.


By Lakshay Jain

Tags: Cloud