The Junior Dev's Guide to Deploying Laravel on cPanel (Without the Public Folder Hassle!)

Sohag HasanSohag Hasan
3 min read

Deploying Laravel on cPanel

Hey there, fellow developers! ๐Ÿ‘‹ Remember your first Laravel deployment? Mine was a roller coaster of emotions that involved a lot of coffee, some hair-pulling moments, and finally, that sweet victory when everything worked. Today, I'm going to share how to deploy your Laravel application on cPanel without dealing with that pesky public folder configuration.

The Challenge

Picture this: It's 9 PM, and you've just finished your first Laravel project for a client. You're feeling pretty good about yourself until you realize โ€“ oh wait, the client has a shared hosting with cPanel, and you need to deploy it TODAY. The traditional Laravel structure wants you to point to the public folder, but your client's hosting doesn't support that. Don't panic! I've got you covered.

The Solution

We're going to use a clever workaround that keeps your application secure while working within cPanel's limitations. Here's how:

Step 1: Prepare Your Project

First, let's get your project ready for deployment:

  1. Run composer install --optimize-autoloader --no-dev in your local environment

  2. Run php artisan key:generate

  3. Create a zip file of your entire Laravel project

  4. Make sure your .env file is configured for production

Step 2: The Upload

  1. Log into cPanel (take a deep breath, you got this!)

  2. Navigate to File Manager

  3. Go to public_html

  4. Upload your zip file and extract it here

Step 3: The Magic Files

Now, here's where the magic happens. We need to create two special files in your root directory (public_html):

  1. First, create a .htaccess file with these contents:
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    RewriteEngine On
    php_value open_basedir "/var/www/html:/tmp"
    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
  1. Create an index.php file:
<?php
$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}
require_once __DIR__.'/public/index.php';

Step 4: The Asset Update (Very Important!)

Here's a crucial step that many developers miss: You need to update all your asset URLs to include 'public/'. In your blade files, update your assets like this:

<!-- Before -->
<img src="/images/logo.png">
<link href="/css/app.css" rel="stylesheet">
<script src="/js/app.js"></script>

<!-- After -->
<img src="/public/images/logo.png">
<link href="/public/css/app.css" rel="stylesheet">
<script src="/public/js/app.js"></script>

Understanding the Magic

Let's break down what these files do:

  • The .htaccess file acts as your traffic controller. It tells Apache how to handle incoming requests and properly route them through your Laravel application. It's like a security guard who knows exactly where to send your visitors.

  • The index.php file is your bouncer. It catches all requests and forwards them to your actual Laravel application in the public folder. Think of it as a smart receptionist who knows how to handle every visitor.

Pro Tips from Someone Who Learned the Hard Way

  1. Always keep a backup of your original files before making these changes

  2. Test your asset paths thoroughly after deployment

  3. Check your storage folder permissions (set to 755 for folders and 644 for files)

  4. Don't forget to update your .env file with production settings

Troubleshooting Common Issues

  • If your images aren't loading, double-check that you've added 'public/' to all asset URLs

  • Getting a 500 error? Check your storage folder permissions

  • Blank page? Enable error reporting in your .env file temporarily to see what's wrong

Conclusion

And there you have it! Your Laravel application is now running smoothly on cPanel without exposing your entire application structure. Remember, we all started somewhere, and these deployment challenges are just part of the journey to becoming a better developer.

Happy deploying! ๐Ÿš€

P.S. Don't forget to celebrate with your favorite beverage when it all works โ€“ you've earned it!

2
Subscribe to my newsletter

Read articles from Sohag Hasan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sohag Hasan
Sohag Hasan

WhoAmI => notes.sohag.pro/author