Using Subversion to version control websites
I can remember when I first started developing oh-so-many years ago, instinct and common sense told me I should implement some sort of version control, even though at the time I was writing simple MS-DOS Basic applications, and working by myself. At strategic intervals, if I remembered, I took a copy of my source code, renamed it and put it somewhere ’safe’. In reality I didn’t, but I knew I should so that if all else failed I had something to go back to.
As my applications started getting more complicated, as the business risk of getting it wrong increased, and especially when I started working collaboratively with other developers, the need for a ‘better way’ grew. I was aware of systems like CVS, but they didn’t seem entirely suited to our web development environments and the challenges we faced. We needed something which didn’t unduly get in the way of our work, and helped with as many problems and risks as possible:
- Audit trail - who changed what, when, to get the code get into the current state?
- Comparison - what changed between two versions
- Collaboration - assisting multple developers working on the same files
- Branching and Merging - the ability to create parallel threads of development, and combine them back together if required
- Disaster recovery - assistance reconstructing sites in a hurry
- Code migration -Â keeping envirnoments in sync, and moving changes between them
The break came, as with most things, with the opportunity to spend a bit of time ‘playing’ with possible solutions. First thing was giving Subversion a go - although similar in many ways to CVS, I found it much more suitable for my needs in that it was more usable, a ‘version’ represents the entire codebase rather than a specific file, and commits are atomic meaning if something fails it cancel the entire transaction rather than leaving a working copy ‘broken’ or inconsistent.
Now I had a tool I liked, how to apply it to our needs? My primary concern was moving sets of amends between environments - I wanted to get this automated and as safe and foolproof as feasible. Yet it had to be an easy process that anyone (within reason) could do.
So the files on the webserver, or any other environment which needs to be covered by version cnotrol, should be a working copy - that is checked out from a repository using a Subversion client. The new method to get changes to the live site is to make them in a dev environment, commit them to the repository (with a suitable log message of course) then ’svn up’ the live site.
One stumbling block was that different environments tended to need different configuration - whether it was filesystem path, url of the web root, or database connection details. Like all well structured applications this configuration was already in a single global include file - but this was a very risky file to have under version control. It was too easy to have a locally modified file overwritten, or even worse to accidentally commit a dev server’s config and update (and break) a live site.
The solution - don’t version environment configuration!
Put all your environment specific code in a file named for instance .config.local ( the dot prefix prevents Apache from serving requests for this file which would be a bit of a security loophole) placed in the sites DOCUMENT_ROOT and setting a global ignore on *.local in all svn client config. Now in every script you can ‘include ($_SERVER['DOCUMENT_ROOT'].’.config.local’);’ and you’re away, safe in the knowledge that you don’t have to keep an eye out for this dangerous file.
Of course that’s a simplification of the task for safe management of sites, but it demonstrates the point. Neither does it take into account all the potential issues surrounding managing websites - changes in the unprotected config file, filesystem permissions and database schemas to name but a few; but with a bit of thought and some good working practices Subversion can usually help out to some degree, if only to keep manual documentation of such things in the same place as the code.
But that’s another post someday … what is for certain is that properly used and suitably configured, Subversion is most definitely worth the effort, and it could save your life one day - or at least your job.









I’ve been using darcs for version control. The nice bit about darcs over svn is that it uses a ‘patches’ system to manage changes. Which means when I have local stuff that’s different between versions (like ftp root, etc), all I do is run ‘darcs record’ immediately after making the localisation changes, then just never push that ‘local changes’ patch to other instances of the repository. It’s worked for the last couple of years without problems - I haven’t used svn much so I can’t really compare them, but darcs conflict resolution is pretty neat, and I’ve never ended up breaking mbUK live while pushing changes from the testing repository.
Another handy thing I picked up (off Ben) was to create a folder with all the server config files in (ie, apache’s conf, etc), then symlink from /etc/apache2/sites-available/mysite –> /home/myuser/site/config/apacheconf, where ~/site is under version control. Useful if you have non-standard server configs.
If you want to version control database schema, it’s but the work of a moment to create a script that mysqldump’s your DB structure to a file, which can then be version controlled. But then you know that.
Have you come across Trac at all? Really nice bit of software project management software. Nice ticketing system, and you can hook it up to your SVN or Darcs repository too.
Comment by mat — March 16, 2008 @ 5:23 pm
[...] configuration include called at the start of each page that’s not under version control (for obvious reasons), and on this server it somehow acquired a couple of line breaks at the end after the closing php [...]
Pingback by » PHP ob_gziphandler “Content Encoding Error” | Povich - Jim Whiteside spouting squit — December 17, 2008 @ 3:56 pm