How to Deploy GitHub Code to CPanel
Table of contents
- 1. Setting Up a Subdomain (Optional but Recommended)
- 2. Enable Git Version Control in CPanel
- 3. Connecting GitHub to CPanel via SSH (Recommended)
- 4. Deploying Code from GitHub to CPanel
- 5. Automating Deployment with Webhooks
- 6. Managing Code Updates Efficiently
- Pros & Cons of Deploying via GitHub & CPanel
- Conclusion
- Most Asked Questions :
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.
1. Setting Up a Subdomain (Optional but Recommended)
A subdomain is useful for staging or production environments. If you want to deploy the project to a subdomain:
Steps to Create a Subdomain
Log in to CPanel.
Navigate to Domains > Subdomains.
Enter the subdomain name (e.g.,
staging.yourdomain.com
).Set the document root (e.g.,
public_html/staging
).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
Log in to CPanel.
Go to Git Version Control under the Files section.
Click Create and select Clone a Repository.
Enter your GitHub repository URL.
Set the deployment directory (e.g.,
public_html/staging
).Click Create.
This will clone your GitHub repository into the specified folder.
3. Connecting GitHub to CPanel via SSH (Recommended)
For automatic deployments, you need to set up SSH authentication between GitHub and CPanel.
Steps to Generate SSH Key on CPanel
In CPanel, go to SSH Access > Manage SSH Keys.
Click Generate a New Key.
Copy the public key and add it to your GitHub repository.
Steps to Add SSH Key to GitHub
Go to your GitHub repository.
Click on Settings > Deploy Keys.
Click Add Deploy Key.
Paste the public key copied from CPanel.
Check Allow write access if you need to push changes.
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
Log in to CPanel.
Open the Terminal or use File Manager to navigate to the project directory:
cd ~/public_html/staging
Pull the latest code from the
main
branch:git pull origin main
If using Composer (PHP projects):
composer install --no-dev
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
Go to your GitHub repository.
Click Settings > Webhooks.
Click Add Webhook.
Enter the payload URL:
https://yourdomain.com/deploy.php
Select application/json as the content type.
Select Just the push event.
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 codedev
: Ongoing developmentfeature/<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:
CPanel me Git repository pehle se clone ho.
SSH authentication sahi se configured ho.
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! 🚀