How to Deploy Laravel to Shared Hosting (cPanel) in 2024: The Easy Way
Posted By
kamlesh paulon
Dec 8, 2024Hey 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?
- Method 1: The No-Terminal-Access Approach
- Method 2: The Terminal-Access Shortcut
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_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:
- Open your uploaded Laravel folder.
- Find the
public
folder and open it. - Select all the contents (yes, all of them!).
- 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:
- Go to the
public_html
folder. - Find
index.php
and right-click it. - 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:
- For new Laravel 11 and above:
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!
Step 2: The Symlink Sorcery
Here’s where the magic happens. Open up your terminal and type in this spell:
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!