Skip to content

Instantly share code, notes, and snippets.

@beppe9000
Forked from dusta/Deploy_on_cPanel.md
Last active November 20, 2020 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beppe9000/f528c5fefb33cc81822c84e641107a2f to your computer and use it in GitHub Desktop.
Save beppe9000/f528c5fefb33cc81822c84e641107a2f to your computer and use it in GitHub Desktop.
Ghetto Deployment

Deploy your site with git on cPanel

The most important: You must have shell access!

This gist assumes:

  • you have a local git repo
  • with an online remote repository (github / bitbucket etc)
  • and a cloud server (Rackspace cloud / Amazon EC2 etc)
    • your (PHP) scripts are served from /var/www/html/
    • your webpages are executed by apache
    • apache's home directory is /var/www/
    • (this describes a pretty standard apache setup on Redhat / Ubuntu / CentOS / Amazon AMI etc)

you should be able to do the same with Java, Perl, RoR, JSP etc. however you'll need to recreate the (rather simple) PHP script

1 - On your local machine

Here we add the deployment script and push it to the origin, the deployment script runs git commands to PULL from the origin thus updating your server

Grab a deployment script for your site

See deploy.php

Add, commit and push this to github (Optional)

git add deploy.php
git commit -m 'Added the git deployment script'
git push -u origin master

2 - On your server

Here we install and setup git on the server, we also create an SSH key so the server can talk to the origin without using passwords etc

cPanel should have installed git allredy.

To check run in shell account command

git --version

Generate a deploy key for apache user

ssh-keygen -t rsa # choose "no passphrase"
#Enter file in which to save the key (/home/[user]/.ssh/id_rsa): #Rewrite this Path!

3 - On your origin (github / bitbucket)

Here we add the SSH key to the origin to allow your server to talk without passwords. In the case of GitHub we also setup a post-receive hook which will automatically call the deploy URL thus triggering a PULL request from the server to the origin

GitHub instructions

Add the SSH key to your user

  1. https://github.com/settings/ssh
  2. Create a new key
  3. Paste the deploy key you generated on the server

Set up service hook

  1. https://github.com/oodavid/server.com/admin/hooks
  2. Select the Post-Receive URL service hook
  3. Enter the URL to your deployment script - http://server.com/deploy.php?key=[RANDOM_KEY]
  4. Click Update Settings

Bitbucket instructions

Add the SSH key to your account

  1. https://bitbucket.org/account/ssh-keys/
  2. Create a new key
  3. Paste the deploy key you generated on the server

Set up service hook

  1. Go to: Repo > Admin > Services
  2. Select "POST"
  3. Add the URL to your deployment script - http://server.com/deploy.php
  4. Save

First Pull from origin

git remote add origin ssh://git@bitbucket.org/project.git
git fetch
git pull origin master

Rejoice!

Now you're ready to go :-)

Some notes

  • If you have forced redirect to /web or /public_html in project create deploy.php in folder that you have .git folder and in public folder create symlink deplay.php to your deploy file.
  • Navigate the the deployment script to trigger a pull and see the output:
  • When you push to GitHub your site will automatically ping the above url (and pull your code)
  • When you push to Bitbucket you will need to manually ping the above url
  • It would be trivial to setup another repo on your server for different branches (develop, release-candidate etc) - repeat most of the steps but checkout a branch after pulling the repo down

Sources

<?php
/**
* GHETTO DEPLOYMENT SCRIPT - WEBHOOK RECEIVER
* Update folder from external download zip
*/
$config = [
'tokenList' => [hash('sha256', $_POST['omnia'])], // sha256(password) // deny all if empty
'targetPath' => null, // web root if null
];
$config = json_decode(read_file('~/ghetto_deploy_keys/yoursite.com.sha256.txt'));
$key = hash('sha256', $_POST['token');
$v1 = verifyToken();
$v2= verifyZip();
if(!verify()){
header('Location: ./');
die('NO.');
} else {
$type = null; $path = null;
$dl = download($type, $path);
switch($type){
case 'zip':
$output = installZip($zip,$config['targetPath']);
break;
}
}
function verify(){
return (in_array($key,$config['tokenList'],true));
}
function verifyZip(){
return !empty($_POST['payload'])
}
function decompress($zip, $target){
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo($targetDir);
$zip->close();
return true;
} else {
return false;
}
}
function installZip($zip, $target){
decompress($zip, $target);
// array of commands
$commands = array(
'echo $PWD',
'whoami',
'git checkout -- .',
'git pull',
'git status',
'git submodule sync',
'git submodule update',
'git submodule status',
);
chdir("/home/[SET_PATCH_TO_PROJECT]/public_html");
// exec commands
$output = '';
foreach($commands AS $command){
$tmp = shell_exec($command);
$output .= "<span style=\"color: #6BE234;\">\$</span><span style=\"color: #729FCF;\">{$command}\n</span><br />";
$output .= htmlentities(trim($tmp)) . "\n<br /><br />";
}
}
function output() {
?><!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>GIT DEPLOYMENT SCRIPT</title>
</head>
<body style="background-color: #000000; color: #FFFFFF; font-weight: bold; padding: 0 10px;">
<div style="width:700px">
<div style="float:left;width:350px;">
<p style="color:white;">Git Deployment Script</p>
<?php echo $output; ?>
</div>
</div>
</body>
</html><?php }
function download(&$type, &$path) {
$type = 'zip'; // download a zip file here
$path = null; // path of zip file to be extracted
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment