LoginSignup
3
7

More than 5 years have passed since last update.

VPSに個人用のGitサーバを作る

Posted at

目的

色々作るにあたって、ソースコードを公開できる場合とできない場合がある。
企業レベルでなら、ちゃんと契約してprivateで作業できるようにしてもいいと思うが、個人レベルでやってる場合には、そうはいかない。
そこで自分でGitサーバを構築することにした。
ここで問題が発生する。それは、そのサーバをどのように調達するかだ。
しかし、今回は作るものがWebサーバということで、元々VPSを借りる予定だったため問題とはならなかった。
その他の対応としては、ローカルLAN内にサーバを立てるなどがあるだろう。
現状予定では使用するのが私一人なので、GUIは作らず、CLIで管理できれば良いとする。

環境

  • サーバ
    VPSサーバ
    CentOS release 6.5 (Final)

  • クライアント
    Windows 10(Fall Creators Update済み)
    Subsystem for Linux(Ubuntu)

構築する

Git用ユーザの作成

gitユーザを作成。

[root@Server:~]# adduser git
[root@Server:~]# passwd git

SSHの設定

デフォルトで接続できる設定になっていた。

公開鍵認証でログイン

[root@Server:~]# su git
[git@Server:~]# cd
[git@Server:~]# mkdir .ssh
[git@Server:~]# chmod 700 .ssh/

[Client:~]# ssh-keygen -t rsa
[Client:~]# scp ~/.ssh/id_rsa.pub git@{ServerIP}:~/.ssh/authorized_keys

[git@Server:~]# chmod 600 .ssh/authorized_keys

[Client:~]# ssh git@{ServerIP} -i ~/.ssh/id_rsa

.ssh/configに登録

.ssh/configファイルでSSH接続を管理するを参照。

Gitのインストール

[root@Server:~]# yum install git

これでGitのインストール完了!
でも、バージョンが古すぎる...

バージョンアップ

参考:CentOSに最新版のGitをインストール・アップデートする方法 - TASK NOTES
2018年9月23日現在 最新版v2.19.0

[root@Server:~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker
[root@Server:~]# mkdir /usr/local/src/
[root@Server:~]# cd /usr/local/src/
[root@Server:src]# git clone https://github.com/git/git
[root@Server:src]# yum erase git
[root@Server:src]# cd git
[root@Server:git]# git checkout refs/tags/v2.19.0
[root@Server:git]# make prefix=/usr/local all
[root@Server:git]# make prefix=/usr/local install

これでSSHを再接続すれば、Gitは最新版!

使ってみる

クローン

[git@Server:~]# mkdir sample.git
[git@Server:~]# git init sample.git --bare --shared

[Client:~]# git clone git@{ServerIP}:sample.git

Gitクローンできた!

コミット&プッシュ

[Client:~]# cd sample
[Client:~]# echo "サンプルだよー" > sample.txt
[Client:~]# git add .
[Client:~]# git commit -m "first commit"
[Client:~]# git push

[git@Server:~]# cd sample.git
[git@Server:sample.git]# git log --oneline

ちゃんとプッシュできてるわー

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