LoginSignup
16
29

More than 5 years have passed since last update.

httpsでgitサーバを立てて複数人でいじれるとこまで行いました

Last updated at Posted at 2017-03-14

Gitを導入してみました。かなりの数の人が使っているので、簡単なのかなと思っていたら意外とハマってしまいました。。
自分なりに導入した手順をメモします。
プロトコルはhttpsでCentOSにリポジトリを入れてgitサーバを立ち上げます。

gitサーバの構築

まずはgitサーバへのgitインストール
これは簡単でした。

yum install git

httpd.confの設定
/etc/httpd/conf/httpd.confに以下のようなAliasを作ってLocationを以下のように設定

httpd.conf
Alias /git /var/lib/git
<Location /git>
   DAV on
   SSLRequireSSL
   AuthType Basic
   AuthName "Git Repository"
   AuthUserFile /var/lib/git/htpasswd
   Require valid-user
</Location>

/gitはhttpsからアクセスされるurlの一部です。例)https://www.xxx.jp/git/
/var/lib/gitはgitのリポジトリを格納するフォルダになります。
/var/lib/git/htpasswdはhttpのBasic認証で使用するファイルです。htpasswdコマンドでIDとパスワードを設定してください

リポジトリの作成
gitのリポジトリを登録。名前はとりあえずhogeと

$ git init --bare hoge
$ chown -R apache:apache hoge

ここまではなんだ全然、余裕じゃんと思っていたのですが、こっからハマりました。

まずはcloneしようとして、

~ git clone https://www.xxx.jp/git/hoge
fatal: unable to access 'https://www.xxx.jp/git/hoge.git/': SSL certificate problem: unable to get local issuer certificate

まあ、こうなりますよね。オレオレ証明書じゃダメだと。
とりあえず、本番用の証明書は置いておいて
.gitconfigに

[http]
    sslVerify = false

を追記。これでとりあえず先に進めるな。と思ったはいいが、別のエラーが

git clone https://www.xxx.jp/git/hoge
error: The requested URL returned error: 401 Authorization Required while accessing ...

となりました。はい。Basic認証しているので認証をしなければなりません。
ここでちょっとgitのバージョンによって動作が変わります。
ver 1.7.1 だとurlをgit clone --vare https://[user名]@www.xxx.jp/git/hogeとしないと動きません。
cygwin上のgit(version 2.8.3)では、対話形式でUsernameとPasswordで聞いてきますが1.7.1の場合明示的にurlに埋め込む必要があります。
でも、こういうところで毎回パスワードを入力するのは面倒なので

git clone https://[user名]:[pass]@www.xxx.jp/git/hoge

とついでにパスワードも設定しておきます。
気を取り直してもう一度clone

~ $ git clone https://www.xxx.jp/git/hoge
fatal: https://[user名]:[パスワード]@www.highwide.jp/git/bb/info/refs not found: did you run git update-server-info on the server?

は?
cloneが失敗してしまいました。
git update-server-info
実行しろよみたいなエラーですね
なのでgit update-server-infoを実行します

~$ git update-server-info

とコマンドを打たなきゃダメらしい。しかも毎回。
毎回は面倒なので

mv hoge/hooks/post-update.sample hoge/hooks/post-update

としておきます。さて準備が整ったのでclone

~ $ git clone https://www.xxx.jp/git/hoge
Cloning into 'hoge'...
Username for 'https://www.xxx.jp':
Password for 'https://o-ta@www.xxx.jp':
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

よし 成功!
では軽く某かのファイルをcommitしてpushしてみるかと

~/hoge $ echo a > a
~/hoge $ git add a
~/hoge $ git commit -m 'init'

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address

おっとそういえばidとmailを設定してなかったなと

$ git config --global user.email "YamadaKun@gmail.com"
$ git config --global user.name "YamadaKun"

よし、今度こそ

ダメでした

~/hoge $ git commit -m 'init'
[master (root-commit) 38090ae] init
 1 file changed, 1 insertion(+)
 create mode 100644 a
~/hoge $ git push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
Everything up-to-date

commitまでは正常に動いており、pushで躓いているみたいです。
調べた結果
何もない、まっさらなリポジトリでは

git push origin master

を実行していないとダメだとのことです。

これでようやくpushすることができました

$ git push origin master
Fetching remote heads...
  refs/
  refs/heads/
  refs/tags/
updating 'refs/heads/master'
  from 0000000000000000000000000000000000000000
  to   04829bac3e1ad317e99c619b73f198434b54dfea
    sending 3 objects
    done
Updating remote server info
To https://www.xxx.jp/git/bb
 * [new branch]      master -> master
16
29
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
16
29