22.13 搭建git服务器nginx
22.14 22.15 安装gitlabgit
22.16 使用gitlabgithub
22.17 gitlab备份与恢复web
22.13 搭建git服务器shell
github毕竟是公开的,而私有仓库又得花钱买。因此咱们能够想办法搭建一个私有的,只本身公司使用的。Gitlab是个不错的选择。在介绍它以前,先讲述一下命令行的git服务器vim
搭建准备工做centos
1 找一台服务器,首先要安装git,bash
yum install -y git
2 添加git用户,而且设置shell为/usr/bin/git-shell,目的是为了避免让git用户远程登录
服务器
useradd -s /usr/bin/git-shell git cd /home/git
3 建立authorized_keys文件,并更改属主、属组和权限,用来存客户端机器上的公钥ssh
mkdir .ssh touch .ssh/authorized_keys chown -R git.git .ssh chmod 600 .ssh/authorized_keys
4 把客户端的公钥,放到服务器的/root/.ssh/authorized_keys上,失效免密登陆
[root@9F-VM1 ~]# cat .ssh/id_rsa.pub Nlet2N+TXAQJDOnrRtN42qFoRLAYXO+D8xsYDmwPMLdJymcYBbQJL5jU65/+loL7kuHcTtRSwk46xQXdLFPjN/c2c0I6OofxZAVKgPHGtjd8Y8dkOzYep6S6n7C/jug5PwbQedFoNy16+0JU1lBQa6IxqrWsXdLuM3Gc8Oko4sAvKQgZQRfJh root@9F-VM1 [root@DD-Server-9F ~]# vim !$ vim .ssh/authorized_keys #9F-VM1 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7TRMjzg1SiLIZg3X3HBVaVzvfCMouDV/d0owu/SLddyHAitEvii3Nq1ElOhuB2hDbVq9fhEH61ZuBVqI+PkcI0KE04p8/KCSzF4dEvUdIYrQAeafxnLd7hl4QofmJbEnP07FQOurFFXItnniGz12mN95cbvijoLPNlet2N+TXA
5 登陆验证测试
[root@9F-VM1 ~]# ssh git@192.88.24.200 Last login: Tue Sep 4 16:16:31 2018 from 192.88.29.250 fatal: Interactive git shell is not enabled. hint: ~/git-shell-commands should exist and have read and execute access. Connection to 192.88.24.200 closed.
不能直接远程经过登陆的,可是能验证到客户端29.250的公钥,稍后会再作说明。
在服务器建立git仓库
6定好存储git仓库的目录,好比 /data/git
mkdir /data/git cd /data/git
7 初始化仓库
[root@DD-Server-9F git]# git init --bare sample.git
# 会建立一个裸仓库,裸仓库没有工做区,由于服务器上的Git仓库纯粹是为了共享,因此不让用户直接登陆到服务器上去改工做区,而且服务器上的Git仓库一般都以.git结尾
chown -R git.git sample.git
以上操做是在git服务器上作的,平时git服务器是不须要开发人员登陆修改代码的,它仅仅是充当着一个服务器的角色,就像github同样,平时操做都是在咱们本身的pc上作的
8 客户端使用仓库
首先要把客户端上的公钥放到git服务器上/home/git/.ssh/authorized_keys文件里
在客户端上(本身pc)克隆远程仓库
执行命令:
git clone git@ip:/data/git/sample.git
[root@9F-VM1 git]# cd /data/git [root@9F-VM1 git]# git clone git@192.88.24.200:/data/git/sample.git 正克隆到 'sample'... warning: 您彷佛克隆了一个空版本库。 [root@9F-VM1 git]# cd sample/ [root@9F-VM1 sample]# ls -la 总用量 0 drwxr-xr-x 3 root root 18 9月 4 16:41 . drwxr-xr-x 9 root root 131 9月 4 16:41 .. drwxr-xr-x 7 root root 119 9月 4 16:41 .git
此时就能够在当前目录下生成一个sample的目录,这个就是咱们克隆的远程仓库了。进入到这里面,能够开发一些代码,而后push到远程。
9 测试
添加文件实现远程仓库功能
[root@9F-VM1 sample]# touch 1.txt && echo -e "111\n222\n333" >> 1.txt [root@9F-VM1 sample]# cat 1.txt 111 222 333 [root@9F-VM1 sample]# git add 1.txt [root@9F-VM1 sample]# git commit -m "add the first file" [master(根提交) e46c02a] add the first file 1 file changed, 3 insertions(+) create mode 100644 1.txt [root@9F-VM1 sample]# git push origin master Counting objects: 3, done. Writing objects: 100% (3/3), 219 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@192.88.24.200:/data/git/sample.git * [new branch] master -> master
9.1 在别的目录克隆远程仓库,查看协同效果是否实现
[root@9F-VM1 git]# cd /data/git [root@9F-VM1 git]# mkdir sample-t [root@9F-VM1 git]# cd sample-t/ [root@9F-VM1 sample-t]# git clone git@192.88.24.200:/data/git/sample.git 正克隆到 'sample'... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0) 接收对象中: 100% (3/3), done. [root@9F-VM1 sample-t]# ls sample [root@9F-VM1 sample-t]# cd sample/ [root@9F-VM1 sample]# ls 1.txt [root@9F-VM1 sample]# vim 2.txt [root@9F-VM1 sample]# echo "222" > 2.txt [root@9F-VM1 sample]# ls 1.txt 2.txt [root@9F-VM1 sample]# git add 2.txt [root@9F-VM1 sample]# git commit -m "add 2.txt" [master 0f7b313] add 2.txt 1 file changed, 1 insertion(+) create mode 100644 2.txt [root@9F-VM1 sample]# git push Counting objects: 4, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@192.88.24.200:/data/git/sample.git e46c02a..0f7b313 master -> master
切换原来开始的仓库目录
[root@9F-VM1 sample]# cd - /data/git/sample [root@9F-VM1 sample]# ls 1.txt [root@9F-VM1 sample]# git pull remote: Counting objects: 4, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. 来自 192.88.24.200:/data/git/sample e46c02a..0f7b313 master -> origin/master 更新 e46c02a..0f7b313 Fast-forward 2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 2.txt [root@9F-VM1 sample]# ls 1.txt 2.txt
协同实现,远程仓库项目搭建完成
22.14 安装gitlab(上)
安装方法一:
gitlab官网 https://about.gitlab.com/gitlab-com/
官方安装文档 https://about.gitlab.com/installation/?version=ce#centos-7 (ce/ee)
要求服务器内存很多于2g
方法二:本地yum源安装
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 install -y gitlab-ce gitlab-ctl reconfigure
22.15 安装gitlab(下)
执行命令自动加载配置
gitlab-ctl reconfigure
若是系统自带nginx,先把它停掉,由于gitlab也自带了一个nginx,以避免发生冲突
/etc/init.d/nginx stop chkconfig nginx off netstat -lnpt //查看监听端口 gitlab-ctl #后面带的执行命令(中止,开始,重启,状态) gitlab-ctl stop/restart/start/status
访问gitlab服务器ip:192.88.24.200
第一次登陆须要建立新的密码
改完密码后须要登陆
默认帐户:root
密码:新的密码
22.16 使用gitlab
gitlab经常使用命令 https://www.cnyunwei.cc/archives/1204
1 使用gitlab自带的nginx配置代理
路径:
/var/opt/gitlab/nginx/conf/nginx.conf
#主配置文件
/var/opt/gitlab/nginx/conf/gitlab-http.conf
#gitlab相关的web配置文件,修改相关gitlab web能够在这里修改
2 建立新的组group
3 建立新的项目project
4 在gitlab添加SSH Key
User Settings-->SSH key-->把公钥复制进去
5 添加组
管理员区域 添加用户,组,项目
6 添加用户
密码获取方法一:新的用户密码会使用连接方式将密码发送通知给邮件地址来获取
密码获取方法二:选中用户-->Edit下直接编辑定义密码
7 新用户登陆
建立项目project
22.17 gitlab备份与恢复
1 gitlab备份
gitlab-rake gitlab:backup:create [root@DD-Server-9F ~]# gitlab-rake gitlab:backup:create Dumping database ... Dumping PostgreSQL database gitlabhq_production ... [DONE] done Dumping repositories ... * g1/p1 ... [SKIPPED] [SKIPPED] Wiki * kevin/kp01 ... [SKIPPED] [SKIPPED] Wiki 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: 1536129054_2018_09_05_11.2.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
备份目录在/var/opt/gitlab/backups
[root@DD-Server-9F ~]# ls /var/opt/gitlab/backups/ 1536129054_2018_09_05_11.2.3_gitlab_backup.tar
2 gitlab 恢复
gitlab 恢复 先停服务 gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq
[root@DD-Server-9F ~]# gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq ok: down: unicorn: 1s, normally up ok: down: sidekiq: 0s, normally up
停掉服务后,继续恢复
gitlab-rake gitlab:backup:restore BACKUP=xxxxx (这里是一个编号,即备份文件的前缀,时间戳_日期_版本号)
[root@DD-Server-9F ~]# gitlab-rake gitlab:backup:restore BACKUP=1536129054_2018_09_05_11.2.3