How to Deploy GitHub Code to CPanel

As a manager in an IT company, keeping the production environment updated with the latest code from the main branch of GitHub is crucial. This guide will walk you through deploying a project from GitHub to CPanel step by step.


A subdomain is useful for staging or production environments. If you want to deploy the project to a subdomain:

Steps to Create a Subdomain

  1. Log in to CPanel.

  2. Navigate to Domains > Subdomains.

  3. Enter the subdomain name (e.g., staging.yourdomain.com).

  4. Set the document root (e.g., public_html/staging).

  5. Click Create.


2. Enable Git Version Control in CPanel

CPanel provides built-in Git support, allowing you to pull code directly from GitHub.

Steps to Enable Git Version Control

  1. Log in to CPanel.

  2. Go to Git Version Control under the Files section.

  3. Click Create and select Clone a Repository.

  4. Enter your GitHub repository URL.

  5. Set the deployment directory (e.g., public_html/staging).

  6. Click Create.

This will clone your GitHub repository into the specified folder.


For automatic deployments, you need to set up SSH authentication between GitHub and CPanel.

Steps to Generate SSH Key on CPanel

  1. In CPanel, go to SSH Access > Manage SSH Keys.

  2. Click Generate a New Key.

  3. Copy the public key and add it to your GitHub repository.

Steps to Add SSH Key to GitHub

  1. Go to your GitHub repository.

  2. Click on Settings > Deploy Keys.

  3. Click Add Deploy Key.

  4. Paste the public key copied from CPanel.

  5. Check Allow write access if you need to push changes.

  6. Click Add Key.


4. Deploying Code from GitHub to CPanel

Once the repository is cloned, you can pull the latest changes from the main branch.

Steps to Manually Update the Code

  1. Log in to CPanel.

  2. Open the Terminal or use File Manager to navigate to the project directory:

     cd ~/public_html/staging
    
  3. Pull the latest code from the main branch:

     git pull origin main
    
  4. If using Composer (PHP projects):

     composer install --no-dev
    
  5. If using Node.js:

     npm install --production
    

5. Automating Deployment with Webhooks

To automate deployment whenever code is pushed to GitHub:

Steps to Set Up a GitHub Webhook

  1. Go to your GitHub repository.

  2. Click Settings > Webhooks.

  3. Click Add Webhook.

  4. Enter the payload URL:

     https://yourdomain.com/deploy.php
    
  5. Select application/json as the content type.

  6. Select Just the push event.

  7. Click Add Webhook.

Create a Deploy Script on CPanel

Create a deploy.php file in your project root and add the following:

<?php
    // Path to your repository
    $repo_path = '/home/your_user/public_html/staging';

    // Pull latest changes
    exec("cd {$repo_path} && git pull origin main", $output);

    // Log output
    file_put_contents('deploy.log', implode("\n", $output));

    echo "Deployment Successful!";
?>

Ensure this script is accessible by GitHub by setting correct permissions.

chmod +x deploy.php

Now, every time you push to main, GitHub will trigger this script and update the code automatically.


6. Managing Code Updates Efficiently

  • Always test changes in a staging environment before pushing to production.

  • Use branches efficiently:

    • main: Stable production code

    • dev: Ongoing development

    • feature/<feature_name>: New features or bug fixes

  • Rollback in case of failure:

      git reset --hard HEAD~1
    
  • Backup database before major changes:

      mysqldump -u user -p database_name > backup.sql
    

Pros & Cons of Deploying via GitHub & CPanel

Pros

  • Easy to manage code versions.

  • Secure deployment with SSH keys.

  • Automates deployment with webhooks.

  • CPanel provides a UI for non-technical users.

Cons

  • No built-in CI/CD pipeline like GitHub Actions.

  • CPanel’s Git integration is limited.

  • Requires manual setup for advanced automation.


Conclusion

By following this structured Git workflow with CPanel, you can efficiently manage and deploy code to your production or staging environments. Implementing GitHub webhooks and SSH authentication will streamline deployments, ensuring code updates are pushed automatically when needed.

Let me know if you need further improvements! 🚀


Most Asked Questions :

👉 kya me direct khali ak command chala kr projet ko push kr skti hu ?

Haan, aap ek single command se GitHub ki main branch ka latest code CPanel pr update kar sakti hain, lekin iske liye CPanel pr pehle se Git setup aur SSH authentication configure honi chahiye.

Agar aapne CPanel me Git repository clone ki hai (jaisa maine guide me bataya), toh aap sirf yeh command chala kar latest code pull kar sakti hain:

cd ~/public_html/staging  # Apne project ke directory me jayein
git pull origin main      # GitHub se latest code pull karein

Agar aap ek hi command me execute karna chahti hain:

cd ~/public_html/staging && git pull origin main

Yeh command sirf tab kaam karegi jab:

  1. CPanel me Git repository pehle se clone ho.

  2. SSH authentication sahi se configured ho.

  3. Git repository CPanel ke "Git Version Control" me linked ho.

Agar aap automation chahti hain, toh GitHub Webhooks setup karein jo automatically git pull origin main run kare jab bhi koi push ho.

Agar aapke paas koi specific issue ho raha hai, batao! 🚀