阅读量:2
Ubuntu环境下搭建GitLab私有仓库完整步骤
1. 准备工作
- 系统要求:Ubuntu 20.04及以上版本(推荐),确保系统已更新(
sudo apt update && sudo apt upgrade -y)。 - 依赖包:安装GitLab运行必需的组件(包括SSH服务、证书工具、数据库等):
安装Postfix时选择“Internet Site”,并设置系统域名(如sudo apt install -y curl openssh-server ca-certificates postfixexample.com,若为内网可随意填写)。
2. 添加GitLab官方仓库
- 导入GPG密钥(验证软件包完整性):
curl https://packages.gitlab.com/gpg.key | sudo apt-key add - - 添加APT源(以Ubuntu 22.04为例,其他版本替换
jammy为对应代号):echo "deb https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu jammy main" | sudo tee /etc/apt/sources.list.d/gitlab-ce.list - 更新软件包列表:
sudo apt update
3. 安装GitLab Community Edition(CE,社区免费版)
- 安装GitLab:
sudo apt install -y gitlab-ce - 配置外部访问URL(修改
/etc/gitlab/gitlab.rb,将your_server_ip替换为服务器实际IP或域名):
找到sudo vim /etc/gitlab/gitlab.rbexternal_url行,取消注释并修改为:若需使用域名,可将external_url 'http://your_server_ip'your_server_ip替换为域名(如example.com)。
4. 启动GitLab服务
- 重新配置并启动:
sudo gitlab-ctl reconfigure # 应用配置变更(耗时约1-2分钟) sudo gitlab-ctl start # 启动GitLab服务 - 设置开机自启(可选但推荐):
sudo systemctl enable gitlab-runsvdir.service
5. 访问GitLab Web界面
- 获取初始管理员密码(安装后自动生成,有效期24小时):
sudo cat /etc/gitlab/initial_root_password - 登录Web界面:
打开浏览器,访问http://your_server_ip,使用默认管理员账号root和上述密码登录。 - 修改初始密码(登录后立即操作):
进入root用户设置→Password,设置新密码(需包含大小写字母、数字和特殊字符)。
6. 创建私有仓库
- 创建用户(可选但推荐,便于权限管理):
点击右上角头像→Admin Area→Users→New User,填写用户名、邮箱、密码等信息,点击Create user。 - 创建项目(私有仓库):
登录后点击右上角New project,输入项目名称(如my-private-repo),选择Visibility Level为Private(仅项目成员可访问),点击Create project。
7. 配置本地Git客户端
- 生成SSH密钥(若未生成):
按提示回车,默认保存到ssh-keygen -t rsa -b 4096 -C "your_email@example.com"~/.ssh/id_rsa(私钥)和~/.ssh/id_rsa.pub(公钥)。 - 添加公钥到GitLab:
复制公钥内容(cat ~/.ssh/id_rsa.pub),登录GitLab→Profile Settings→SSH Keys,粘贴公钥并点击Add key。 - 克隆私有仓库:
在本地终端执行:输入GitLab账号密码(或SSH密钥密码)即可克隆。git clone git@your_server_ip:root/my-private-repo.git
8. 可选优化(增强安全性与可用性)
- 配置SSL证书(启用HTTPS):
使用Let’s Encrypt免费获取证书(需域名):
按提示完成配置,GitLab会自动使用HTTPS访问。sudo apt install -y certbot python3-certbot-nginx sudo certbot --nginx -d your_domain.com - 配置防火墙:
若启用ufw(Ubuntu防火墙),允许HTTP(80)、HTTPS(443)和SSH(22)端口:sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw allow 22/tcp sudo ufw enable
通过以上步骤,即可在Ubuntu服务器上搭建GitLab私有仓库,实现团队代码的私有化托管与协作。
以上就是关于“ubuntu中gitlab如何搭建私有仓库”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm