温馨提示×

GitLab怎么在CentOS上运行

小樊
49
2026-08-29 19:55:46
栏目: 智能运维

在 CentOS 上运行 GitLab,官方最推荐、最稳定的方式是使用 GitLab CE(社区版)官方 RPM 包。下面我按从零开始给你一份可操作的完整流程,适用于 CentOS 7 / CentOS 8 Stream


一、准备工作

1️⃣ 系统要求(最低)

  • CPU:2 核以上(推荐 4 核)
  • 内存:4GB 以上(8GB 更稳)
  • 磁盘:至少 20GB(视仓库大小)
  • CentOS 7 / 8 Stream(64 位)

2️⃣ 更新系统

sudo yum update -y

二、安装依赖

CentOS 7

sudo yum install -y curl policycoreutils-python openssh-server

CentOS 8

sudo dnf install -y curl policycoreutils openssh-server

启动 SSH

sudo systemctl enable sshd
sudo systemctl start sshd

三、安装 GitLab CE(官方方式 ✅)

1️⃣ 添加 GitLab 官方仓库

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

2️⃣ 安装 GitLab CE

方式一:使用域名(推荐)

sudo EXTERNAL_URL="http://gitlab.example.com" yum install -y gitlab-ce

gitlab.example.com 换成你的 服务器 IP 或域名

方式二:直接安装(不指定域名)

sudo yum install -y gitlab-ce

四、配置 GitLab

1️⃣ 初始化配置

sudo gitlab-ctl reconfigure

⚠️ 这一步会:

  • 生成配置文件
  • 启动所有服务
  • 可能需要几分钟

2️⃣ 修改访问地址(可选)

编辑配置文件:

sudo vi /etc/gitlab/gitlab.rb

修改:

external_url 'http://你的IP或域名'

然后重新配置:

sudo gitlab-ctl reconfigure

五、访问 GitLab

在浏览器中访问:

http://服务器IP

首次登录:

  • 用户名:root
  • 密码:查看下面命令
sudo cat /etc/gitlab/initial_root_password

⚠️ 密码只显示一次,建议登录后立即修改


六、常用管理命令

查看状态

sudo gitlab-ctl status

启动 / 停止 / 重启

sudo gitlab-ctl start
sudo gitlab-ctl stop
sudo gitlab-ctl restart

查看日志

sudo gitlab-ctl tail

七、防火墙配置(很重要)

CentOS 7

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

CentOS 8

sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload

八、可选配置(企业常用)

✅ 配置 SMTP 邮件

sudo vi /etc/gitlab/gitlab.rb

配置 smtp_settings,然后:

sudo gitlab-ctl reconfigure

✅ 配置 HTTPS(Let’s Encrypt)

external_url 'https://gitlab.example.com'
letsencrypt['enable'] = true

九、常见问题

❌ 内存不足

  • 至少 4GB
  • 避免与 Jenkins、Docker 同机

❌ 无法访问

  • 防火墙
  • SELinux
  • 端口被占用

❌ CentOS 8 已停止维护

✅ 建议:

  • Rocky Linux / AlmaLinux
  • Docker 方式运行 GitLab

十、Docker 方式(替代方案)

如果你想用 Docker:

docker run -d \
  --hostname gitlab.example.com \
  -p 80:80 -p 443:443 -p 22:22 \
  --name gitlab \
  gitlab/gitlab-ce:latest

总结一句话

CentOS 上运行 GitLab 最稳妥的方式就是:官方 RPM + gitlab-ctl 管理

如果你愿意,可以告诉我:

  • ✅ CentOS 几?
  • ✅ 是否生产环境?
  • ✅ 是否需要 HTTPS / 邮件 / 备份?

我可以帮你写一份完全贴合你环境的 GitLab 部署方案

0