首页 > 代码库 > Configure SVN (Subversion) Server

Configure SVN (Subversion) Server

Note: It may need root user permissions to execute following commands.

(1) Install needed packages

yum install mod_dav_svn subversion
In my PC, packages that were installed are

Note: if you are going to visit SVN repository via http, you shall have apache HTTP server installed firstly.

yum install –y httpd
httpd-2.2.15-30.el6.centos.x86_64 and httpd-tools-2.2.15-30.el6.centos.x86_64 were installed automatically.


(2) Create and configure SVN repository
mkdir /home/svn
cd /home/svn
svnadmin create prjname
After the preceding commands were conducted, your first SVN project with name of prjname would have been created. You need to configure access permissions for the project.
cd /home/svn/prjname/conf
vi svnerve.conf
vi passwd
vi authz
A good sample of these files can be found below.


#FILE svnerve.conf
[general]
anon-access = none
auth-access = write
password-db = /opt/svn/conf/passwd.conf
authz-db = /opt/svn/conf/authz.conf
#FILE passwd
[users]
username = password
#FILE authz
[groups]
admin = username, username1, username2
[prjname:/]
@admin = rw
* =


The conf files let username with password have access to the project. Use the below command to start SVN server.

svnserve –d –r /home/svn

svnserve –d –r /home/svn

Note: if you are going to visit SVN repository via http, you need to configure the http SVN user separately.
vi /etc/httpd/conf.d/subversion.conf

vi /etc/httpd/conf.d/subversion.conf

A good sample of the file can be found below.


LoadModule dav_svn_module     modules/mod_dav_svn.so
LoadModule authz_svn_module   modules/mod_authz_svn.so
# The /svn is according to the repository that you created before.
# The DAV svn shall be just DAV svn no matter the repository name of your SVN server.
 <Location /svn>
   DAV svn
   SVNParentPath /home/svn
   AuthType Basic
   AuthName "the project name that you want it to be displayed"
   AuthUserFile /etc/svn-auth-users
   Require valid-user
</Location>


As you can see, the AuthUserFile was specified to be /etc/svn-auth-users. This is because that the http will require a cipher-texted password for the user, which is different from the former …/svn/prjname/conf/passwd file. You can use htpasswd command to create that file for http login user.

htpasswd -cm /etc/svn-auth-users username

After you input the password for the user, you will find a new file svn-auth-users was created with some cipher text inside.

And to let Apache server have access to the SVN server, you have to do some more configuration.

chown -R apache.apache prjname
## If you have SELinux enabled (you can check it with "sestatus" command) ##
## then change SELinux security context with chcon command ##
chcon -R -t httpd_sys_content_t /home/svn/prjname
## Following enables commits over http ##
chcon -R -t httpd_sys_rw_content_t /home/svn/prjname

At last, please restart the apache server to let the configuration come effect.

Service httpd restart

Go to http://SVN_SERVER_IP:port/svn/prjname/ to visit SVN repository.


(3) Create trunk, branches and tags structure
It’s not necessary to have any of trunk, branches and tags files under the project home, but usually we can find these there files in most famous open source projects. Developers develop and submit under the trunk directory. For some uncertain requirements or a stage release version, it is better to save them in the branches directory. At last tags is read-only which is used to save phased production version for archiving.  A tree like structure of the project will be similar as below.

- Project_svn_home
+ trunk
++ main.cpp    (latest version which is under developing)
++ common.h
+
+ branches
++ -- r1.0
++ -- + main.cpp (Latest of 1.x version)
++ -- + common.h
++ -- r2.0
++ -- + main.cpp (Latest of 2.x version)
++ -- + common.h
+
+ tags (read-only)
++ -- r1.0
++ -- + main.cpp (version 1.0 release)
++ -- + common.h
++ -- r1.1
++ -- + main.cpp ( version 1.1 release)
++ -- + common.h
++ -- r1.2
++ -- + main.cpp ( version 1.2 release)
++ -- + common.h
...
++ -- r2.0
++ -- + main.cpp ( version 2.0 release)
++ -- + common.h
++ -- r2.1
++ -- + main.cpp ( version 2.1 release)
++ -- + common.h
...

There are two ways to create trunk, branches and tags directory structure.
1) Use the svn mkdir command to either make your directory structure without a working directory.
svn mkdir -m "structure-trunk" svn://localhost/prjname/trunk 
svn mkdir -m "structure-trunk" svn://localhost/prjname/branches
svn mkdir -m "structure-trunk"  svn://localhost/prjname/tags

OR

svn mkdir -m "structure-trunk" file:///home/svn/prjname/trunk 
svn mkdir -m "structure-trunk" file:///home/svn/prjname/branches
svn mkdir -m "structure-trunk" file:///home/svn/prjname/tags


It may be possible that you have to write some comment to the log file when you are adding these directories without ‘-m‘ option.
2) Or, you can checkout a copy of the repository in into a working directory and do everything from there.

svn co svn://localhost/prjname
cd prjname
svn mkdir trunk tags branches
svn commit -m "Creating basic directory structure"
Notice that you will not see the directories directly inside your repository. However, you can use the svn ls command to see the structure.

svn ls -R svn://localhost/prjname

OR

svn list svn://localhost/prjname

(4) Now you can connect prjname with any type of project in a reachable URI, or you can just check it out and then expand it as your like.
To checkout current developing codes in the trunk directory, you can use the following commands in a shell.
cd /home/user/projects/
svn co svn://localhost/prjname/trunk prjname -username your_user_name

After authentication your user name and password, you will find the project is checkout with the project name that you specified.
However, it’s very possible we want to use some IDE to initialize the project. As with me, I would create a dynamic WEB project by Eclipse and then share it to SVN.
For example, you can create a Dynamic Web Project with the Eclipse.
Then you can right click the home folder of the project and select the Team menu. You can take use the share project function to connect your project with SVN prjname.
svn://svnserver_IP:port/prjname
[Use specified folder name] trunk/

The default SVN port is 3306.
If the hosts of Eclipse and the SVN server are in different private LAN, then port mapping in router can be taken use of to connect the hosts.
I will going to discuss more aspects about SVN such as auto commit in a next article.
Note: It usually can be more comprehensive and helpful to refer to  the subversion manual page http://svnbook.red-bean.com/ if you have questions.