I’ve been busy lately with lots of side projects (including the new theme surrounding this post), and one key tool that helps keep my work organized and sane is Subversion. A while back, I discovered that my web host of choice, MediaTemple offers two ways to host Subversion repos for your own usage alongside regular web hosting: you can make use of a single Apache webserver driven Subversion repository, or configure full-blown Subversion over secure SSH tunneling. Here’s how to get the latter up and running, complete with a hosted checkout of your work that auto-updates after your commit.
Enable SSH Access
By default, SSH remoting is disabled on MediaTemple accounts, requiring you to explicitly enable it in the Server Administrator control panel section for your primary domain:
Once there, set SSH on “Enabled”, and save your changes. MediaTemple’s easy-to-use configuration tool couldn’t make this step any simpler.
Connecting and Creating a Repository
Fire up Terminal in your Applications/Utilities folder under Mac OS X, or grab the latest version PuTTY under Windows, and connect to your MediaTemple host as firstname.lastname@example.org. For example, if your site were example.com, you would connect as follows:
The domain “example.com” is in there twice, first because it’s part of your full username, and second to point SSH at the right server. (If your SSH client complains about not being able to connect, you can try replacing the first @ sign with its encoded version, “%25″.) Type “yes” to confirm adding example.com to your known_hosts, if asked, then enter your administrator password to finish logging in.
Enter the following commands to switch to your “data” directory (provided by MediaTemple), and create a basic repository for your code/information:Change directory (cd) to your data directory:
cd ../../data/Create a “repos” directory to hold one or more repositories:
mkdir reposChange directory to the folder you made in the previous step:
Create a repository (replace “tests” with your desired repository name, using underscores for spaces). Letter case is important, so if you capitalize any part of the name now, you’ll have to capitalize it later when using it. The bit about “fsfs” is to use a specific Subversion database type recommended by MediaTemple — it’s the default type for newer versions of Subversion, but it’s best to put it just to be safe.
svnadmin create tests --fs-type fsfs
You can repeat this to create as many repositories as you wish inside the “repos” folder.
Log out of MediaTemple:
Checking Out Your Repository
On your local computer (not on MediaTemple), decide where you want to keep your repositories. I recommend someplace easy to get to like /workingcopies/, but a repo can go anywhere you prefer. Check out a copy of the repo you just made:
svn checkout svn+ssh://email@example.com@example.com/home/1234/data/repos/tests /workingcopies/tests
Be sure to replace “1234″ with your four-digit MediaTemple server ID number that can be found in your Service Activation email.
To verify everything drop a simple file into your new repository folder at /workingcopies/tests, such as test.txt, and do:
svn add test.txtfollowed by
svn commit -m "Adding test file."
If you have some code stored in a repository that you’d like to automatically be made available to the world, such as a WordPress theme or web application, Subversion provides various “hooks” that can be run at times like pre-commit, post-commit, etc. Subversion hooks are nothing more than scripts which you define, and get run when the applicable action occurs. Here’s how to set up an automatically updating WordPress theme repository:
Connect to MediaTemple again, using the initial ssh command at the start of this tutorial, then change directory to where you want to use this repository on your MediaTemple server:
Check out a copy of your repository elsewhere on your server, using file:// instead of svn+ssh://
svn checkout file:///home/1234/data/repos/tests tests
Again, replace “1234″ with your server ID, and also replace “tests” with your repository name (in both places). Now you have a copy of that repository on your server. Edit the hook so it gets updated automatically when you commit changes:
Make a copy of the post-commit hook example:
cp post-commit.tmpl post-commit
Use your favorite command-line text editor the post-commit script:
nano -w post-commit
Change the entire script (below #!/bin/sh) to:
svn update /home/1234/domains/example.com/html/wp-content/themes/tests
Exit, saving changes, with Control-x, y, Enter.
Make your post-commit script executable:
chmod +x post-commit
Finally, log out of MediaTemple again:
Now, after a commit, Subversion will automatically update the repo you have checked out on your server, and your changes should be reflected almost instantly.
There’s a lot more to using Subversion, so here are some hand-picked resources to keep you going:
- Setting up Ubuntu Hardy
A good reference for setting up SSH keys, which ensures only you can log in remotely, and helps automate repetitive password entry
- HOWTO: (gs) SVN multiple users
How to allow multiple users to access your repositories on MediaTemple
- Version Control with Subversion
The free Subversion book/manual
- Versions and Cornerstone
Two great Subversion applications for Mac
The standard for Subversion under Windows