How to Deploy Laravel to Shared Hosting (cPanel) in 2024: The Easy Way

How to Deploy Laravel to Shared Hosting (cPanel) in 2024: The Easy Way

Posted By

kamlesh paul

on

Dec 8, 2024

Hey there, Laravel enthusiasts! 👋 I recently had to deploy a Laravel app to shared hosting using cPanel, and boy, was it an adventure. But don’t worry, I’ve got your back! I’m going to share my battle-tested method that’ll work whether you’ve got terminal access or not. Let’s dive in and get that app deployed!

Table of contents

Why Bother with Shared Hosting?

Before we get our hands dirty, let’s chat about why we’re even considering shared hosting. Picture this: you’ve built an awesome Laravel app, but your client is adamant about using their existing shared hosting plan. Frustrating, right? But fear not! With a few tricks up our sleeve, we can make Laravel play nice with cPanel. It’s like teaching a gourmet chef to cook in a small kitchen – challenging, but totally doable!

Method 1: The No-Terminal-Access Approach

Alright, let’s start with the scenario where you’re working with one hand tied behind your back – no terminal access. Don’t sweat it, we’ve got this!

Step 1: Upload Files to cPanel

First things first, we need to get your Laravel files onto the server. But here’s the twist:

  1. Log into your cPanel account.
  2. Navigate to the File Manager.
  3. Upload your entire Laravel project to the root directory – NOT the public_html folder! Why? It’s like hiding your secret recipe – we want to keep the core Laravel files away from prying eyes.

Step 2: Move Public Folder Contents

Now, let’s get your public assets where they need to be:

  1. Open your uploaded Laravel folder.
  2. Find the public folder and open it.
  3. Select all the contents (yes, all of them!).
  4. Move these files to the public_html directory. This step is like setting the table for your guests – we’re putting all the pretty stuff where people can see it.

Step 3: Edit index.php

Here’s where it gets a bit tricky, but I believe in you! We need to edit the index.php file:

  1. Go to the public_html folder.
  2. Find index.php and right-click it.
  3. Choose Code Editor from the menu.

Now, depending on your Laravel version, you’ll need to make some changes:

  • For old Laravel versions below 11:
index.php
require __DIR__.'/../folderName/vendor/autoload.php';
$app = require_once __DIR__.'/../folderName/bootstrap/app.php';
  • For new Laravel 11 and above:
index.php
use Illuminate\Http\Request;
define('LARAVEL_START', microtime(true));
// Determine if the application is in maintenance mode...
if (file_exists($maintenance = __DIR__.'/../folderName/storage/framework/maintenance.php')) {
    require $maintenance;
}
// Register the Composer autoloader...
require __DIR__.'/../folderName/vendor/autoload.php';
// Bootstrap Laravel and handle the request...
(require_once __DIR__.'/../folderName/bootstrap/app.php')
    ->handleRequest(Request::capture());

Remember to replace folderName with the actual name of your Laravel root directory. It’s like giving your app a map to find its way home!

Method 2: The Terminal-Access Shortcut

Got terminal access? Lucky you! You’ve got a secret weapon that’ll make this process a breeze.

Step 1: Upload Your Files (Same as Before)

Just like in Method 1, upload your Laravel project to the root directory, not public_html. It’s still our little secret!

Here’s where the magic happens. Open up your terminal and type in this spell:

ln -s /laravel/public/* public_html

Boom! You’ve just created a magical bridge (symlink) between your Laravel public directory and the public_html folder. It’s like teleporting your files without actually moving them!

Share this article

25 views