How to Deploy a Laravel App from a GitHub Private Repository to cPanel (2022)

Published: 2 years ago - Updated: 1 year ago

7 min - 1388 Words

article 'How to Deploy a Laravel App from a GitHub Private Repository to cPanel (2022)' banner

Summary

In this article, I will demonstrate the proper and secure methods for deploying/hosting your Laravel App in cPanel from a GitHub private repository.

Introduction

So you’ve finished developing your Laravel app and are ready to release it to the public. This article will walk you through the steps of hosting your Laravel app in cPanel from a GitHub private repository.

I decided to write about this because I was attempting to do the same thing a few weeks ago and encountered numerous issues and problems. It took a few days and a lot of reading and research for me to finish it. I decided to write about it so you wouldn’t have to go through it.

What is covered?

In this article we will learn:

  • How to create a SSH key in cPanel
  • How to add SSH key to your GitHub account
  • How to create and clone a repository in cPanel
  • How to modify your code to make it work for cPanel

Assumptions

  • You have terminal access to your Cpanel - If you do not have access to terminal, contact your hosting provider to request access.
  • Your GitHub repository is private
  • You are using a sub domain

Get Started

At this point you have completed your Laravel project and uploaded the code to a GitHub repository.
If you have a public repository, you can skip steps 1 to 3.

Step 1: Create ssh key using Cpanel terminal.

As I mentioned above, I have assumed that you have terminal/shell access to your cpanel account. if you can not find the terminal option in your Cpanel, contact your hosting provider and request terminal/shell access.

  • Go to your terminal window in Cpanel

Description
Make sure to replace your_email@example.com to your actual email address.

  • Enter the bellow command to create ssh key pair
ssh-keygen -t ed25519 -C "your_email@example.com"  

You will be prompted to enter a passcode after entering the command. Give your ssh key no password and keep pressing enter.

Description

Step 2: Authorize the created ssh key

After you’ve created an ssh key, you must authorize it from your account. If your key is not authorized, it will not work; this step is required to protect your account and ssh keys.

  • Go to SSH Access page in cPanel

Description

  • Click on Manage SSH Keys button

Description

  • In the Public Keys section, select your key and click on Manage.

Description
In the next page click the Authorize button to authorize your ssh key.

  • Copy your public ssh key

Description
Description
Now that you have created your ssh key , authorized it and copied it, It is time to add it to your GitHub account.

Step 3: Add your SSH key to your GitHub

To gain access to your private GitHub repositories from Cpanel, you must first add the newly created ssh key to your account so that GitHub can verify your identity.

  • Go to your GitHub account settings -> SSH and GPG Keys
    SSH and GPG Keys

  • Click on New SSH Key button

Description

  • In the new page give your key a name and paste the public ssh key that you copied from Cpanel in the Key section.

Description
When you are finished press Add SSH Key button.

Step 4: Create a repository in Cpanel

You’ve made an ssh key and linked it to your GitHub account. It’s time to set up a Cpanel repository to transfer our code from GitHub to Cpanel.

  • Get your repository clone link from GitHub
    Go to your Laravel project repository and copy the ssh clone URL . we will need it for cloning the repository in Cpanel.

Description

  • Go to Git Version Control page in Cpanel

Description

  • Click on Create button

Description

  • Paste your repository link in the Clone URL input field

Description
Keep the Repository Path and Repository Name as default.
When your repository is created, it will be added in your repositories list like bellow screenshot.

Description

  • To get the latest commit from your github repository click on Manage -> Pull or Deploy -> Update from Remote

Description

  • Create a .env file in your project

    If you have everything all the steps you will have all your project files in the repositories -> your_repository_name

    Because your project’s.env file will not be in GitHub, you must create one or rename the.env.example file to .env.

Make sure that .env.example file does not contain any sensitive information such as your database username and password or your app key.

Description
If you do not see any .dot files, turn on the Show Hidden Files from the settings

Description

Step 5: Copy the public folder to your subdomain main folder

For security purposes we should only put the Laravel public folder inside out subdomain main folder.
If we put all of our project files inside public_html folder or a subdomain main folder, we will be exposing our .env file to the internet.

Anyone can access your.env file via the “'http://your-domain.com/.env”’ link and obtain sensitive information such as your database username and password, your SMTP email password,… and so on.
Description

To move the public folder, select all files inside it and click copy, then choose your main folder.

Make sure to copy .htaccess also

Step 6: Edit the index.php file to point to correct project location

After copying Laravel’s public folder to public html, we must make a few changes to index.php to ensure it points to the correct path.

Description
Update the highlighted areas with new addresses that point to the correct files.
Right now, it will look in the subdomain main folder for the required files, but those files exist elsewhere.
In my case, the new addresses will look like this.

if (file_exists($maintenance = __DIR__.'/../repositories/simplesocial/storage/framework/maintenance.php')) {
    require $maintenance;
}
require __DIR__.'/../repositories/simplesocial/vendor/autoload.php'
$app = require_once __DIR__.'/../repositories/simplesocial/bootstrap/app.php';

Update the addresses accordingly.

Step 7: Install the composer dependencies

After setting everything up, you need to install composer dependencies of your project.

  • Go to Terminal
  • Navigate to your repository cd repositories/simplesocial
  • Run composer update and composer install

Description
After the installation is done, your project will be up and running and accessible to everyone.

Step 8: Apply new changes to your project

To get the latest commit from your github repository go to Git Version Control -> Manage -> Pull or Deploy -> Update from Remote

If you have new changes to the frontend of your project like changes in JavaScript or Vue, you have to manually upload it to the subdomain folder for your changes to get effect.

Summary

To host your Laravel Project in cPanel with GitHub follow these 8 steps

  1. Create SSH key using cPanel terminal.
  2. Authorize the created SSK Key
  3. Add your SSH key to your GitHub
  4. Create a repository in cPanel
  5. Copy the public folder to your subdomain main folder
  6. Edit the index.php file to point to correct project location
  7. Install the composer dependencies
  8. Apply future changes to your project

Related Article

How to change the default public folder path in Laravel and upload files to it for cPanel deployment

Add Comment

Conversations (0)

Sign up for our newsletter

Stay up to date with the latest news and articles.