Deploying a Laravel project on cPanel can seem tricky, especially if you're coming from a local development environment. But don’t worry — in this post, I’ll walk you through every step to make your Laravel application live on shared hosting via cPanel.
๐ ️ Prerequisites
- Laravel project ready on your local machine
- cPanel access with PHP 8.0+ support
- Access to MySQL via phpMyAdmin
๐ Step 1: Prepare Your Laravel Project
- Run
composer install --optimize-autoloader --no-dev
to install only production dependencies. - Run
php artisan config:cache
andphp artisan route:cache
. - Zip the entire Laravel project.
๐ค Step 2: Upload Project to cPanel
- Login to your cPanel account.
- Open File Manager.
- Navigate to the
public_html
directory. - Upload your Laravel zip file and extract it.
๐ Step 3: Move Folders Appropriately
cPanel serves from public_html
, but Laravel expects its entry point in public/
. Do this:
- Move all contents from your Laravel project’s
public/
folder intopublic_html/
. - Edit
index.php
inpublic_html
:require __DIR__.'/../vendor/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php';
Change paths to:require __DIR__.'/../your-laravel-folder/vendor/autoload.php'; $app = require_once __DIR__.'/../your-laravel-folder/bootstrap/app.php';
๐พ Step 4: Set Up the Database
-
Access phpMyAdmin:
Log in to your cPanel dashboard. Scroll down to the Databases section and click on phpMyAdmin. This will open a web-based interface where you can manage MySQL databases.
-
Create a New Database and User:
Go back to the cPanel dashboard and open MySQL® Databases. Under the Create New Database section, enter a name and click Create Database.
Scroll down to the MySQL Users section to create a new user with a secure password. After that, use the Add User to Database section to link the user with the new database.
Ensure that All Privileges are granted to the user. -
Import Your Local SQL File:
Return to phpMyAdmin and select your new database from the sidebar. Click on the Import tab in the top menu.
Click Choose File and upload your exported.sql
file from your local Laravel project. Then click Go to import the database structure and content. -
Configure Your Laravel
.env
File:Open the
.env
file in your Laravel project root and update the database configuration with the credentials created in cPanel. Example:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_db_name DB_USERNAME=your_db_user DB_PASSWORD=your_db_password
Save the changes. If you're editing locally, re-upload the updated
.env
file to your server.
๐ Step 5: Configure .env and Permissions
- Set
APP_ENV=production
andAPP_DEBUG=false
- Set proper write permissions for
storage
andbootstrap/cache
๐ Step 6: Test Everything
- Clear browser cache and reload your domain.
- If errors occur, check
storage/logs/laravel.log
or enable temporary debug mode.
๐ Done!
Your Laravel app should now be live on your cPanel-hosted domain. Make sure to monitor logs and update dependencies regularly.
If this helped you, feel free to share or connect with me on LinkedIn!
Comments
Post a Comment