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


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:
Run
composer install --optimize-autoloader --no-dev
in your local environmentRun
php artisan key:generate
Create a zip file of your entire Laravel project
Make sure your
.env
file is configured for production
Step 2: The Upload
Log into cPanel (take a deep breath, you got this!)
Navigate to File Manager
Go to
public_html
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):
- 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>
- 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
Always keep a backup of your original files before making these changes
Test your asset paths thoroughly after deployment
Check your storage folder permissions (set to 755 for folders and 644 for files)
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!
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