GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的Web服务。
Gitlab是被广泛使用的基于git的开源代码管理平台, 基于Ruby on Rails构建, 主要针对软件开发过程中产生的代码和文档进行管理, Gitlab主要针对group和project两个维度进行代码和文档管理, 其中group是群组, project是工程项目, 一个group可以管理多个project, 可以理解为一个群组中有多项软件开发任务, 而一个project中可能包含多个branch, 意为每个项目中有多个分支, 分支间相互独立, 不同分支可以进行归并。
下面我们通过在Linux centOS8 中搭建GitLab私有仓库并且结合Cpolar内网穿透工具,实现在公网环境下也可以访问私有仓库Gitlab。
1. 下载Gitlab
在系统中创建一个目录,名字叫GitLab,存放下载的安装包
mkdir /usr/local/gitlabmkdir /usr/local/gitlabmkdir /usr/local/gitlab
进入创建的目录
cd /usr/local/gitlabcd /usr/local/gitlabcd /usr/local/gitlab
下载Gitlab安装包,等待下载完成
wget --content-disposition https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/8/gitlab-ce-15.0.2-ce.0.el8.x86_64.rpm/download.rpmwget --content-disposition https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/8/gitlab-ce-15.0.2-ce.0.el8.x86_64.rpm/download.rpmwget --content-disposition https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/8/gitlab-ce-15.0.2-ce.0.el8.x86_64.rpm/download.rpm
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[1]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/6e1f4cb8b1f36e4566a55d95aa893cf2.png)
2. 安装Gitlab
下载成功后,开始安装Gitlab,首先安装一个工具包,时间很长,等待安装完成:
yum install policycoreutils-python-utilsyum install policycoreutils-python-utilsyum install policycoreutils-python-utils
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[2]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/b053ddbf606c913eeed2d11c941b73fc.png)
然后安装Gitlab,进去我们上面创建的/usr/local/gitlab目录下执行:
rpm -Uvh gitlab-ce-15.0.2-ce.0.el8.x86_64.rpmrpm -Uvh gitlab-ce-15.0.2-ce.0.el8.x86_64.rpmrpm -Uvh gitlab-ce-15.0.2-ce.0.el8.x86_64.rpm
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[3]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/5125fdda775ccf5c2ac05720d26357f3.png)
安装完成后,更新配置,稍微需要点时间,耐心等待一下,完成后,我们可以看到用户名和密码
gitlab-ctl reconfiguregitlab-ctl reconfiguregitlab-ctl reconfigure
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[4]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/836aa99d96e0282712f463b9c6d67ae4.png)
上面那个路径就是密码路径,查看密码:
cat /etc/gitlab/initial_root_passwordcat /etc/gitlab/initial_root_passwordcat /etc/gitlab/initial_root_password
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[5]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/b135e47461b62bce1392f2059b0b4114.png)
3. 启动Gitlab
测试启动,没有出错表示成功
gitlab-ctl startgitlab-ctl startgitlab-ctl start
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[6]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/c05ab9727efc271144da1bd4ab547acc.png)
然后修改访问地址,编辑Gitlab配置文件,
vim /etc/gitlab/gitlab.rbvim /etc/gitlab/gitlab.rbvim /etc/gitlab/gitlab.rb
把external_url的值换成http://127.0.0.1:8088,端口号可以自己指定,这里指定8088,然后保存
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[7]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/4a387140b47a0f6bd5fd0bb35c7c0927.png)
修改完后重新加载配置文件
gitlab-ctl reconfiguregitlab-ctl reconfiguregitlab-ctl reconfigure
然后重新启动Gitlab
sudo gitlab-ctl restartsudo gitlab-ctl restartsudo gitlab-ctl restart
如果有防火墙,添加防火墙端口:8088
firewall-cmd --zone=public --add-port=8088/tcp --permanentfirewall-cmd --zone=public --add-port=8088/tcp --permanentfirewall-cmd --zone=public --add-port=8088/tcp --permanent
然后打开浏览器,输入Linux 局域网ip+8088,即可访问成功
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[8]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/c8eba6fa08845a27d207b07563deb977.png)
输入上面的用户名:root 和上面查看的密码即可登录成功啦
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[9]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/1f66b6823557ac44dea16672b5ffb6ae.png)
4. 安装cpolar
上面我们在本地Linux虚拟机安装了Gitlab,下面我们安装cpolar内网传统工具,通过cpolar的http公网地址,我们可以很容易远程也可以访问GitLab,而无需自己注册域名.下面是安装cpolar步骤
- 使用一键脚本安装命令
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bashcurl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bashcurl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
- token认证
登录cpolar官网后台,点击左侧的验证,查看自己的认证token,之后将token贴在命令行里:
cpolar authtoken xxxxxxxcpolar authtoken xxxxxxxcpolar authtoken xxxxxxx
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[10]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/2bd4a13cd8b0a504ab1c2c977acc9d3a.png)
- 向系统添加服务,将cpolar配置为开机自启
sudo systemctl enable cpolarsudo systemctl enable cpolarsudo systemctl enable cpolar
- 启动cpolar服务
sudo systemctl start cpolarsudo systemctl start cpolarsudo systemctl start cpolar
5. 创建隧道配置访问地址
成功启动cpolar服务后,我们在浏览器上访问Linux局域网ip地址+9200端口,登录cpolar web UI管理界面。
登录成功后,点击左侧仪表盘的隧道管理——创建隧道,创建一个http协议的隧道指向上面设置的8088端口:
- 隧道名称:可自定义,注意不要与已有的隧道名称重复
- 协议:http
- 本地地址:8088
- 域名类型:免费选择随机域名
- 地区:选择China VIP
点击创建
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[11]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/a06e31cb40314225d86d01dc0c0ac492.png)
然后打开在线隧道列表,查看并且复制公网地址
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[12]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/773afd168ad6019cf2b81aaead5b7a65.png)
然后打开浏览器,输入公网地址,即可访问成功
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[13]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/43deea24fa996adb44dfc399b93ed916.png)
6. 固定GitLab访问地址
由于刚刚创建隧道使用的是随机临时地址,该地址会在24小时内发生变化,为了长期远程访问,我们接下来将这个公网地址配置为固定的。
6.1 保留二级子域名
需升级至基础套餐或以上才支持配置二级子域名
登录cpolar官网后台,点击左侧仪表盘的预留
,找到保留二级子域名
,为http隧道保留一个二级子域名。
- 地区:选择服务器地区
- 名称:填写您想要保留的二级子域名(可自定义)
- 描述:即备注,可自定义填写
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[14]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/a10bcb05752c53773cb924292545346c.png)
本例保留一个名称为gitlabTest
的二级子域名。子域名保留成功后,我们将子域名复制下来,接下来需要将其配置到隧道中去。
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[15]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/8abd4d5685346f6b81f4c947a9ed1cd9.png)
6.2 配置二级子域名
登录cpolar web ui管理界面。点击左侧仪表盘的隧道管理
——隧道列表
,找到需要配置二级子域名的隧道,点击右侧的编辑
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[16]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/124f236c7b1cb7677d9a135e741a44c7.png)
修改隧道信息,将二级子域名配置到隧道中:
- 域名类型:改为选择
二级子域名
- Sub Domain:填写我们刚刚所保留的二级子域名(本例为
gitlabTest
)
修改完成后,点击更新
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[17]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/4ebc66fd189fe3334d3c54a39c624914.png)
隧道更新成功后,点击左侧仪表盘的状态
——在线隧道列表
,可以看到隧道的公网地址,已经更新为二级子域名了,将公网地址复制下来。
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[18]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/727363ee91437aa8bc3906057338230a.png)
7. 测试访问二级子域名
打开浏览器,我们来测试一下访问配置成功的二级子域名,测试成功,可以正常访问。
现在,我们全网唯一的私有二级子域名,就创建好了。
而且这个地址也不会再随机变化,他是固定不变的,只要保持隧道在线,我们在外就可以通过这个公网地址,随时随地远程访问,无需公网IP,也不用设置路由器。
![Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程) 图片[19]-Linux如何搭建GitLab私有仓库,并内网穿透实现公网访问(GitLab私有仓库搭建详细教程)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/03/4685d370be049c181d6cb66395d552c0.png)