搭建git服务器

简介:

我们都知道GitHub只有公开库是免费的,而私有仓库是需要花钱买的。所以我们可以想办法自己搭建一个私有的,仅自己公司使用的。Gitlab是个不错的选择。在介绍它之前,先讲述一下如何搭建命令行的git服务器。

我这里准备了两台机器做这个实验,一台作为服务器,一台作为客户端:

  • 服务器IP:192.168.77.134
  • 客户端IP:192.168.77.130

首先在服务器上安装git,命令如下:

yum -y install git

添加git用户,并且设置shell为/usr/bin/git-shell,目的是为了不让git用户远程登陆,并且在该用户的家目录下创建authorized_keys文件,并更改属主、属组和权限,用来存客户端机器上的公钥:

[root@localhost ~]# useradd -s /usr/bin/git-shell git
[root@localhost ~]# cd /home/git/
[root@localhost /home/git]# mkdir .ssh/
[root@localhost /home/git]# touch .ssh/authorized_keys
[root@localhost /home/git]# chmod 600 .ssh/authorized_keys
[root@localhost /home/git]# chown -R git:git .ssh
[root@localhost /home/git]# passwd git  # 设置一下git用户的密码
更改用户 git 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost /home/git]# 

然后把客户端上的公钥复制到服务器的authorized_keys文件中,如果没有密钥则使用ssh-keygen命令生成,过程我就不演示了。

接着到客户端上使用ssh连接git用户,输出结果如下代表没问题,因为我们设置了不让git用户远程登陆:

[root@localhost ~]# ssh git@192.168.77.134
Enter passphrase for key '/root/.ssh/id_rsa':  # 客户端上的私钥文件所在路径吗,默认是/root/.ssh/id_rsa
git@192.168.77.134's password:   # 服务端上的git用户的密码
Last failed login: Tue Jan 16 22:30:40 CST 2018 from 192.168.77.130 on ssh:notty
There were 7 failed login attempts since the last successful login.
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.77.134 closed.
[root@localhost ~]#

完成以上操作后,在服务端创建git仓库的目录:

[root@localhost ~]# mkdir /data/gitroot
[root@localhost ~]# cd /data/gitroot
[root@localhost /data/gitroot]#

在该目录下创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾:

[root@localhost /data/gitroot]# git init --bare sample.git
初始化空的 Git 版本库于 /data/gitroot/sample.git/
[root@localhost /data/gitroot]# ls
sample.git
[root@localhost /data/gitroot]# chown -R git.git sample.git
[root@localhost /data/gitroot]# 

注意:以上是在git服务器上操作的,平时git服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像github一样,平时的add、commit等命令都是在我们自己的pc上操作。

在客户端上克隆远程仓库:

[root@localhost ~]# git clone git@192.168.77.134:/data/gitroot/sample.git
正克隆到 'sample'...
Enter passphrase for key '/root/.ssh/id_rsa': 
git@192.168.77.134's password: 
warning: 您似乎克隆了一个空版本库。
[root@localhost ~]#

此时当前目录下就会生成一个sample的目录,这个就是我们克隆的远程仓库了。进入到这里面,可以开发一些代码,然后push到远程:

[root@localhost ~]# cd sample/
[root@localhost ~/sample]# ll -a
总用量 4
drwxr-xr-x   3 root root   17 1月  16 22:55 .
dr-xr-x---. 18 root root 4096 1月  16 22:55 ..
drwxr-xr-x   7 root root  111 1月  16 22:55 .git
[root@localhost ~/sample]# echo "This is test data" > test.txt
[root@localhost ~/sample]# git add test.txt
[root@localhost ~/sample]# git commit -m "add test.txt"
[master(根提交) e6e3dad] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@localhost ~/sample]# git push origin master  # 因为是裸仓库,所以需要指定分支进行提交
Enter passphrase for key '/root/.ssh/id_rsa': 
git@192.168.77.134's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 220 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.77.134:/data/gitroot/sample.git
 * [new branch]      master -> master
[root@localhost ~/sample]#

然后到另一个目录下进行克隆,看看是否能从服务端上克隆刚刚提交上去的文件:

[root@localhost /tmp]# git clone git@192.168.77.134:/data/gitroot/sample.git
正克隆到 'sample'...
Enter passphrase for key '/root/.ssh/id_rsa': 
git@192.168.77.134's password: 
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收对象中: 100% (3/3), done.
[root@localhost /tmp]# cd sample/
[root@localhost /tmp/sample]# ll -a
总用量 8
drwxr-xr-x   3 root root   32 1月  16 23:03 .
drwxrwxrwt. 11 root root 4096 1月  16 23:03 ..
drwxr-xr-x   8 root root  152 1月  16 23:03 .git
-rw-r--r--   1 root root   18 1月  16 23:03 test.txt
[root@localhost /tmp/sample]# cat test.txt 
This is test data
[root@localhost /tmp/sample]#

如上,可以成功克隆则代表该git服务器已经能够正常提供服务了。


22.14/22.15 安装gitlab

gitlab官网:

https://about.gitlab.com/gitlab-com/

官方的安装文档:

https://about.gitlab.com/installation/?version=ce#centos-7

注:官方说安装gitlab要求服务器内存最好不少于4g ( 我之前试了一下使用2g的机器去搭gitlab,卡顿挺明显的,所以最好还是使用4个g的内存 ) ,gitlab的社区版是免费的,企业版则是收费的。

由于官方提供的yum源是国外的,而且包的大小有几百M,所以下载会很慢,我这里使用的是国内的镜像源进行安装,编辑文件如下:

[root@localhost ~]# vim /etc/yum.repos.d/gitlab.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

然后就可以使用yum安装了:

[root@localhost ~]# yum install -y gitlab-ce

注:如果使用这种方式安装不成功的话,可以尝试其他的镜像源,还是不行的话,就按照官方文档进行安装,虽然会慢一点。

安装完后使用以下命令进行配置,这个过程可能需要花几分钟:

[root@localhost ~]# gitlab-ctl reconfigure
# 成功后末尾会输出如下信息
Running handlers:
Running handlers complete
Chef Client finished, 384/544 resources updated in 01 minutes 37 seconds
gitlab Reconfigured!
[root@localhost ~]# echo $?
0
[root@localhost ~]#

检查进程和端口:

[root@localhost ~]# ps aux |grep git  # 会输出很多信息
[root@localhost ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9100          0.0.0.0:*               LISTEN      6769/node_exporter  
tcp        0      0 127.0.0.1:9229          0.0.0.0:*               LISTEN      6730/gitlab-workhor 
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      6917/unicorn master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6761/nginx: master  
tcp        0      0 127.0.0.1:9168          0.0.0.0:*               LISTEN      6726/ruby           
tcp        0      0 127.0.0.1:8082          0.0.0.0:*               LISTEN      6812/sidekiq 5.0.4  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1168/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2320/master         
tcp        0      0 0.0.0.0:8060            0.0.0.0:*               LISTEN      6761/nginx: master  
tcp        0      0 127.0.0.1:9121          0.0.0.0:*               LISTEN      6806/redis_exporter 
tcp        0      0 127.0.0.1:9090          0.0.0.0:*               LISTEN      6789/prometheus     
tcp        0      0 127.0.0.1:9187          0.0.0.0:*               LISTEN      6775/postgres_expor 
tcp6       0      0 ::1:9168                :::*                    LISTEN      6726/ruby           
tcp6       0      0 :::22                   :::*                    LISTEN      1168/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2320/master         
[root@localhost ~]#

gitlab-ctl命令可以用于重启、启动、停止gitlab服务以及查看服务状态:

gitlab-ctl {stop | restart | star | status}

现在就可以在浏览器上访问gitlab了,如下可以看到第一步就是设置密码,设置的是root用户的密码:
搭建git服务器

设置完之后就可以使用root账户登录了:
搭建git服务器

登录后的界面:
搭建git服务器


22.16 简单使用gitlab

先来看看如何配置gitlab内置的nginx服务器,配置文件所在的路径如下:

[root@localhost ~]# ls /var/opt/gitlab/nginx/conf/
gitlab-http.conf  nginx.conf  nginx-status.conf
[root@localhost ~]#

域名和监听端口在gitlab-http.conf文件中配置,如果机器上只跑一个gitlab服务就不用配置保持默认即可。

在gitlab上新建一个用户组:
搭建git服务器
搭建git服务器

然后在当前这个组里新建一个项目:
搭建git服务器
搭建git服务器

点击右上角头像,在settings界面里,添加密钥认证:
搭建git服务器
搭建git服务器

gitlap的用户可以自己注册,也可以由管理员进行创建,以下演示由管理员来创建用户:
搭建git服务器
搭建git服务器
剩下的默认即可,然后点击页面下方的Create user按钮。

创建完成后,还可以点击Edit按钮编辑该用户,例如修改个密码什么的:
搭建git服务器
搭建git服务器

保存修改后,退出当前用户,看看是否可以登录新建的用户:
搭建git服务器
搭建git服务器

然后再重新登录即可,我这里是登录成功的:
搭建git服务器

点击Create a project可以创建一个新项目:
搭建git服务器

创建成功:
搭建git服务器

剩下的操作就和GitHub很像了,而且平时我们在这个服务端上也都只是创建或编辑组和用户,大部分的操作都是在pc完成的。

gitlab常用命令:

https://www.cnyunwei.cc/archives/1204


22.17 gitlab备份和恢复

gitlab自带了一个工具用于备份和恢复,命令如下:

[root@localhost ~]# gitlab-rake gitlab:backup:create
Dumping database ... 
Dumping PostgreSQL database gitlabhq_production ... [DONE]
done
Dumping repositories ...
 * example/exampleProject ... [SKIPPED]
 * example/exampleProject.wiki ...  [SKIPPED]
 * example-test/study ... [SKIPPED]
 * example-test/study.wiki ...  [SKIPPED]
done
Dumping uploads ... 
done
Dumping builds ... 
done
Dumping artifacts ... 
done
Dumping pages ... 
done
Dumping lfs objects ... 
done
Dumping container registry images ... 
[DISABLED]
Creating backup archive: 1516123939_2018_01_17_10.3.3_gitlab_backup.tar ... done  # 备份文件名
Uploading backup archive to remote storage  ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... skipping
[root@localhost ~]# 

如果数据量大的话,需要蛮久的。这个操作会备份:组、用户、项目以及仓库文件等。

备份目录在/var/opt/gitlab/backups目录下:

[root@localhost ~]# ls /var/opt/gitlab/backups
1516123939_2018_01_17_10.3.3_gitlab_backup.tar
[root@localhost ~]#

备份文件名的前缀格式依次是:时间戳、日期、gitlab版本号

恢复数据需要停掉以下两个服务:

[root@localhost ~]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@localhost ~]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
[root@localhost ~]#

停止这两个服务的目录是让gitlab停止数据的变更,以免出现数据不一致的问题。

恢复数据命令如下:

# BACKUP的值是备份文件的前缀
[root@localhost ~]# gitlab-rake gitlab:backup:restore BACKUP=1516123939_2018_01_17_10.3.3

恢复的过程中会询问是否继续什么的,输入yes即可。

恢复完之后记得要启动服务:

gitlab-ctl start

注意:恢复备份需要注意版本问题,如果当时备份的是旧版本的gitlab数据,而现在要恢复到新版本的gitlab上,是无法恢复的。只有版本一致才能进行恢复。



本文转自 ZeroOne01 51CTO博客,原文链接:http://blog.51cto.com/zero01/2061710,如需转载请自行联系原作者

相关文章
|
3月前
|
存储 Java 开发工具
WinServer服务器上搭建Git代码库
本文介绍如何在WinServer服务器上搭建Git代码库。
73 0
|
5月前
|
开发工具 git
服务器定时自动拉取Git仓库代码自动部署
服务器定时自动拉取Git仓库代码自动部署
125 0
|
2月前
|
安全 Shell 网络安全
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
130 0
|
4月前
|
存储 Linux 网络安全
Git - Centos7下安装GitLab服务器
Git - Centos7下安装GitLab服务器
95 1
|
6月前
|
网络安全 开发工具 git
如何搭建你自己的 Git 服务器呢?
假如您既不想公开源代码,又舍不得给GitHub交保护费,那就只能自己搭建一台Git服务器作为私有仓库使用。
如何搭建你自己的 Git 服务器呢?
|
6月前
|
Linux 网络安全 开发工具
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
Git学习---Git快速入门、Git基础使用、Git进阶使用、Git服务器使用(IDEA集成GitHub、Gitee、GitLab)、GitHub Desktop客户端
|
8月前
|
缓存 网络协议 Linux
CentOS 服务器 git clone下载加速(下载过慢或超时)
CentOS 服务器 git clone下载加速(下载过慢或超时)
1065 0
|
安全 Ubuntu Shell
Git - 自定义Git之搭建Git服务器
Git - 自定义Git之搭建Git服务器
289 0
|
网络安全 开发工具 git
|
安全 Shell Linux
手把手教你如何搭建Git服务器?
Git 是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
1853 0