ubuntu如何配置gitlab环境
Ubuntu配置GitLab环境详细步骤
1. 环境准备
1.1 系统要求
- 操作系统:Ubuntu 22.04及以上(推荐LTS版本,稳定性更高);
- 硬件配置:至少4GB内存(建议8GB及以上,避免性能瓶颈)、4核CPU、100GB及以上磁盘空间(用于存储代码仓库及附件);
- 网络:确保服务器能访问互联网(需下载GitLab安装包及依赖)。
1.2 基础配置
- 设置主机名:将主机名改为易识别的标识(如
gitlab),方便后续配置域名解析。sudo hostnamectl set-hostname gitlab & & bash # 立即生效 echo "127.0.0.1 gitlab" | sudo tee -a /etc/hosts # 绑定主机名与IP - 停止/禁用防火墙(可选,若需严格安全控制,可后续配置防火墙规则):
sudo systemctl disable --now ufw # 禁用ufw防火墙 - 更新系统包:同步软件源并升级现有包,避免依赖冲突。
sudo apt update & & sudo apt upgrade -y
1.3 安装依赖包
GitLab运行需依赖SSH(远程访问)、CA证书(HTTPS加密)、时区数据(时间同步)等工具,通过以下命令安装:
sudo apt install -y curl openssh-server ca-certificates tzdata perl
注:openssh-server用于GitLab的SSH协议访问(默认端口22);postfix为邮件服务(可选,若需邮件通知需额外配置)。
2. 添加GitLab官方包存储库
GitLab未包含在Ubuntu默认源中,需通过官方脚本添加专用存储库,以获取最新稳定版本的安装包。
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
注:若需安装企业版(GitLab EE),将gitlab-ce替换为gitlab-ee即可。
3. 安装GitLab Community Edition(CE)
3.1 指定外部URL
GitLab需通过外部URL访问(如服务器IP或域名),修改默认配置以适配你的环境:
EXTERNAL_URL="http://your_server_ip" # 替换为实际IP或域名(如http://192.168.1.100)
注:若使用域名,需提前解析到服务器IP,并配置SSL证书(可选但推荐)。
3.2 安装GitLab CE
通过apt命令安装GitLab CE,系统会自动解决依赖关系:
sudo apt install gitlab-ce -y
注:若需指定版本(如17.5.2),可使用sudo apt install gitlab-ce=17.5.2-ce.0(需提前查询可用版本)。
4. 配置GitLab
4.1 重新配置GitLab
安装完成后,需运行reconfigure命令应用配置(如外部URL、服务端口等),生成必要的系统文件:
sudo gitlab-ctl reconfigure
此命令会自动配置SSH端口、Web服务端口、数据库连接等,耗时约1-2分钟。
4.2 启动GitLab服务
启动GitLab相关服务(包括Web服务器、数据库、Git服务等),并设置为开机自启动:
sudo gitlab-ctl start # 启动服务
sudo systemctl enable gitlab-runsvdir.service # 开机自启动
4.3 查看服务状态
确认GitLab各组件是否正常运行(应显示run状态):
sudo gitlab-ctl status
示例输出:
run: alertmanager: (pid 1234) 1m;
run: log: (pid 1235) 1m
run: gitaly: (pid 1236) 1m;
run: log: (pid 1237) 1m
run: gitlab-exporter: (pid 1238) 1m;
run: log: (pid 1239) 1m
run: gitlab-workhorse: (pid 1240) 1m;
run: log: (pid 1241) 1m
run: logrotate: (pid 1242) 1m;
run: log: (pid 1243) 1m
run: postgres: (pid 1244) 1m;
run: log: (pid 1245) 1m
run: prometheus: (pid 1246) 1m;
run: log: (pid 1247) 1m
run: redis: (pid 1248) 1m;
run: log: (pid 1249) 1m
run: sidekiq: (pid 1250) 1m;
run: log: (pid 1251) 1m
run: unicorn: (pid 1252) 1m;
run: log: (pid 1253) 1m
4.4 查看GitLab版本
确认安装的GitLab版本,确保符合预期:
gitlab-ce --version
示例输出:
gitlab-ce 17.5.2-ce.0
5. 访问GitLab
5.1 浏览器访问
在浏览器中输入配置的外部URL(如http://your_server_ip),进入GitLab登录页面。
- 首次登录:默认管理员账号为
root,系统会提示设置新密码(需包含大小写字母、数字和特殊字符,长度≥8位)。 - 后续登录:使用
root账号及设置的密码登录。
5.2 可选配置
- 修改界面语言:登录后进入
Settings → Appearance,选择“中文(简体)”即可切换界面语言。 - 配置邮件通知(用于密码重置、Issue提醒等):编辑
/etc/gitlab/gitlab.rb文件,取消以下注释并修改为你的SMTP信息:保存后运行gitlab_rails['smtp_enable'] = true gitlab_rails['smtp_address'] = "smtp.example.com" gitlab_rails['smtp_port'] = 587 gitlab_rails['smtp_user_name'] = "your_email@example.com" gitlab_rails['smtp_password'] = "your_password" gitlab_rails['smtp_domain'] = "example.com" gitlab_rails['smtp_authentication'] = "login" gitlab_rails['smtp_enable_starttls_auto'] = true gitlab_rails['gitlab_email_from'] = "your_email@example.com"sudo gitlab-ctl reconfigure使配置生效。
6. 常用维护命令
- 停止GitLab服务:
sudo gitlab-ctl stop - 重启GitLab服务:
sudo gitlab-ctl restart - 查看GitLab日志(排查问题必备):
sudo gitlab-ctl tail(实时日志)或sudo less /var/log/gitlab/gitlab-rails/production.log(历史日志) - 重置root密码(若忘记):
sudo gitlab-rails console production # 进入Rails控制台 user = User.find_by(username: 'root') # 查找root用户 user.password = 'new_password' # 设置新密码 user.password_confirmation = 'new_password' # 确认新密码 user.save! exit # 退出控制台
注意事项
- 内存要求:GitLab对内存需求较高,若服务器内存不足(<
4GB),建议关闭不必要的服务(如
postgresql、redis)或升级硬件。 - 数据备份:定期备份GitLab数据(包括仓库、数据库、配置文件),可使用以下命令:
sudo gitlab-rake gitlab:backup:create # 创建备份(默认路径:/var/opt/gitlab/backups) - 安全加固:生产环境中建议开启防火墙(
ufw),仅允许HTTP(80)、HTTPS(443)、SSH(22)端口访问;配置SSL证书(如Let’s Encrypt),启用HTTPS加密传输。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: ubuntu如何配置gitlab环境
本文地址: https://pptw.com/jishu/735368.html
