自己租的 VPS 經歷上次的更新 MySQL 5.7 版本之後就爛掉,資料庫變成只能讀不能儲存…..
一氣之下,就把上面的專案都把它放到其他的主機裡,這台就重新的 reset 掉。
這台目前打算就拿來架設:Git server 與 CI server 作為日後有private Git 專案使用,本來打算是是架設 GitLab 作為 Git server,可是現在目前的VPS規格不夠好。
據說 GitLab 要跑得順的話,需要將近 8G RAM 才有辦法…..於是目前就先放棄了….先打算架設版本控制跟可以做持續整合的服務的 server 為主。
在經歷了一番GitLab server 安裝不起來之後,現在是轉向這篇要講的東西了,這個方式的 private Git server 算是比較輕量型的方案,也是比較合理的。
所以以下就是安裝 Gitolite + GitWeb 的方法。
[參考的資料]
how-to-create-own-git-server-with-gitolite-and-gitweb-in-ubuntu
- 首先,先 setup 一個乾淨 VPS 並裝好Ubuntu 14.04 或是 16.04 的 LTS 版本,沒有的話就自己去租一臺吧,DigitalOcean 都有很便宜的價格有可以加上 Github 的學生方案可以使用,可以拿到 50 美金作運用。不過,不用的話要記得關掉,不然可是會一直的扣錢喔!
- 接著跑下面的 bash script: initial_ubuntu.sh,使用方式是:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
# initial_ubuntu.sh
#!/bin/bash
#Firstly, we have to login the root user via ssh.
# install some required package
# set locales (zh_TW.UTF-8 or en_US.UTF-8)
sudo
locale-gen
"en_US.UTF-8"
sudo
dpkg-reconfigure locales
sudo
echo
'LC_ALL="en_US.UTF-8"'
>
/etc/default/locale
export
USERNAME=$1
if
[
"$USERNAME"
=
""
]
then
echo
'please add the user name!'
exit
1;
fi
sudo
apt-get update
echo
'Upgrading the package...It will be let user type the yes | no'
echo
'We have to notice that this upgrade package will be installed the Apache2 HTTP server...'
# skip the kernel update (OpenVZ is not allowed updating the Kernel.)
sudo
apt-mark hold linux-image-generic linux-headers-generic
sudo
apt-get upgrade
sudo
apt-get
install
-y curl wget vim ufw
sudo
useradd
-m $USERNAME
sudo
usermod
-s
/bin/bash
$USERNAME
sudo
adduser $USERNAME
sudo
echo
'Please set password for the $USERNAME ...'
sudo
passwd
$USERNAME
sudo
ufw
enable
sudo
ufw default deny
sudo
ufw allow
in
ssh
sed
-i
's/PermitRootLogin yes/PermitRootLogin no/g'
/etc/ssh/sshd_config
sudo
echo
'ClientAliveInterval 60'
>>
/etc/ssh/sshd_config
sudo
service
ssh
restart
echo
'done.'
exit
0;
1bash
initial_ubuntu.sh your-usernmae
可以安裝一些基本或是需要的套件在以及設定基本的軟體防火牆還有 SSH 的簡易安全防禦設定。
這裡的 your-username 在範例中,我們先填 peter 做為這一次範例的使用者名稱。 - 接下來就是要安裝 Git server 了,首先先依照步驟慢慢來。
- 首先,先更新一下軟體來源庫的網址。
1
sudo
apt-get update
接著,在安裝 Git 套件:git-core,接著新增一個使用者
12345678910111213sudo
apt-get
install
git-core -y
#新增一個使用者
sudo
adduser \
--system \
--shell
/bin/bash
\
--gecos
'git version control'
\
--group \
--disabled-password \
--home
/home/git
git
#切換使用者:git
sudo
su
-l git
- 切換到 git 使用者的家目錄下面並 clone Gitolite 的 repository.
在家目錄下建立 bin 目錄。123mkdir
$HOME
/bin
cd
/home/git/bin
git clone git:
//github
.com
/sitaramc/gitolite
- 驗證使用者為 git 相關的設定 (user verification)
12345678910
# Verification 登入使用者 git
# ------------------------------
# git@ubuntu:~$ ls bin/gitolite/src/gitolite
# git@ubuntu:~$
# ------------------------------
# ssh key generation and we have to type one and more enter key to check some interactive message.
# In your Linux client and run your-name@your-ip-address-or-hostname:~$ ssh-keygen -t rsa -C "Git-Admin"
# If you have already had the id_rsa.pub, we can skip previous step and run following command directly: (我們使用這一項)
# scp ~/.ssh/id_rsa.pub username@git-server-FQDN-or-its-ip-address:~ then go back to the git-server
- 我們使用的是將本地端的 id_rsa.pub 上傳到 Git server 上
要注意的是,我們在新建使用者 Git 的時候,我們並沒有為使用者: git 設定一組密碼,因此使用 scp 傳輸 public key 時,我們需要先傳給另一個使用者。
並登入之後把檔案搬移到 git 使用者的家目錄下面。1scp
~/.
ssh
/id_rsa
.pub username@git-server-FQDN-or-its-ip-address:~
- 接著,執行下面的指令:
123456
sudo
mv
id_rsa.pub
/home/git/Git-Admin
.pub
sudo
chown
git:git
/home/git/Git-Admin
.pub
sudo
su
-l git
bin
/gitolite/src/gitolite
setup -pk Git-Admin.pub
# 離開使用者 git (logout)
exit
- 安裝 GitWeb 與 Apache2 以及修改權限
123
sudo
apt-get
install
gitweb apache2
sudo
usermod
-G git www-data
sudo
chmod
-R 750
/home/git/repositories/
- 修改 gitolite.rc 設定檔並找到下面這一行改成:
12
# sudo vim /home/git/.gitolite.rc
UMASK value as 0027
- 修改 /etc/gitweb.conf 的檔案
sudo vim /etc/gitweb.conf
並修改下面這幾行:12$projectroot =
"/home/git/repositories/"
;
$projects_list = $projectroot;
- 重新啟動 Apache HTTP server
1
sudo
service apache2 restart
- 有啟動防火牆設定的話,記得將 ufw 允許 http 可以有外部連線。
1
sudo
ufw allow http
- 複製 gitweb.conf 設定檔
123
sudo
cp
/etc/apache2/conf
.d
/gitweb
/etc/apache2/conf-available/gitweb
.conf
cd
/etc/apache2/conf-enabled
sudo
ln
-s ..
/conf-available/gitweb
.conf
- 修改位在:/etc/apache2/conf-available/gitweb.conf 的設定檔
12345678910111213
<Directory
/usr/share/gitweb
>
# 修改這一行
Options +FollowSymLinks +ExecCGI
AddHandler cgi-script .cgi
# 如果有 HTTPS 連線的需要,則需要使用以下的設定
# 從下面開始,是要啟動 rewrite module 之外
# 設定強制使用 HTTPS 進行連線
# 可以進行 HTTPS 連線有:Cloudflare and Let's encrypt
AllowOverride All
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https:
//
%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<
/Directory
>
123# 接著重啟 Apcahe2 服務與啟用 cgi module
sudo
a2enmod cgi
sudo
service apache2 restart
- 最後,輸入網址:http://you-ip-address-or-hostname/gitweb 就可以看到 GitWeb 的清單了。
- 下面是要新建一個 repository 的方法
1
git clone git@your-ip-address:gitolite-admin.git
- 接著切換到:gitolite/conf/gitolite.conf 編輯:
12345
RW+ = Git-Admin
repo testing
RW+ = @all
像這樣就是新增一個名字叫做 testing 的 repository。如果要新增其他的,就依照新增 testing 的格式,再增加一個 repository.
接著輸入: git commit -m “your commit message” && git push origin master這樣就完成新增一個 repository 了。
[延伸閱讀]
1. 為 VPS 增加 SSL 憑證:以 Let’s encrypt 為例
2. 如何使用 Cloudflare 作為 DNS 代管
3. 為你的 GitWeb 增加 HTTP-basic-Authentication
4. 利用 reverse proxy server 把 git server 藏在其後面,增加安全性,以 Nginx 為例