作者:zzuuoo666
转载自:
https://www.cnblogs.com/zzuuoo666/p/12597498.html
前言
我们都知道GitHub、oschina等等的存在,也是非常庞大的开源代码社区。如果公司想要使用git来管理代码,但不想所有人都来荡你的代码就需要私服了。
而GitHub的私服是花钱的,我们常常使用gitlab来搭建私服。GitHub和gitlab是分别两个公司提供的。下面记录一下搭建gitlab私服的过程已经git的学习。
环境
首先要先安装git在本地,我的是macOS,在安装xcode的时候就已经安装好git环境的,其他系统可以在官网下载git :https://www.git-scm.com/

公司的服务器是Ubuntu

git本身是一个版本控制软件 git != github
github和gitlab是一个管理版本的服务端软件,gitlab可以理解为企业内部管理的GitHub
为什么使用git
本地建立版本库。
本地版本控制
多主机异地协同工作
重写提交说明
有后悔药可以吃
更好用的提交列表
更好的差异比较
更完备的分支系统
速度极快



Gitlab安装
1.安装依赖包
sudo apt-get install curl openssh-server ca-certificates postfix
注:执行完成后,出现邮件配置,选择Internet那一项(不带Smarthost的)
2.利用清华大学的镜像(https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/)来进行主程序的安装
首先信任 GitLab 的 GPG 公钥
curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/null
使用root用户修改配置文件
vi /etc/apt/sources.list.d/gitlab-ce.list
#添加以下内容
deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu xenial main
vi /etc/gitlab/gitlab.rb
external_url 'http://192.168.31.130/'
安装 gitlab-ce
sudo apt-get update
sudo apt-get install gitlab-ce
3.启动sshd和postfix服务
service sshd start
service postfix start
4.添加防火墙规则
sudo iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
5.启动各项服务
sudo gitlab-ctl reconfigure
6..检查GitLab是否安装好并且已经正确运行
sudo gitlab-ctl status
#如果得到类似下面的结果,则说明GitLab运行正常

7.浏览web页面并设置密码
http://192.168.31.130/

8.创建组

9.添加ssh key
git仓库之间的代码传输协议主要使用ssh协议。而一般搭建gitlab的时候使用的git用户是没有密码的,因此直接ssh是不能登录的,就需要使用ssh-keygen上传公钥,使用非对称加密传输。下面讲述如何上传你的ssh公钥:
生成sshkey
在终端中敲下面的命令,第一步会生成一对私钥和公钥,分别存在 ~/.ssh/id_rsa
和~/.ssh/id_rsa.pub
中。第二步查看公钥字符串。
ssh-keygen -t rsa -C "邮箱地址"
cat ~/.ssh/id_rsa.pub
在面板上依次点击Profile Settings –> SSH Keys –> Add SSH Keys。然后把上一步中的id_rsa.pub
中的内容拷贝出来粘贴到输入框中,保存。

完成上面两步之后就成功的添加了sshkey了,然后就可以上传代码了。
10.创建项目
创建项目Test_Project

下一步,选择组,输入项目名

下一步,创建完成

在项目下面会有git的操作指南教你如何操作

完成创建项目!
获取/修改超级管理员root的密码
a、 切换目录:cd /opt/gitlab/bin
b、执行 :sudo gitlab-rails console production 命令 开始初始化密码 (如果报错 试试 sudo gitlab-rails console)
c、在irb(main):001:0> 后面通过 u=User.where(id:1).first 来查找与切换账号(User.all 可以查看所有用户)
d、通过u.password='12345678'设置密码为12345678(这里的密码看自己喜欢):
e、通过u.password_confirmation='12345678' 再次确认密码
f、通过 u.save!进行保存(切记切记 后面的 !)
g、如果看到上面截图中的true ,恭喜你已经成功了,执行 exit 退出当前设置流程即可。
h、回到gitlab ,可以通过 root/12345678 这一超级管理员账号登录了