How to deploy a Laravel application on a cPanel powered server

We love Laravel. But when it comes to deploying our app on shared hosted server powered by cPanel, it can be a little confusing. I've been searching the web for a solution that has these characteristics: (1) Allowed me to clone my repo from remote source control (2) Allowed me to pull changes without redeploying my app (3) Was clean. I didn't find it right away, so I am bringing it to you.

How to deploy a Laravel application on a cPanel powered server

Step 1 : Login to cPanel

This is a no brainer:

  1. Go to https://cpanel.yourdomain.com/ or http://yourdomain.com:2083 (Replace "yourdomain.com" with your own domain name
  2. Login with your cPanel credentials
  3. Go into Advance section > terminal (Make sure you have SSH access from your provider)

Step 2 : Composer

Make sure you have Composer installed. If not, install it. We're gonna need it.

Step 3 : Clone your repo

In your terminal, execute the following commands. First cd to your public_html folder:

cd /home/username/public_html/

Replace "username" with your username.

Then initialize git

git init

Add your remote repository's URL

git remote add origin

Replace with your own repositoy URL, whether from Github, Bitbucket or whichever remote repository hosting service you use.

When done, pull your app with this command

git pull origin master

I assume here that you want to deploy your master branch to production. You can deploy any branch, just change the name of the branch.

Step 4: Setup your application

Remember when I asked you to check for or install composer? We need it now. So we run the following command to install our dependencies:

composer install

Then change your environmental variables in the .env file. Setup your database, database name, database host and all other things.

When done, generate your application key with this command

php artisan key:generate

Time to migrate your database

php artisan migrate

Don't forget to set the correct permissions for storage/ folder

chmod -R 775 storage

Step 5: Make your app accessible to the public

This is the trickier part. Many resources I found online propose many solutions. But Most of them don't follow my requirements as described in introduction to this article. Here is the solution that meets better those requirements

First, backup your .htaccess file by running this command:

mv .htaccess .htaccess.backup

Then create another .htaccess file:

nano .htaccess

In the open .htaccess file, paste the following lines:

# .htaccess main domain to subfolder redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ public/index.html [L]

Replace example.com with your own domain. Then save your file and close it (Ctrl + o then Ctrl x).

Finally, create a sym link to your app's storage folder

php artisan storage:link

You can check your application by opening your browser and going to yourdomain.com. You should see your app's home page.

I hope this tutorial helped you deploy your Laravel app on a cPanel powered server. 


Written by: Blaise Nduwimana
Published at: Fri, Jun 24, 2022 3:47 AM
Share with others: