
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!
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:
- Log into your cPanel account.
- Navigate to the File Manager.
- Upload your entire Laravel project to the root directory – NOT the
public_htmlfolder! 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:
- Open your uploaded Laravel folder.
- Find the
publicfolder and open it. - Select all the contents (yes, all of them!).
- Move these files to the
public_htmldirectory. 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:
- Go to the
public_htmlfolder. - Find
index.phpand right-click it. - Choose
Code Editorfrom the menu.
Now, depending on your Laravel version, you’ll need to make some changes:
- For old Laravel versions below 11:
require __DIR__.'/../folderName/vendor/autoload.php';
$app = require_once __DIR__.'/../folderName/bootstrap/app.php';- For new Laravel 11 and above:
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
folderNamewith 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!
Step 2: The Symlink Sorcery
Here’s where the magic happens. Open up your terminal and type in this spell:
ln -s /laravel/public/* public_htmlBoom! 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!