1
1

More than 1 year has passed since last update.

EC2にsvnを構築

Posted at

Plannning-Venue Advent Calendar 2021の9日目です。

この令和の時代にsvnかよって感も否めないですが、クローズドな環境(EC2上に立ててセキュリティグループやWAF等で制御できる)で完結できる等、意外と使い所はあったりします。
ということで今回はEC2上にsvn環境を構築していきます。※構築するところまでです。

svnとはなんぞやについては公式を見ていただけるとわかりやすいかと思います。

簡潔に書くと、 民族の偉大なる太陽Apache大先生の無限にある機能の内の一つでgithubみたいなもん です。

また、今回は折角EC2で構築するのでリポジトリの場所は後付EBS配下にしてあげましょう。
こうすることで管理保守が楽になりますね。

以下の様に後付けしたEBSボリュームを/dataにmountしています。

[root@svn data]# lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  10G  0 disk
└─xvda1 202:1    0  10G  0 part /
xvdf    202:80   0  10G  0 disk /data
[root@svn data]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        482M     0  482M   0% /dev
tmpfs           492M     0  492M   0% /dev/shm
tmpfs           492M  412K  492M   1% /run
tmpfs           492M     0  492M   0% /sys/fs/cgroup
/dev/xvda1       10G  1.7G  8.3G  18% /
/dev/xvdf        10G   43M   10G   1% /data
tmpfs            99M     0   99M   0% /run/user/1000

マウント方法は割愛しますが、わからない方はこの辺を確認してみましょう。

インストール

では以下のパッケージをインストールしていきます。

httpd
subversion
mod_dav_svn

とりあえずこの3つをinstallすれば動きます。

それぞれこんなイメージでとりあえずはOKです。

httpd(言わずもがな)
subversion(本体)
mod_dav_svn(svnをブラウザからアクセスさせるのに必要なやつ)

ldap認証をしたい場合はこれも入れましょう。(今回は入れません)

mod_ldap

インストール完了したら次へ行きます。

[root@svn data]# yum list installed | grep httpd
generic-logos-httpd.noarch            18.0.0-4.amzn2                 @amzn2-core
httpd.x86_64                          2.4.51-1.amzn2                 @amzn2-core
httpd-filesystem.noarch               2.4.51-1.amzn2                 @amzn2-core
httpd-tools.x86_64                    2.4.51-1.amzn2                 @amzn2-core
[root@svn data]# yum list installed | grep subversion
subversion.x86_64                     1.7.14-16.amzn2.0.1            @amzn2-core
subversion-libs.x86_64                1.7.14-16.amzn2.0.1            @amzn2-core
[root@svn data]# yum list installed | grep mod_dav_svn
mod_dav_svn.x86_64                    1.7.14-16.amzn2.0.1            @amzn2-core
[root@svn data]#

リポジトリ作成

今回はtestという名前でリポジトリを作成します。
ディレクトリはこんな感じで作成しました。

[root@svn data]# mkdir -p /data/svn/repos/test/
[root@svn data]# cd svn/repos/test/
[root@svn test]# pwd
/data/svn/repos/test
[root@svn test]# ls -la
total 0
drwxr-xr-x 2 root root  6 Dec 16 22:04 .
drwxr-xr-x 3 root root 18 Dec 16 22:04 ..

ではこのtestというディレクトリをリポジトリとして使用していきます。
※今回は新規構築の為確認作業無しでmkdirしていますが、既存環境に手を加える場合はsvnに限らず必ず作成前にリポジトリの重複がないか確認しましょう。
作業前の確認作業は必須です。何事も作業前と後には確認作業を実施しましょう(忘れてしまわぬよう未来の私へ)。

リポジトリ作成コマンドはこちら

# svnadmin create <path>

ここからsvnadminというコマンドが出てきますので、暇を見てsvn系のコマンドリファレンスを確認しましょう。

実行するとtestディレクトリがリポジトリとして使用できるようになります。

[root@svn test]# svnadmin create /data/svn/repos/test
[root@svn test]# ls -la
total 8
drwxr-xr-x 6 root root  86 Dec 16 22:10 .
drwxr-xr-x 3 root root  18 Dec 16 22:04 ..
drwxr-xr-x 2 root root  54 Dec 16 22:10 conf
drwxr-sr-x 6 root root 233 Dec 16 22:10 db
-r--r--r-- 1 root root   2 Dec 16 22:10 format
drwxr-xr-x 2 root root 231 Dec 16 22:10 hooks
drwxr-xr-x 2 root root  41 Dec 16 22:10 locks
-rw-r--r-- 1 root root 229 Dec 16 22:10 README.txt

各設定ファイルが作成されました。
試しにREADMEを見てみましょう。

README.txt
This is a Subversion repository; use the 'svnadmin' tool to examine
it.  Do not add, delete, or modify files here unless you know how
to avoid corrupting the repository.

Visit http://subversion.apache.org/ for more information.

このディレクトリがsvnリポジトリであることやら公式サイトへのリンクが貼られていますね。

では次にtrunk, branches, tagsを作成します。
これらはそれぞれ

trunk - githubで言うところのmasterbranch相当
branches - そのままの意味です。trunkからの分岐したものをココで管理します。
tags - 特定時点のtrunkに名前をつけたものです。リビジョンナンバーだけの管理だとわかりにくいので、タグとして名前をつけておくことが出来ます。

作成コマンドにはsvn mkdirを使います。

# svn mkdir file://<parh> -m "<comment>"
[root@svn test]# svn mkdir file:///data/svn/repos/test/trunk -m "create"

Committed revision 1.
[root@svn test]# svn mkdir file:///data/svn/repos/test/tags -m "create"

Committed revision 2.
[root@svn test]# svn mkdir file:///data/svn/repos/test/branches -m "create"

Committed revision 3.

コミットバージョンが記録され始めましたね。
余談ですが、mkdirしたものを削除する場合は rm ではなく svn rm を使用します。

[root@svn test]# svn rm file:///data/svn/repos/test/hogehoge -m "delete"

Committed revision 4.

ではsvn mkdirした結果を確認しましょう。

[root@svn test]# svn ls file:///data/svn/repos/test
branches/
tags/
trunk/

問題なく作成されていますね。
ここで単にlsコマンドを実行しても確認できないので注意しましょう。

[root@svn test]# ls -la
total 8
drwxr-xr-x 6 root root  86 Dec 16 22:10 .
drwxr-xr-x 3 root root  18 Dec 16 22:04 ..
drwxr-xr-x 2 root root  54 Dec 16 22:10 conf
drwxr-sr-x 6 root root 253 Dec 16 22:42 db
-r--r--r-- 1 root root   2 Dec 16 22:10 format
drwxr-xr-x 2 root root 231 Dec 16 22:10 hooks
drwxr-xr-x 2 root root  41 Dec 16 22:10 locks
-rw-r--r-- 1 root root 229 Dec 16 22:10 README.txt

svnコマンドで作成したものはsvnコマンドで確認、ですね。
ということで作成できました。

次に、svnリポジトリとして使用するtestディレクトリをapacheユーザーに変更します。
chownを叩くだけです(結構忘れがち)。

[root@svn repos]# ls -la
total 0
drwxr-xr-x 3 root root 18 Dec 16 22:04 .
drwxr-xr-x 3 root root 19 Dec 16 22:04 ..
drwxr-xr-x 6 root root 86 Dec 16 22:10 test
[root@svn repos]# chown -R apache. /data/svn/repos/test/
[root@svn repos]# ls -la
total 0
drwxr-xr-x 3 root   root   18 Dec 16 22:04 .
drwxr-xr-x 3 root   root   19 Dec 16 22:04 ..
drwxr-xr-x 6 apache apache 86 Dec 16 22:10 test

リポジトリ作成はこれで完了です。

confの編集

では次にsvnのconfを書いていきましょう。この辺はapacheさんが読み込んでくれればどこでもOKです。
今回はconf.dの下にvhost.confとして作成していきます。また、動作確認のみでパスワードの類は設定しません。パスワードないしldap認証などは本記事では割愛します。
※まずはパスワード等無しで動作を確認し、その後に制限をかけていく手順が良いかと思います。

[root@svn conf.d]# pwd
/etc/httpd/conf.d
[root@svn conf.d]# vim vhost.conf
vhost.conf
<VirtualHost *:80>
        ServerName hogehoge.svntest.com

        Alias /svn "/data/svn/"

       <Location "/test">
            DAV svn
            SVNPath /data/svn/repos/test

            Order deny,allow
            Deny from all
            Allow from 127.0.0.1
            Satisfy Any
       </Location>

</VirtualHost>

confが書けたらチェックしてみましょう

[root@svn conf.d]# apachectl configtest
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::45a:efff:fedc:ded3%eth0. Set the 'ServerName' directive globally to suppress this message
Syntax OK

OKですね。最後にアクセス確認です。

アクセス確認

httodを起動してアクセス確認します。

[root@svn repos]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd.service(8)
[root@svn repos]# systemctl start httpd
[root@svn repos]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-12-16 10:49:33 UTC; 1s ago
     Docs: man:httpd.service(8)
 Main PID: 7751 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─7751 /usr/sbin/httpd -DFOREGROUND
           ├─7752 /usr/sbin/httpd -DFOREGROUND
           ├─7753 /usr/sbin/httpd -DFOREGROUND
           ├─7754 /usr/sbin/httpd -DFOREGROUND
           ├─7755 /usr/sbin/httpd -DFOREGROUND
           └─7756 /usr/sbin/httpd -DFOREGROUND

Dec 16 10:49:33 svn systemd[1]: Starting The Apache HTTP Server...
Dec 16 10:49:33 svn httpd[7751]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::4f2:ddff:feb2...is message
Dec 16 10:49:33 svn systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@svn repos]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@svn repos]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-12-16 10:49:33 UTC; 9s ago
     Docs: man:httpd.service(8)
 Main PID: 7751 (httpd)
   Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─7751 /usr/sbin/httpd -DFOREGROUND
           ├─7752 /usr/sbin/httpd -DFOREGROUND
           ├─7753 /usr/sbin/httpd -DFOREGROUND
           ├─7754 /usr/sbin/httpd -DFOREGROUND
           ├─7755 /usr/sbin/httpd -DFOREGROUND
           └─7756 /usr/sbin/httpd -DFOREGROUND

Dec 16 10:49:33 svn systemd[1]: Starting The Apache HTTP Server...
Dec 16 10:49:33 svn httpd[7751]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::4f2:ddff:feb2...is message
Dec 16 10:49:33 svn systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@svn repos]#

起動OKです。では試しにローカルからアクセス確認してみしましょう。

$ curl -L hogehoge.svntest.com/test
<html><head><title>test - Revision 5: /</title></head>
<body>
 <h2>test - Revision 5: /</h2>
 <ul>
  <li><a href="branches/">branches/</a></li>
  <li><a href="tags/">tags/</a></li>
  <li><a href="trunk/">trunk/</a></li>
 </ul>

アクセスOKですね。
IPアドレスで確認したりも出来ます。ココで詰まった場合はconfを再確認してみましょう。
ブラウザからだとこんな感じに確認できます。

スクリーンショット 2021-12-16 23.24.05.png

また、confでServerNameも書いたのでドメインを取得すればドメイン名でのアクセスも可能です。

おまけ

リポジトリを増やしたい場合はココまでやった内容と同じ用に作業してvhost.confのLocationを追記すればOK。
ユーザー認証をldapで行いたい場合はだいたいこんな感じでvhostを書いてあげればOK。

       <Location "/test">
            DAV svn
            SVNPath /data/svn/repos/test

            Order deny,allow
            Deny from all
            AuthType Basic
            AuthBasicProvider ldap
            AuthLDAPURL "ldap://<ldapサーバー>/ou=huga,dc=piyo,dc=hogera"
            AuthName "Please Enter Your Password"
            AuthLDAPGroupAttribute memberUid
            AuthLDAPGroupAttributeIsDN off
            Require ldap-group cn=hoge,ou=huga,dc=piyo,dc=hogera
            Require ldap-group cn=hoge,ou=huga,dc=piyo,dc=hogera
            Allow from 127.0.0.1
            Satisfy Any
       </Location>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1