Quickie: using SVN for website development
It only took me about a decade, but I finally made the switch to using version control to manage the development of my websites. Looking back, I can only wonder what took me so long. Now I use Subversion. I am happy. Why? Here are some reasons:
- It provides a seamless way for several people to collaborate on the same project without stepping on each other's toes. (Have you ever had a collaborator delete one of your crucial files?)
- You can easily document the changes you make to your code.
- If you make a mistake, You can easily "roll back" to an earlier version.
Making the switch to Subversion is well worth the modest effort required. (If I can do it, so can you.) This "Quickie" article will take you through (1) creating an online repository for your site; (2) creating a development site for doing live testing on your live server; and (3) using Subversion in routine development work. There are, doubtless, other, better, ways to accomplish all this, but this is what has worked for me.
Assumptions in this article
- You have a basic grasp of what Subversion is, what it's for, and (roughly) how to use it
- Subversion is installed on your server and on your local computers
- You have a site (we'll call it
www.example.com) that's already up and running on your Apache server. I'll also assume your site lives in the server directory~/example.com(i.e., in your home directory) - You have login (shell) access to the server
Setup
For starters, you'll need to create two new sub-domains. Follow your web host's instructions about how to do that:
- Create two new sub-domains:
svn.example.com(for your svn repository) anddev.example.com(for your development site). [If you're using DreamHost, go to your webpanel and select Domains » Manage Domains » Add New Domain/Sub-Domain.] It's a good idea to password-protect your dev site, so that only you and your co-developers have access to your not-quite-ready-for-prime-time version of the site. - Create a new Subversion project. We'll give it the id "myproject", and install it to the
svn.example.comURL. (Don't forget the username and password you choose here!) [If you're using DreamHost, you can create a new Subversion project right from your webpanel: select Goodies » Subversion.]
The rest of the setup happens through the login shell on your server. So log on to your server and do the following:
- To hide the Subversion directories from web browsers, create (or edit) the
.htaccessfile in~/dev.example.com. Add the following line:RedirectMatch 403 /\.svn.*$
- Add this line also to the
.htaccessfile in~/www.example.com. - Create an empty trunk/branch/tag directory structure as the initial import into the project. (In the following, "YOURNAME" is your svn username.)
% cd % mkdir tmp % cd tmp % mkdir branches trunk tags % svn import . --username YOURNAME -m "initial import" \ http://svn.example.com/myproject Authentication realm: <http://svn.example.com:80> My Project Password for 'YOURNAME': {enter password} Adding trunk Adding branches Adding tags Committed revision 1. % cd .. % /bin/rm -fr tmp
- Now go ahead and import your current site (
www.example.com) into the repository:% cd ~/www.example.com % svn import . --username YOURNAME -m "importing website" \ http://svn.example.com/myproject/trunk
If your site contains some dirs that you want to exclude from the development version (e.g., wordPress), you might prefer to import the site on a directory-by-directory basis:
% cd ~/www.example.com % svn import ./keep_me1 --username YOURNAME \ -m "importing keep_me1 directory" \ http://svn.example.com/myproject/trunk/keep_me1 % svn import ./keep_me2 --username YOURNAME \ -m "importing keep_me2 directory" \ http://svn.example.com/myproject/trunk/keep_me2
Or, to be more efficient about it, you could run a little shell script like this:
#!/bin/tcsh -f foreach d ( some_dir another_dir yet_another_dir ) { svn import $d --username YOURNAME -m "importing $d directory" \ http://svn.example.com/myproject/trunk/${d} }
- Checkout a working copy to the development site
(dev.example.com):% svn co http://svn.example.com/myproject/trunk ~/dev.example.com
Your dev site (dev.example.com) now contains a full working copy of the latest revision of your site.
Routine workflow
- Checkout a working copy from the repository to your local machine. (I recommend the svnX GUI client for the Mac.)
- Develop and test your code, etc., on your local machine.
- At logical stages (say, when you fix a bug or finish writing a new function), commit your changes to the repository. Some good habits to practice: (1) Always
updateyour working copy before eachcommit. Always jot down a meaningful comment with eachcommit. (Instead of saying "Fixed a bug", say "Fixed function Foo() in bar.php that had wrong mySQL query.") - Log on to your server and update the development site:
% svn up ~/dev.example.com
- View the dev site. You may discover bugs on the dev site that weren't apparent on your local machine. (They might be using different versions of Apache, PHP, mySQL, etc.) If there are bugs to be worked out, iron them out on your local working copy (i.e., go back to Step 2).
- If you're satisfied that your dev site is good to go, then either checkout a copy to your live deployment site:
% svn co http://svn.example.com/myproject/trunk ~/www.example.com
or update the copy already in your deployment site:% svn up ~/www.example.com
- Double-check that the deployment site (
http://www.example.com) is working to your satisfaction. If it's not, go back to Step 2. If it is OK, you're done!
The development cycle is now much simpler and safer. If you go on the road, just checkout a working copy onto your laptop, make your changes, and commit them back to the repository when you get the chance. If you (or your collaborator) made a mistake 6 revisions ago, just tell svn to checkout or update to an earlier revision.
Nice!
Hey, I just set up a couple of sites to use svn, and thought I’d share a tip. You can easily set up a “post-commit” script in the hooks directory of your repository to automatically ‘update’ the dev version of your site whenever anyone does a check-in. This makes it possible to have a collaborator working on the site and able to generate the dev version, without giving them shell-access to the server where the dev site is hosted. I’ve then gone further and made a page that’s visible only in the (password protected) dev site to do a check-out to the main site.
Great guide. Thanks.
–Dave
@Dave: Good idea! You’ve just given me an incentive to finally figure out how to use those hooks. Thanks.
Thank you very much for the great tutorial, it was extremely helpful! But I’m very curious how you got the hooks set up and how you could get a page to automatically do the checkout (I’m using dreamhost also!).
@Dave 2: Glad this helped. Alas, I haven’t gotten around to hooks yet. I’ve been distracted by another, thornier problem: how to keep my site up to date with svn, while also using svn to install WordPress upgrades to my site. I’m still working out that tangle… Anyway, if I learn anything useful, I’ll post it here. Stay tuned…
I don’t know if this might help, I use it for upgrading third party code used in my solutions, it’s called a vendor branch…:
http://svnbook.red-bean.com/en/1.5/svn.advanced.vendorbr.html
@Dave 2: Yes, that’s exactly what I need to learn next! Thank you for steering me in that direction.
I’m glad someone out there is reading the manuals. Clearly I’m not.
i just wanted to say thank you for this truly great guide !!!
thank you very much!
Sean.
@Sean: You’re welcome. I’m glad it was helpful.
the easiest way to set up a post commit hook is:
- find the hooks folder in your repo
- rename (or better copy) the post-commit.tmpl to post-commit
- add this (part of might already be there):
REPOS=”$1″
REV=”$2″
/home/YOURUSERNAME/bin/svn update /home/YOURUSERNAME/public_html/YOUR_DEV_FOLDER/
nb, you might have to adapt the paths, but they have to be (i think) absolute since the hooks run without ENV variable set.
cheers Marco
Here’s another thornier problem – if you’re working in the dev. area and what you’re doing included database manipulation – adding tables, columns, etc.
How do you update database stuff given that while you’re doing this work the site owner has been updating information in the live database?
Oh, and thanks for the tutorial – I had done it once, couldn’t recall how I did it and this worked out just fine.
@Ken
You basically have to have two databases copies then, the production (live) one, and your dev one. Make a copy of the live database as a dev copy. If the data has personal info I will have a script that I wrote which will go through the dev copy (make sure you have the right db!) and overwrite whatever fields that are “sensitive” with latin words/numbers.
Before making your changes live, (and depending on how complex your site is):
1) place the main site offline (during a posted downtime notice, etc)
2) Apply any needed db schema changes, run any data conversion scripts on the production database
3) Update the code on the live site
4) Bring the main site back online
5) Test
Hey, I have a question about SVN and I’m hoping to get it answered here as it is actually on topic.
I have a requirement to setup a production web server for a development group. They want SVN installed onto it to support their deployment process. Now I’m somewhat familiar with SVN as a stand alone server, but why do I need to install SVN on a production server to support developers “putting” code to the website.
Their workflow seems similar to what your doing here. So I’m hoping you can illuminate this for me, or point me in the right direction.
@Patrick: You don’t have to install svn on the production server (“PS”). When the code is ready for release, your developers could simply copy (cp or ftp or whatever) the new code over to the PS, or email the clients a zip archive of the new website and let them install it. No svn needed.
Now what if your developers discover a few bugs in the code, and they want to update the PS to version 1.01? If they keep track of which files they modified, they can simply copy the altered files over to the PS. Alternatively, they could send the client an archive of version 1.01 for them to install. This is still quite manageable without svn.
And what if the clients don’t like some of the changes that appeared in 1.01, and they want to downgrade back to 1.00? Well, the developers could roll back their working copy to version 1.00 and then copy that over to the PS (or send an archive to the client). Again, no svn needed.
Bottom line: you don’t have to have svn on the PS. Personally, though, I find it helpful because it makes updating the PS to the latest version so very, very simple: on the PS you simply type “svn up”. That’s it. No manual copying or ftp’ing; no keeping track of which files need to be copied; fewer ways for a scatterbrain like me to screw up.
Hey John,
Thanks for the response. So do I need to have the SVN client tools installed onto the PS, but not an actual SVN server?
@Patrick: Yes, the client tools should be all you need.