[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 [email protected]

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. [email protected]: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.

Monsoon Framework – A Lightweight, Secure, Rapid MVC PHP Framework

Monsoon Framework (monsoonphp.com) is a lightweight, secure and fast PHP framework designed for enterprise-level rapid application development (RAD) needs. It contains a folder structure that serves as placeholder to insert PHP code for your web application.

The framework is useful in creating a simple, fast and secure application quickly. It gives you an advantage to maintain Application code, API code as well as CLI based code in a single code repository. You can bring in your own composer libraries into this composer-ready framework.

Getting started is very easy with just 2 commands.

composer create-project monsoon/framework <my_project_name>
php -S localhost:8080 -t public/

Monsoon framework includes these conventions

  • PSR-1, PSR-2 and PSR-4
  • Model-View-Controller pattern
  • PDO (MySQL)
  • Composer
  • Docker
  • PHPUnit
  • PHPCS
  • PHPMD

The framework carries a MIT licence, which gives you a flexibility to use it in commercial projects. This is also an opensource framework intiated by Lightracers, giving more flexibility for developers to contribute.

Visit monsoonphp.com to know more about this new framework.

Configure XDebug on WAMP and Netbeans

Xdebug is an extension for PHP to assist with debugging and development. It contains a single step debugger to use with IDEs; it upgrades PHP’s var_dump() function; it adds stack traces for Notices, Warnings, Errors and Exceptions; it features functionality for recording every function call and variable assignment to disk; it contains a profiler; and it provides code coverage functionality for use with PHPUnit. This article contains a step by step instruction guide in configuring XDebug on your WAMP Server (http://www.wampserver.com/en/) on Windows.

  1. Before starting, make a note of the PHP version enabled, its Compiler and Architecture values from phpinfo().
  2. Go to https://xdebug.org/download.php and download the relevant .dll file based on the information collected in step 1.
  3. Place the downloaded dll file in your WAMP’s PHP folder. e.g. C:\wamp64\bin\php\php7.1.9\zend_ext\php_xdebug-2.5.5-7.1-vc14-x86_64.dll
  4. Open php.ini from the WampServer’s manager in the system tray, i.e. [W] > PHP > php.ini. Alternatively, you can find the php.ini in use in the WAMP folder also. e.g. C:\wamp64\bin\apache\apache2.4.27\bin\php.ini
  5. Add the code below to the bottom of your php.ini. (replace the path with the relevant one)
    [xdebug]
    
    zend_extension ="c:/wamp64/bin/php/php7.1.9/zend_ext/php_xdebug-2.5.5-7.1-vc14-x86_64.dll"
    
    xdebug.extended_info=on
    
    xdebug.profiler_enable = on
    
    xdebug.profiler_enable_trigger = on
    
    xdebug.profiler_output_name = cachegrind.out.%t.%p
    
    xdebug.profiler_output_dir ="c:/wamp64/tmp"
    
    xdebug.show_local_vars=0
    xdebug.remote_enable=on
    
    xdebug.remote_handler=dbgp
    
    xdebug.remote_host=localhost
    
    xdebug.remote_port=9000
    
    xdebug.remote_autostart=on
    
    xdebug.idekey="netbeans-xdebug"
  6. Change the below parameters to the given values.
    output_buffering = Off
    
    implicit_flush = On
  7. Save the php.ini file and restart the Apache service
  8. Verify that xdebug is enabled in your WAMP’s phpinfo page. Go to http://localhost/?phpinfo=1 and look for the section – xdebug.
  9. Configure your IDE to use xdebug

Configuring Netbeans

  1. Go to Tools menu > Options > PHP > Debugging and give the following options.
    1. Debugger port: 9000
    2. Session ID: netbeans-xdebug
    3. Maximum Data Length: 2048
    4. Stop at first line: checked
    5. Show debugger console: checked
  2. Click on OK to save the configuration
  3. Open your desired PHP file and place a breakpoint beside the line number of your choice
  4. Go to Debug menu and click on Debug Project or Debug File
  5. Netbeans will start listening to XDebug on the given 9000 port.

In this way, you can debug your PHP script using XDebug.