unitech

Deploying a Laravel Backend on cPanel: A Comprehensive Guide

 Sunday, February 11, 2024

Laravel-Hosting in Cpanel

 

Laravel, a popular PHP web framework, provides developers with a powerful toolset for building robust backends. If you're ready to take your Laravel project live and your hosting environment is cPanel, here's a step-by-step guide to deploying your Laravel backend.

 

A- Prerequisites

1. cPanel Hosting Account: Ensure you have access to a cPanel hosting account with PHP support.

2. Laravel Project: Have your Laravel backend codebase ready for deployment.

 

B- Steps to Deploy

   Step 1: Access cPanel

  Log in to your cPanel hosting account. Once logged in, navigate to the "Files" and "Software" sections.

 

   Step 2: Upload Laravel Project

  Using the cPanel File Manager, upload your Laravel project files to the public_html directory or a subdirectory within it.

 

   Step 3: Configure File Permissions

  Set appropriate file permissions for Laravel. In the File Manager, right-click on the Laravel project folder, select "Change Permissions," and set folders to 755 and files to 644.

 

   Step 4: Create a MySQL Database

  1- In the "Databases" section, use the "MySQL Database Wizard" to create a MySQL database and user.

  2- Note down the database name, username, and password; you'll need these for Laravel configuration.

 

   Step 5: Configure .env File

  Edit the .env file in your Laravel project to reflect the database connection details:

 

dotenv

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=your_database_name

DB_USERNAME=your_database_user

DB_PASSWORD=your_database_password

 

   Step 6: Composer Install

  In the "Software" section, find "Select PHP Version." Ensure that the PHP version matches the one compatible with your Laravel project. Then, in the cPanel terminal or via SSH, navigate to your project folder and run:

bash

composer install

 

   Step 7: Artisan Migrate

  Run Laravel migrations to set up your database schema:

bash

php artisan migrate

 

   Step 8: Set Up Domain

  Navigate to the "Domains" section in cPanel to configure your domain for the Laravel application.

 

   Step 9: SSL Configuration (Optional)

  For secure communication, set up SSL for your domain. In cPanel, find SSL/TLS settings to manage certificates.

 

   Step 10: Configure Apache (if needed)

  In some cases, you might need to configure Apache to point to the public folder of your Laravel project. This can be done in the "Apache Configuration" section of cPanel.

 

Conclusion

   Deploying a Laravel backend on cPanel involves uploading your code, configuring database connections, and adjusting settings through the cPanel interface. By following these steps, you can ensure a smooth deployment process for your Laravel application, making it accessible through your configured domain. Explore additional cPanel features to enhance security and optimize the performance of your hosted Laravel backend.