CodingTricks LogoCodingTricks
HomePostsTipsCopy/PasteLinksContact UsAbout Us
2024 - 2025 CodingTricks.co | All rights reserved
Privacy PolicyTerms of Service
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

| 3 min read

Last updated on : Dec 8, 2024

Tips
82 views

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?
  • Method 1: The No-Terminal-Access Approach
    • Step 1: Upload Files to cPanel
    • Step 2: Move Public Folder Contents
    • Step 3: Edit index.php
  • Method 2: The Terminal-Access Shortcut
    • Step 1: Upload Your Files (Same as Before)
    • Step 2: The Symlink Sorcery

#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!

#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_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!

Related Posts

  • Blocking Disposable Emails with the laravel-disposable-email PackageBlocking Disposable Emails with the laravel-disposable-email Package
  • NextJS App Router SEO Best PracticesNextJS App Router SEO Best Practices
  • Mastering Laravel Streamed Responses: Boost Performance with Fast Data DeliveryMastering Laravel Streamed Responses: Boost Performance with Fast Data Delivery
  • How to Personalize Visual Studio Code (VSCode)How to Personalize Visual Studio Code (VSCode)
  • Email Testing with Mailtrap in NextJSEmail Testing with Mailtrap in NextJS

Tags

Api(1)Authentication(5)Backup (1)Copy Paste(12)Email(2)Express(1)Firebase(1)Github Action(2)News(8)Push Notification(1)Queue(2)Server(11)Server Action(3)Testing(1)Tips(17)Websocket(1)

Popular Posts

  • TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!  TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!
  • How to use WebSocket in NextJS App router with Socket.IOHow to use WebSocket in NextJS App router with Socket.IO
  • How to Set Up Queue Jobs in NextJS Using BullMQHow to Set Up Queue Jobs in NextJS Using BullMQ
  • Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)
  • How to Set Up NextJS cron jobs without VercelHow to Set Up NextJS cron jobs without Vercel
  • Mastering Laravel Streamed Responses: Boost Performance with Fast Data DeliveryMastering Laravel Streamed Responses: Boost Performance with Fast Data Delivery
  • Tinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHPTinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHP
  • Nextjs 14 roles and permissions (RBAC) : Step-by-Step GuideNextjs 14 roles and permissions (RBAC) : Step-by-Step Guide

Get updates directly to your inbox.

Join 500+ developers getting updates on Laravel & Next.js tips. No spam,
unsubscribe anytime.


Share this article:

82 views