Hosting a Laravel 11 project on cPanel shared hosting

 

Hosting a Laravel 11 project on cPanel shared hosting involves several steps since Laravel is built to work with a virtual host setup (like localhost with public/ as root), but cPanel typically uses public_html/ as the root directory. Here’s a full guide to help you:


Prerequisites

  • Laravel 11 project ready on your local machine.

  • cPanel access (via your hosting provider).

  • PHP version on server must be PHP 8.2+.

  • Composer installed on server (optional but helpful).

  • MySQL database (if needed) created in cPanel.


📁 Step-by-Step Hosting Guide

1. Structure Your Project for cPanel

By default, Laravel uses public/ as the web root. But cPanel uses public_html/. So you need to move the contents of Laravel's public/ folder into public_html/, and update paths accordingly.

Let’s say your project folder is called laravel11app.

Option A: Simple Adjustment (Recommended for shared hosting)

  1. Compress your entire Laravel project, excluding the vendor folder (optional to reduce size).

  2. Login to cPanel → File Manager.

  3. Upload the ZIP to your home directory (e.g., /home/youruser/) — not inside public_html yet.

  4. Extract the ZIP there — your project files should be in /home/youruser/laravel11app.

Now do the following:

Inside public_html/:
  1. Go into /laravel11app/public/

  2. Copy all contents (including .htaccess and index.php)

  3. Paste them into public_html/

2. Update index.php Paths

Edit public_html/index.php, update the paths:

 require __DIR__.'/../laravel11app/vendor/autoload.php';
$app = require_once __DIR__.'/../laravel11app/bootstrap/app.php';
This tells Laravel to load from your app folder, not from public_html.

3. Environment Configuration

In root of your Laravel app (laravel11app/), edit .env:

  • Set your APP_URL:

APP_URL=https://yourdomain.com
Set database details (from your cPanel > MySQL section):
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_cpanel_dbname
DB_USERNAME=your_cpanel_dbuser
DB_PASSWORD=your_cpanel_dbpass

4. Upload vendor Folder and Run Composer

Option A: Upload vendor Folder (if no SSH/composer access)

  1. Run this locally:
    composer install --optimize-autoloader --no-dev
  2. Zip the vendor/ folder and upload it to your project root on cPanel.
  3. Extract it there.

    Option B: If you have SSH access:

    Login via Terminal/SSH:

     cd ~/laravel11app
    composer install --optimize-autoloader --no-dev

    5. Set Laravel Permissions

    From cPanel → Terminal or SSH:

    chmod -R 775 storage
    chmod -R 775 bootstrap/cache

    Or use File Manager to set permissions.


    🛠️ 6. Run Migrations (if needed)

    Via SSH or route:

    php artisan migrate
    If no SSH, create a temporary route in web.php to run migrations once (then delete it):
     

     Route::get('/migrate', function () {
        Artisan::call('migrate');
    });


    ✅ Final Notes:

    • Visit your domain: https://yourdomain.com → It should load your Laravel app.
    • Make sure to delete any temporary routes or files used for setup.
    • Use .htaccess in public_html/ to enforce HTTPS or custom rules.

Would you like a ready-made .htaccess file for Laravel 11 in public_html/


 

 


Comments

Popular posts from this blog

How to Turn Off Ads in uTorrent

Disable browser caching using a PHP header.