How to Deploy GitHub Repositories to Cpanel | Learn Git Version Control and SSH key | Step by Step 🔥

3 min read 2 months ago
Published on Aug 29, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the process of deploying GitHub repositories to cPanel using Git version control and SSH keys. By following these steps, you'll learn how to set up your environment, create SSH keys, and manage your Laravel project effectively on cPanel. This process is essential for developers looking to streamline their deployment workflows.

Step 1: Login to cPanel and Create SSH Key

  1. Login to Your cPanel Account

    • Access your cPanel account via your hosting provider’s URL.
  2. Open the Terminal

    • Navigate to the terminal or SSH access section in cPanel.
  3. Generate SSH Key

    • Use the following command to create an SSH key:
      ssh-keygen -t ed25519 -C "your_email@example.com"
      
    • This will create a new SSH key using the ed25519 encryption algorithm.
  4. Save the Key

    • When prompted, save the key to the default location (usually ~/.ssh/id_ed25519) and press Enter.
  5. Authorize the Key

    • Copy the public key to the authorized_keys file:
      cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys
      

Step 2: Configure SSH Key with GitHub

  1. Copy the SSH Key

    • Display your public key using:
      cat ~/.ssh/id_ed25519.pub
      
    • Copy the displayed key to your clipboard.
  2. Add SSH Key to GitHub

    • Go to your GitHub account settings.
    • Navigate to SSH and GPG keys.
    • Select "New SSH key," paste your key, and give it a title.
  3. Test the Connection

    • Use the following command to check the connection:
      ssh -T git@github.com
      
    • You should receive a success message confirming the connection.

Step 3: Set Up Git Version Control in cPanel

  1. Open Git Version Control

    • In cPanel, find the "Git Version Control" feature.
  2. Create a Repository

    • Click on "Create" to start a new repository.
    • Enter your GitHub project’s SSH link as the repository path.
  3. Select the Write Folder

    • Choose the directory where you want to deploy your project.
    • Ensure that this folder is empty before proceeding.

Step 4: Configure Your Laravel Project

  1. Create a .cpanel.yml File

    • In your project root, create a file named .cpanel.yml.
    • Add the following code, replacing your_cpanel_username with your actual cPanel username:
      deployment:
        username: your_cpanel_username
      
  2. Create a .env File

    • In your Laravel project, create a .env file.
    • Set up your database connection and other environment variables.
  3. Open Terminal for Composer

    • Access the terminal in cPanel.
  4. Install Dependencies and Migrate Database

    • Run the following commands:
      composer install
      php artisan migrate
      

Conclusion

Following these steps, you have successfully deployed your GitHub repository to cPanel using Git version control and SSH keys. You learned how to generate SSH keys, configure them with GitHub, set up Git in cPanel, and manage a Laravel project. For further exploration, consider learning about automated deployment tools or continuous integration to enhance your workflow even more.