[Tutorial] cPanel Push (Automatic) Deployment of PHP application from private GitHub repository

cPanel is (still) a popular server management software preferred by many IT managers. Web-based access makes it easy to manage and modify the server from any where. cPanel is often an integral part of WHM (Web Hosting Manager) which can control multiple cPanels and automate server management tasks easily. In the moden days, with the advent of DevOps process, we can automate the deployment of PHP application directly into cPanel using the popular code versioning systems such as Github or Bitbucket.

In this tutorial, we will implement Automatic Push deployments of PHP Application in cPanel from private Github repository. We will look at pulling code from a private repository into a selected path on your cPanel enabled server.

Step 1: Create a private repository on Github

Create a private repository as mentioned in this link – https://help.github.com/en/articles/create-a-repo

Add some code into this repository.

Step 2: Manage SSH keys between cPanel and Github

[Ref: https://documentation.cpanel.net/display/CKB/Guide+to+Git+-+Deployment ]

  • Go to cPanel > Advanced > Terminal and generate a key with this command. Important: DO NOT GIVE ANY PASSPHRASE. Leave it empty.
            ssh-keygen -t rsa -b 4096 -C "username@domain"
  • Copy the public key generated using this command
            cat ~/.ssh/id_rsa.pub
  • Leave the terminal window open, and go to your Git respository in another browser tab
  • Go to repository Settings > Deploy Keys and click on “Add deploy key” button
  • Paste the copied key in the text box and click on “Add Key” button
  • Go back to your cPanel terminal window and give this command to confirm the access. If the key is working, you will get to see a “Hi” message.
           ssh -T git@github.com

On your local system, generate a set of SSH keys to be imported into cPanel. Follow the same procedure given above to generate public and private keys. REMEMBER TO NOT GIVE ANY PASSPHRASE during the key generation.

  • Go to cPanel > Security SSH Access
  • Click on “Manage SSH Keys” button and click on “Import Key” button
  • Give a different name (other than id_rsa) and enter the private key and public key content from your local system files
  • Go to cPanel > Security SSH Access  and click on “Manage” button against your imported key
  • Click on “Authorize” button

Note: If you don’t add your local key in cPanel, you have to manually pull the code changes each time, because the repository is residing outside cPanel. To skip that process, this step is required.

Till this point, you have configured cPanel to access Github. Now we create a cPanel counterpart to link Github repository

Step 3: Creating repo in cPanel

  • Go to cPanel > Files > Git Version Control
  • Click on “Create” button
  • Enable “Clone Repository” slider and give the Github repository url in the “Clone Url” textbox. This URL would be the SSH URL of your Git repository. E.g. git@github.com:lightracers/ourprivaterepo.git
  • Enter the required path in “Repository path”
  • Give a proper name in “Repository Name”
  • Click on “Create” button

You will be shown a screen with a “Clone Url” which has your cPanel user name. Copy this URL. You would need use this in your local system.

Step 4: Updating Git references

Some of the online references will tell you that a Webhooks are automatically configured in cPanel. Automatic webhooks would work only if repository is with cPanel. It would not work in our case, because the actual repository is not with cPanel but with Github. cPanel has just cloned the repo. So we would need to update the cPanel references in your local clone.

When you modify code on your local system and “git push” it, it would go into just Github. By adding an extra reference as a pushUrl, updated code would also go to your given url.

Give a command similar to this, in local terminal inside your code repository.

git remote set-url --add --push origin ssh://username@mycpanelhost:2232/home/username/<path>

This command will add the cPanel URL as a pushUrl under [remote “origin”] section in your git config (.git/config).

Step 5: Push some code

Lets try to push some code now. Go ahead and make some modifications in your code and give these commads. You should see output from Github as well as from cPanel.

git add .
git commit -m "Push to cPanel"
git push

Go to cPanel > File Manager and open the “Repository path” you have specified in Step 3. You should be able to see the updated code automatically showing up over there.

If yes, congrats you can now work in a much simpler way. If something goes wrong, try to understand the error and see if you have missed any of the above connecting chains.

Let us know if something doesn’t work for you in the comments below. We will try to help you.