qshinoの日記

Powershell関係と徒然なこと

git http server setup

git httpサーバ設定手順

  1. sv: リポジトリ用意
  2. sv: apache2
  3. sv: リポジトリ更新
  4. cl: クライアント設定

1. リポジトリ準備

$ sudo apt-get install git

次に「/home/user/git/project.git/」にリモートリポジトリを作成します。

$ mkdir git
$ cd git
$ git init --bare --shared project.git

作成したリポジトリ全体について、Apacheユーザのアクセス権を与えます。Apacheユーザはデフォルトでは「www-data」です。

$ sudo chown -R www-data:www-data project.git

リポジトリの準備は以上です。

2. Apache設定

リモートリポジトリにはsvn、http、httpsプロトコルを使用した接続がありますが、今回はローカルからVirtual Hostを使ってhttpでアクセスすることにします。

まずは、Virtual Hostの設定ファイルを「/etc/apache2/sites-available/001-git.conf」を用意し、以下の内容を記述します。ファイル名「001-git.conf」は任意です。

<VirtualHost *:80>
    ServerName git.localhost
    DocumentRoot /home/user/git/

    ServerAdmin webmaster@localhost

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SetEnv GIT_PROJECT_ROOT /home/user/git/
    SetEnv GIT_HTTP_EXPORT_ALL
    ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
    
    <Location />
        Order allow,deny
        Allow from all
        AuthType Basic
        AuthName "git repository"
        AuthUserFile /etc/apache2/git.passwd
    AuthBasicProvider external
    AuthExternal pwauth

        Require valid-user
    </Location>
</VirtualHost>

ポイントとしてはVirtual Hostを有効にするためにServerName、リポジトリを作成したディレクトリにDocumentRootを設定します。 gitコマンドを利用するためにDocumentRootと同じディレクトリにGIT_PROJECT_ROOT、また、GIT_HTTP_EXPORT_ALLとScriptAliasを設定します。 アクセス制限を設けるため、apache2でパスワード管理ファイル「/etc/apache2/git.passwd」を別途用意します。

書き終えたら、設定ファイルを有効にして、apacheを再起動します。

$ sudo a2ensite 001-git.conf
$ sudo service apache2 restart

HTTPでリモートリポジトリからcloneしてみる

それでは実際にリモートリポジトリをcloneしてみます。 今回はローカル環境でVirtual Hostを利用するので、ファイル「/etc/hosts」に先ほどServerNameに設定したサーバ名を記述します。

127.0.0.1 git.localhost

$ mkdir myproject
$ cd myproject
$ git clone http://git.localhost/project.git .
Cloning into '.'...
Username for 'http://git.localhost': user
Password for 'http://user@git.localhost':
Checking connectivity... done.

apache2で設定したユーザ名とパスワードを入力して、.gitが生成されれば一通り設定成功です。 commit、push、pullコマンドも試してみましょう。

リモート接続できない場合

私のケースですが、初めてリモートからcloneする時に、以下のエラーが発生しました。

fatal: repository 'http://git.localhost/' not found

clone、pull、push する際に、リポジトリフォルダ内で以下のコマンドを実行する必要があるようです。

$ cd /home/user/git/project.git/
$ git update-server-info
Related posts:

ref

https://helog.jp/git/http/

centos7の場合

repository: /opt/git/wk/tools.git

http://localhost/wk/tools.git

###
gitdir=/opt/git
wkdir=$gitdir/wk/tools.git
user=tanaka
###

sudo apt install httpd httpd-utils git
# sudo a2enmod cgi

sudo mkdir -p ${wkdir}
cd $wkdir
sudo git init --bare --shared
sudo chown -R apache:apache $gitdir

echo "pw for $user"
sudo htpasswd -c /etc/httpd/conf.d/git.passwd $user

git.conf

ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
SetEnv GIT_PROJECT_ROOT /opt/git
SetEnv GIT_HTTP_EXPORT_ALL

<LocationMatch "^/git">
    AuthType Basic
    AuthName "git repository"
    Require valid-user
    AuthUserFile /etc/httpd/conf.d/git.passwd
</LocationMatch>

sudo systemctl restart httpd

ref

git http backend

https://git-scm.com/docs/git-http-backend

mod_rewrite

https://www.google.com/amp/s/oxynotes.com/%3fp=7392&