搭建git服务器
笔记内容:搭建git服务器
22.13 搭建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官网:
官方的安装文档:
注:官方说安装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用户的密码:
设置完之后就可以使用root账户登录了:
登录后的界面:
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上新建一个用户组:
然后在当前这个组里新建一个项目:
点击右上角头像,在settings界面里,添加密钥认证:
gitlap的用户可以自己注册,也可以由管理员进行创建,以下演示由管理员来创建用户:
剩下的默认即可,然后点击页面下方的Create user按钮。
创建完成后,还可以点击Edit按钮编辑该用户,例如修改个密码什么的:
保存修改后,退出当前用户,看看是否可以登录新建的用户:
然后再重新登录即可,我这里是登录成功的:
点击Create a project可以创建一个新项目:
创建成功:
剩下的操作就和GitHub很像了,而且平时我们在这个服务端上也都只是创建或编辑组和用户,大部分的操作都是在pc完成的。
gitlab常用命令:
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上,是无法恢复的。只有版本一致才能进行恢复。
- 获取局域网内所有用户和IP
- CLR和.Net对象生存周期
- PHP从数据库提取并显示数据的典型代码
- ThinkPHP导出CSV、Excel
- 使用HttpClient的优解
- 浅谈 EF CORE 迁移和实例化的几种方式
- Python标准库10 多进程初步 (multiprocessing包)
- .NET CORE——Console中使用依赖注入
- EntityFramework Core 自动绑定模型映射
- Python标准库09 当前进程信息 (os包)
- Dapper连接与事务的简单封装
- 数据可视化的秘密
- EntityFramework Core 学习扫盲
- 协议森林03 IP接力赛 (IP, ARP, RIP和BGP协议)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Mybatis-generator 逆向工程 自定义PO,xml,mapper,example
- 高速上云/网络穿透/视频上云网关EasyNTS组网服务登录状态检测优化记录
- 树莓派基础实验38:逻辑分析仪分析PWM、UART信号
- 【终端设备】视频上云/网络穿透EasyNTS云组网硬件终端无法单独修改账号的优化方式
- 测试环境问题排查的那些事儿
- RTSP流媒体协议视频平台EasyNVR和EasyNTS智能云组网同一浏览器运行为什么会导致EasyNTS无法登陆?
- Java:手写线程安全LRU缓存X探究影响命中率的因素
- 视频上云/网络穿透/网络映射服务EasyNTS设备管理为什么会出现无法搜索到设备的情况?
- 快速打造属于你的接口自动化测试框架
- 大数据下的质量体系建设
- PostgreSQL 日志系统 及 设置错误导致磁盘塞满案例
- 六、乘胜追击,将剩下的Git知识点搞定
- 树莓派基础实验39:解析无线电接收机PWM、SBUS信号
- nodejs源码分析第十九章 -- udp模块
- Spark Extracting,transforming,selecting features