Deploying WordPress On MediaTemple Grid Server Using Git

<!-- 2022 Me: I wrote this in 2015 but never finished (or published) it. I haven't used Media Temple in a long time so this is probably super outdated info, but I'm posting it anyway. -->

I’ve been hosting sites with MediaTemple since 1983. Or something like that. It’s been a long time. I think I first heard about them because k10k was hosted there. Now it’s not hosted anywhere because that was a long time ago and I’m old.

Anyway, in more recent years I’ve been looking for a way to set up a Wordpress site with both a staging and production environment so I can test changes before pushing them to the live site. Speaking of pushing, I also wanted to be able to “deploy” changes with a single command rather than having to connect through FTP and then remember which files I changed so I can upload them one at a time and hope that no one tries to access the site while any of this is happening.

I finally figured out a way to do it that works pretty well so I’m posting it here for anyone else who may find it useful, including myself in the near future when I forget how to do it.

Create a Git Repo on Your Grid Server

First you’ll need a git repo on your server. This is where you’ll push your code instead of uploading files manually. We’ll then tell Git (using a hook) to automatically check out your code into the proper directory (/wp-content/themes for a WordPress theme for example).

To create a Git repo on your server you’ll need to connect through SSH. If you don’t already have that set up you can find instructions on how to do so here [ADD LINK].

Another thing to consider is where you’re going to put the repo. In my case I decided to create a subdomain to keep it separate from the rest of the site. I went with the obvious choice: git.29thfloor.com but you can call it whatever you want (and put it wherever you want).

Once you decide where you’re going to put it, you connect via ssh:

[bash]$ ssh domain.com@sXXX.gridserver.com[/bash]

(where sXXX is the number of your Grid server. You can find that in your account).

If you don’t have SSH keys set up you’ll need to enter your password to connect, which gets really annoying so I would recommend doing that at some point. [Add Link]

Navigate to wherever you want and create the repo like such:

[bash]
$ cd ~/domains/git.yourdomain.com/html
$ git init –bare
[/bash]

Side note: I put mine in the html folder since that’s the web root (ie, http://git.29thfloor.com points there). You may be able to put it in the root subdomain folder but I’m not sure.

Add this new repo as a remote:

[bash]$ git add remote grid domain.com@sXXX.gridserver.com:domains/git.domain.com/html/repo[/bash]

And push your code up:

[bash]$ git push grid branch-name[/bash]

Wait, what code are you pushing?

I should mention that it’s important to create your repo so it contains only your WordPress theme files [write post?], otherwise you’ll be pushing the entire site and no one wants that. Well, maybe somebody does but they’re probably an insane weird asshole.

Now that you have your git repo on your Grid server, you’ll need to set up a Git hook that tells Git, “When I push changes to this repo, you should also check out the code into this other folder on the server”. The way to do that is with a post-receive hook.

To create that we’ll need to use our ssh connection again and navigate to the hooks folder within the git repo

[bash]$ cd ~/domains/git.yourdomain.com/html/hooks[/bash]

Make a copy of the same post-receive hook (so we’re not editing the original)

[bash]$ cp post-receive.sample post-receive[/bash]

And then edit the new file with vi

[bash]$ vi post-receive[/bash]

Now, if you’re never used it before (and even if you have), vi may seem like something only a complete maniac would use. And you would be right. But that’s what we’re gonna do because I’m not sure how else to do it.

Once you run the previous command, you’ll be editing the file in vi. You’ll be able to move the cursor around but you’ll probably notice that typing doesn’t do anything. That’s because you’re not in ‘editing’ mode, or ‘insert’ mode or whatever it’s called.

Enable editing by typing the letter ‘i’.  Now you should be able to edit the contents of the file.

Delete whatever’s there.

Type this:

[Add hook contents]

You’ll need to update the paths and stuff.

This hook tells Git that, if you push the ‘master’ branch it should copy the changes to the live site. If you push any other branch it will copy changes to the staging site. If you don’t have a staging site or are running one locally you can remove that part of the code.

To save your changes and exit vi, type (I kid you not):

:wq

Now, if everything went according to plan, when you push to your new Git repo you should see an additional message telling you that the latest commit was deployed to the site.