Git Deploy Craft CMS on Media Temple’s Grid Service

How we got the code on the server and how we update the code without needing to FTP

December 12th Articles 7 min read
Clock

Save time up front

When launching any site, not just Craft, we prefer to have a remote repo on the server with a post-receive hook so we can git push only changed files from the latest commits to the server. This saves loads of time when compared to the archaic method of FTPing changes to a server. No more needing to upload files one-by-one or uploading whole directories.

Normally we use and recommend a VPS, either on DigitalOcean or Rackspace. Due to their speediness and customization but we don't always get the option to use a VPS when launching a site. Either due to the client wanting 24-hour server support, not having the technical know-how or manpower, or needing the server to be completely managed. So some clients decide to use an inexpensive managed host to save money, which means us developers don't always have full control of the server environment.

This client opted to use Media Temple, specifically their Grid Service. Thankfully, Media Temple's Grid Service allows for SSH with certain permissions - unlike most shared hosts.

So here's how we setup a Media Temple Grid Service shared hosting server with Git Deployments to allow for quick and easy code updates.

Note: These instructions assume that you've been using Git to version control your project and that you're comfortable using a Unix shell.

Save Time Up Front

Setting up SSH

  1. Ensure SSH is Enabled: MediaTemple shows you how to do that here in their KnowledgeBase.
  2. Add SSH Keys to Server: MediaTemple shows you how to do that here in their KnowledgeBase.

SSH into the server

I'm using sXXXXXX as the server name throughout the examples. You will need to update that with your server name or domain name.

ssh sXXXXXX.gridserver.com@sXXXXXX.gridserver.com

Grid Service comes with Git already installed. Do the following to create the repo:

mkdir repo && cd repo
mkdir site.git && cd site.git
git init --bare
Hooks 2X

Create the post-receive hook

ls

You will see a few files and directories, including the hooks directory. So let's change to that directory:

cd hooks

Now, create the file post-receive and edit it by typing:

touch post-receive && nano post-receive

Modify and then add the following once in nano:

#!/bin/bash -l
echo "----------------------------------"
echo "--- Running git post-receive -----"
echo "----------------------------------\n"
git clone ~/repo/site.git ~/temp/site
echo "Copying"
cp -r ~/temp/site/* ~/domains/yourdomain.com
echo "Moving www to html for MediaTemple's public folder"
cp -r ~/domains/yourdomain.com/www/* ~/domains/yourdomain.com/html
echo "Removing www folder"
rm -rf ~/domains/yourdomain.com/www/
echo "Removing temp files"
rm -rf ~/temp/site/
echo "----------------------------------"
echo "----------------------------------"
echo "------ post-receive complete -----"
echo "----------------------------------"
echo "----------------------------------"

When you finish typing, press control-x, y, and then return (enter key) to save. In order to execute the file, we need to set the proper permissions using:

chmod +x post-receive

Exit SSH

exit

Local machine

Now, on you're local machine, we need to add the remote.

cd yourProjectDirectory
git init .
git remote add prod
ssh://sXXXXXX.gridserver.com@sXXXXXX.gridserver.com:/home/XXXXXX/users/.home/repo/site.git 

Ensure the remote was added correctly:

git remote -v

Push to remote repo:

git push prod master

That's it! Hopefully this was helpful.

You should be able to easily modify these instructions to fit most servers that allow SSH and projects that aren't Craft.

Let us know if we've missed anything.

Got another dev challenge that you need help tackling? Let's Chat.

Stay Connected

Get our insights delivered straight to your inbox

More About Us

Learn more about our Culture, who we work with, and what we specialize in.