2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gitolite3とgitwebの導入@Ubuntu

Last updated at Posted at 2016-07-04

gitolite3GitWebを使用して,プライベートなレポジトリを構築する方法を紹介します.gitolite3を使用することで,git-daemonを使用するよりもはるかに簡単にアクセス制限を実施することが可能です.
特徴は下記のとおりです.なお,想定環境はUbuntu 16.04 LTS Xenial Xerusです.

  • git clone git@github.com:kyokuheki/***.gitみたいな形でcloneできます.githubと同じ形式です.
  • 22/tcphttp/80を使用し,git-daemonで使用される9418/tcpは使用しません.
  • アクセス制限は,テキストベースで記述できます.
  • アクセス制限のコンフィグファイルはgitで管理されます.
  • ユーザは公開鍵によって管理されます.
  • gitolite3だけの導入であれば,Apacheは使用しません.

gitolite3のインストール

管理者用の鍵の作成

gitolite3の管理者となるユーザがgitolite用の鍵を作成し,作成した鍵の公開鍵をサーバの作業ユーザのディレクトリにコピーしておく.
下記は管理者端末から実施する.

gitolite3
$ ssh-keygen
$ ssh-copy-id ubuntu@<gitサーバ> 
$ ssh ubuntu@<gitサーバ>
$ ssh ubuntu@<gitサーバ> "ls .ssh"
authorized_keys
$ ssh ubuntu@<gitサーバ> "cat .ssh/authorized_keys"
ecdsa-sha2-nistp521 *****************************== <####>@<####>

パッケージのインストール

次に,gitサーバにログインして下記コマンドを実行.
インストール時にgitolite3の管理者の公開鍵を聞かれるので,/home/<user>/.ssh/authorized_keysと入力する.gitolite3ユーザが作成される.

$ sudo apt install git gitolite3
$ sudo grep gitolite3 /etc/passwd
gitolite3:x:104:112:git repository hosting,,,:/var/lib/gitolite3:/bin/bash
$ sudo ls -a /var/lib/gitolite3
.   .bash_history  .gitolite     .ssh  projects.list
..  .cache         .gitolite.rc  logs  repositories

.gitolite.rcに設定を追加

下記の操作でgitoliteからgitwebのdescriptionを設定できるようになる.

sudo vim /var/lib/gitolite3/.gitolite.rc
/var/lib/gitolite3/.gitolite.rc
    GIT_CONFIG_KEYS                 =>  'gitweb\.(owner|description|category)',

アクセス権・レポジトリの設定・追加

管理者端末から下記コマンドを実行して,管理者端末にgitolite管理用のデータをクローンする.

git clone gitolite3@<gitサーバ>:gitolite-admin.git

ユーザの追加

公開鍵を追加すれば,ユーザが追加される.ユーザ名は公開鍵のファイル名.

cp ~/.ssh/id_rsa.pub ./gitolite-admin/keydir/<username>.pub

レポジトリのアクセス権の設定

conf/gitolite.confrepoを追加してpushすると,レポジトリが追加される.

vim ./gitolite-admin/conf/gitolite.conf
git commit -m "edit config"
git push
./gitolite-admin/conf/gitolite.conf
@developers = admin user1 user2 user3
@admin      = admin user1

repo gitolite-admin
    RW+     =   admin @admin
    R       =   @developers

repo testing
    RW+     =   @developers
    R       =   gitweb @all

repo sandbox
    desc    =   "git sandbox"
    RW+     =   @developers
    R       =   gitweb @all

repo soft-patch-panel
    R       =   gitweb @all

動作確認

$ ssh gitolite3@<gitサーバ> info
hello admin, this is gitolite3@gitserver running gitolite3 3.6.4-1 (Debian) on git 2.7.4

 R W    gitolite-admin
 R W    sandbox
 R W    testing
$ ssh gitolite3@<gitサーバ> help
hello okuda, this is gitolite3@gitserver running gitolite3 3.6.4-1 (Debian) on git 2.7.4

list of remote commands available:

        desc
        help
        info
        perms
        writable

参考

gitweb

gitサーバ上で下記コマンドを実行する.

sudo apt install gitweb libcgi-pm-perl
sudo a2enconf gitweb
sudo a2enmod cgid
sudo service apache2 restart

以上で完了.

Ubuntu 14.04の場合

ちなみにUbuntu 14.04の場合はもう少し作業が必要.gitwebによって追加されるApache2の設定ファイルはパスや内容が間違っているので修正する.

sudo apt install gitweb
sudo mv /etc/apache2/conf.d/gitweb /etc/apache2/sites-available/gitweb.conf
sudo vim /etc/apache2/sites-available/gitweb.conf
/etc/apache2/sites-available/gitweb.conf
Alias /gitweb /usr/share/gitweb

<Directory /usr/share/gitweb>
  Options +FollowSymLinks +ExecCGI
  AddHandler cgi-script .cgi
</Directory>

gitwebを有効化して,apache2を再起動する.

sudo a2enmod cgid
sudo a2ensite gitweb.conf
sudo service apache2 restart

gitolite3とgitwebの連携

次にgitolite3にgitwebからアクセスできるように設定を追加する.gitwebはapache2のプロセス配下で動作するので,ユーザidはwww-dataとなる.www-dataが属するグループからアクセスできるようにしておけば問題なく動作する.

UMASKの設定

gitolite3によるファイル操作時のUMASKを設定し,グループのパーミッションが追加されるようにする.

sudo vim /var/lib/gitolite3/.gitolite.rc
/var/lib/gitolite3/.gitolite.rc
    UMASK                           =>  0027,

既存ファイルのアクセス権を修正

sudo gpasswd -a www-data gitolite3
sudo chmod g+rx /var/lib/gitolite3
sudo chmod g+r /var/lib/gitolite3/projects.list
sudo chmod -R g+r /var/lib/gitolite3/repositories
sudo find /var/lib/gitolite3/repositories -type d -exec chmod g+x {} +

gitweb.confの修正

gitwebの設定を修正し,gitolite3を参照するように変更する.

vim /etc/gitweb.conf
/etc/gitweb.conf
# cat /etc/gitweb.conf
# path to git projects (<project>.git)
$projectroot = "/var/lib/gitolite3/repositories";

# directory to use for temp files
$git_temp = "/tmp";

# target of the home link on top of all pages
# $home_link = $my_uri || "/";

# html text to include at home page
# $home_text = "indextext.html";

# file with project list; by default, simply scan the projectroot dir.
$projects_list = "/var/lib/gitolite3/projects.list";
$strict_export = "true";

# stylesheet to use
@stylesheets = ("static/gitweb.css");

# javascript code for gitweb
$javascript = "static/gitweb.js";

# logo to use
$logo = "static/git-logo.png";

# the 'favicon'
$favicon = "static/git-favicon.png";

# git-diff-tree(1) options to use for generated patches
# @diff_opts = ("-M");
@diff_opts = ();

参考

リモートレポジトリのミラー

githubにある公開レポジトリをリモートレポジトリにミラーしていつでもアクセスできるようにする.

sudo su gitolite3
cd ~/repositories
git clone --mirror https://github.com/garogers01/soft-patch-panel.git
cd soft-patch-panel.git
git fetch --all
exit
sudo service apache2 restart

Smart HTTPでHTTPでCloneできるようにする

apache2が既に入っている環境であれば,ちょっとの設定追記でSmart HTTPを有効化できます.

vim /etc/apache2/sites-available/git.conf
a2ensite git.conf
apache2ctl configtest
service apache2 restart
/etc/apache2/sites-available/git.conf
SetEnv GIT_PROJECT_ROOT /var/lib/gitolite3/repositories
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git /usr/lib/git-core/git-http-backend
        
<Location /git>
        Options +ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch
        AuthType Basic
        AuthName "Smart HTTP Git Access"
        AuthUserFile "/var/www/trac/.htpasswd"
        Require valid-user
        Order allow,deny
        Allow from all
</Location>

ErrorLog ${APACHE_LOG_DIR}/error_git.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
# LogLevel debug
        
CustomLog ${APACHE_LOG_DIR}/access_git.log combined

参考

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?