Connecting cPanel to GitHub allows you to automate deployments and manage code more efficiently. By linking your hosting environment with your GitHub repository, you can streamline updates and ensure that your website or application always runs the latest code. In this guide, we’ll walk through the steps to connect cPanel to GitHub and automate deployments using SSH keys.
Why Connect cPanel to GitHub?
By connecting cPanel to GitHub, you can:
- Automate Deployments: Push code updates from GitHub to your cPanel-hosted website.
- Version Control: Use Git to manage the versions of your website or app and roll back to previous versions if needed.
- Streamline Updates: Sync changes easily between your local development environment and your live server.
Step-by-Step Guide : Connect cPanel to GitHub
Follow these steps to successfully connect cPanel to GitHub.
Step 1: Access cPanel
First, log into your cPanel account using your credentials. Once logged in, you’ll use several tools within cPanel to generate SSH keys, manage your Git repository, and set up the connection with GitHub.
Step 2: Generate a New SSH Key
To securely connect your cPanel environment to GitHub, you need to generate an SSH key pair. There are two methods to do this in cPanel:
Method 1: Generate SSH Key via SSH Access
- In the cPanel dashboard, scroll down to the Security section and click on SSH Access.
- Click Manage SSH Keys. Select Generate a New Key.
- Enter a Key Name, but leave the Password field empty for a passwordless key.
- Select RSA as the key type and 4096 bits for maximum security.
- Click Generate Key.
Method 2: Generate SSH Key via Terminal in cPanel
Alternatively, you can generate a key directly from the terminal:
- In cPanel, go to Advanced and click on Terminal.
- Type the following command to generate a new SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Press Enter to save the key in the default location, and leave the password empty by pressing Enter twice.
After generating the key, you can test the connection with GitHub using the command:
ssh -T git@github.com
This command will verify that your SSH key is set up correctly. You should see a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Step 3: Authorize the SSH Key in cPanel
- After generating the SSH key (whether through SSH Access or Terminal), go back to SSH Access in cPanel.
- Click Manage next to your new SSH key.
- Click Authorize to enable the key for use.
Step 4: Configure Git Version Control in cPanel
Next, you’ll set up Git Version Control in cPanel to connect to your GitHub repository.
- In the cPanel dashboard, scroll down to the Files section and click on Git Version Control.
- Click the Create button to add a new repository.
- Fill in the required fields:
Repository Path: Specify the directory where the repository will be stored in your cPanel account.
Clone URL: Paste the SSH URL of your GitHub repository, which typically looks likegit@github.com:username/repository-name.git
. - Click Create to clone the repository into your cPanel environment.
Step 5: Add Your Public SSH Key to GitHub
Now that the SSH key is generated and authorized in cPanel, you need to add the public key to your GitHub account for authentication.
- In GitHub, click your profile icon in the top-right corner and select Settings.
- In the left sidebar, click SSH and GPG Keys.
- Click the New SSH Key button.
- Go back to cPanel and copy the public SSH key:
If you generated the key through SSH Access, click View/Download next to the public key, then open with Notepad.
If you generated it through the terminal, use the following command to display the public key:bash cat ~/.ssh/id_rsa.pub
- Copy the entire public key text.
- Paste the key into GitHub under the Key field, and give it a recognizable title (e.g., “cPanel Server Key”).
- Click Add SSH Key.
Step 6: Pull or Deploy from GitHub
Once your repository is set up in cPanel and your SSH key is added to GitHub, you can start pulling changes or deploying code from GitHub to cPanel.
- Pull Code: If you want to pull changes from your GitHub repository, use the Git Version Control interface in cPanel. Navigate to your repository in cPanel and click the Pull button to fetch the latest changes from GitHub.
- Deploy Automatically: If you set up automatic deployment during the repository creation, every time you push changes to GitHub, they will automatically be pulled and deployed to your cPanel-hosted website.
Alternatively, you can use the terminal to pull changes manually:
cd /path/to/your/repository
git pull origin main
This will fetch the latest updates from the main
branch of your GitHub repository.
Conclusion
Connecting cPanel to GitHub streamlines the process of deploying code and managing your website or app. By generating an SSH key, authorizing it in both cPanel and GitHub, and setting up Git Version Control, you can ensure a smooth and secure connection between your development environment and your hosting. Whether you’re using the built-in Git tools in cPanel or the terminal, this integration will make managing your codebase a lot easier.
Leave a Reply