How to host Laravel project on cPanel

Laravel is the fastest-growing PHP framework. Its mainly used to build scalable web applications. So most of the developers are using AWS to host Laravel applications. Because it's very easy to pull the code from Git, also can run artisan commands in the terminal. That's why AWS (EC2) is the favorite hosting provider for most of the developers. But still, some of the companies or developers are using cPanel to host their web applications.

How hard is that?

Let's compare 3 basic tasks that we frequently execute in Laravel

  1. Uploading Laravel project to the server

AWS : git clone git@gitlab.com:abc/pqr.git <- It will do the job

cPanel: Compress your folder and upload it to cPanel-> File manager -> <Your project folder>

2. Updating your code

AWS: git pull — — rebase origin develop

cPanel : Copy individual files and upload to the location

3. Database related operations

AWS: run artisan commands. Such as php artisan migrate or php artisan db:seed

cPanel: Get a SQL data dump and import it into your database

Anyways I’ll get into the topic

Database configurations

  1. Go to MySQL Databases in the cPanel home page
Go to MySQL Databases

2. Create a new Database

Create a database

3. Then create a DB user

Create a new user

4. Assign the created user to the newly created database

Assign the user to the database

5. Now Go to phpMyadmin and import your database

phpMyAdmin

Project Configurations

Please follow the steps to host your Laravel project

  1. Compress your Laravel project
  2. Go to your cPanel ->File manager
File manager

3. Then upload your Laravel project folder and unzip it. Go into your project folder

Your extracted project folder will be like this

4. Now you need to move your public folder contents into the public_html folder. So go back one step. Then you can see the public_html folder. We are doing this because, when someone accesses our website it will check the index.php file in the public_html folder. In Laravel public folder contains an index.php file. That's why we moved the contents.

public_html location

5. Now we need to modify the index.php file.

Index.php file usually accesses the vendor folder and bootstrap folder from public folder. Now we moved the public folder into the public_html folder. So now we need to change the paths for vendor folder and bootstrap folder.

Before

require __DIR__.’/../vendor/autoload.php’;

$app = require_once __DIR__.’/../bootstrap/app.php’;

After

require __DIR__.’/../<your project folder>/vendor/autoload.php’;

$app = require_once __DIR__.’/../<your project folder>/bootstrap/app.php’;

6. Now you need to modify your .env file with new credentials and configurations

You must change the marked configurations from the .env file

After

APP_NAME=“My new project
APP_ENV=production
APP_KEY=<your app key>
APP_DEBUG=false
APP_LOG_LEVEL=debug
APP_URL= http://example.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=<port>
DB_DATABASE=test_sampledb
DB_USERNAME=test_myuser
DB_PASSWORD=534@4524U&l;3J$9rj2

Yayyyy!!! You did it!

Have a good day!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade