Skip to content

Instantly share code, notes, and snippets.

@dusta
Forked from oodavid/README.md
Last active April 22, 2024 18:16
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save dusta/56dbbc0dec0aebd9196538088d2fd758 to your computer and use it in GitHub Desktop.
Save dusta/56dbbc0dec0aebd9196538088d2fd758 to your computer and use it in GitHub Desktop.
Deploy your site with git on cPanel
<?php
/**
* GIT DEPLOYMENT SCRIPT
*
* Used for automatically deploying websites via GitHub
* Based on: https://gist.github.com/oodavid/1809044
*/
$key = '[SET_YOUR_RANDOM_KEY]';
if ($_GET['key'] != $key){
header('Location: ./');
die();
}
// 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 />";
}
?>
<!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>

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

@umanga907
Copy link

Where do i put the deploy.php file on server or on github ?

@dusta
Copy link
Author

dusta commented Aug 9, 2019

@uumanga907 This file you put on server only. Should not be on git because you must edit this set up key and path unless you put this in env.

@umanga907
Copy link

@uumanga907 This file you put on server only. Should not be on git because you must edit this set up key and path unless you put this in env.

It gives me an error, Invalid HTTP response: 302

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment