How to setup an svn server

posted on: 2010-07-18 11:09:39



This is a guide to setting up an svn server.

First be sure that you have subversion installed on your server. If you wish to use the webdav apache module, then you will also need libapache2-svn. If you don't wish to use webdav, then leave this package out. I would recommend that you use webdav as you would use the https port of 443. Which is already open on most firewalls and will already be open on most routers. Using webdav also means that you will not need to create real linux users for each person that you wish to give access to in order to use svn, these can be managed using a webdav config file, which I will show you later. I am using a debian based distro, if you are using another distribution your package management tool and package names may vary.

apt-get install subversion libapache2-svn

Depending on your own need and circumstance I recommend that you create a specific directory to keep all your svn projects in. I usually keep mine in the /var directory.

mkdir /var/svn/

Next we need to actually create the repository that we are going use. We must do this using the svnadmin tool. The command create will create an svn repository for you, if the directory doesn't exist it will also create it for you.

svnadmin create --fs-type fsfs /var/svn/yourproject

As most people who use svn will require multiple users we will add a new group called svnusers, and we can add any linux users to this group.

groupadd svnusers

You can add users to your svn group the same way you would with any linux user to any group.

NOTE: If your users will primarily be accessing via https/webdav you will not need to add your users to this group. But it still may be a handy idea to add any existing linux users that already exist on the server who would require access svn on the server, as if for whatever reason apache fails (highly unlikely) there is always ssh+ssh.

addgroup user svnusers

Now you need to set the permissions for the repository you created. The apache user is required for webdav access. But the group owner we will set as svnusers. We will give both full access and we will not let everyone else have access.

chown -R www-data:svnusers /var/svn/*
chmod -R 770 /var/svn/*

Now at this point if you have ssh access you can test that you have setup your repository correctly by importing a file from your local computer. To do this you will require ssh access for the server you are using and your user will need to be in the svnusers group.

mkdir /tmp/foo
touch /tmp/foo/test.txt
svn import -m "first commit" /tmp/foo svn+ssh://yourserver.com/var/svn/yourproject/trunk

Now you test checkout your new project.

svn co svn+ssh://yourserver.com/var/svn/yourproject/trunk /var/www/yourproject

At this point you currently have all you would need if you just wanted to use svn+ssh to connect to your svn repositories. If you wish to configure webdav, then read on.

To be on the safe side you are recommended to run the following commands. The webdav mods should have been configured automatically. But just to save on possible headaches you should load them anyway as it won't do any harm.

a2enmod dav
a2enmod dav_svn

You need to create a password file for webdav to use. The htpasswd2 program is your friend here. Upon adding each user you will be prompted to enter a password for that user.

To create the file password file you will need add the -c option.

htpasswd2 -c /etc/apache2/dav_svn.passwd user1

but for each addidtional user you can just call like this.

htpasswd2 /etc/apache2/dav_svn.passwd user2

In order to access your repositories through webdav you will need to make an entry for each repository in your dav_svn.conf file located in the /etc/apache2/mods-available/ directory.

nano /etc/apache2/mods-available/dav_svn.conf

You will need to customize the below configuration for each of your repositories. Make sure that your SVNPath is pointing to your repository location and that your AuthUserFile is also pointing to the correct location (if different from the location in this tutorial). In the opening Location tag, the path you put here is how you will be accessing your repository from url, as you will see below.

	<Location /yourproject>
	  DAV svn
	  SVNPath /var/svn/yourproject
	  AuthType Basic
	  AuthName "yourproject repository"
	  AuthUserFile /etc/apache2/dav_svn.passwd
	  Require valid-user
	  SSLRequireSSL
	</Location>
 

After you have made an entry in your dav_svn.conf file you will now need to restart apache.

/etc/init.d/apache2 restart

You can now test your webdav configuration by checking out your project over https

svn co https://yourserver.com/yourproject /var/www/yourproject

Well now you should have a fully configured and fully functioning svn repository. My closing advice simply has to be when committing changes make sure that you always, always enter comments. Each time you commit, no matter how small the change, you have done something. No matter how insignificant the change it is important that you write what you have done. This will help both you and others in the future when browsing revision history. And also if you are like me and can't remember what you were doing last week, let alone last month :) .