温馨提示×

GitLab在CentOS上如何部署

小樊
55
2026-07-20 03:41:50
栏目: 智能运维

下面以 GitLab CE(社区版)在 CentOS 7 / CentOS 8 上的常见部署方式 为例,给你一个清晰、可操作的完整流程。如果你用的是 Rocky / Alma / RHEL,步骤基本一致。


一、部署前准备

1️⃣ 系统要求

最低配置(建议)

  • CPU:2 核
  • 内存:4GB 以上(强烈建议 8GB)
  • 磁盘:至少 20GB(视仓库大小而定)

⚠️ GitLab 比较吃内存,2GB 内存基本不可用。


2️⃣ 关闭或配置防火墙 / SELinux(可选)

# 关闭 SELinux(不推荐生产环境)
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0

防火墙放行 HTTP/HTTPS:

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

二、安装依赖

CentOS 7

yum install -y curl policycoreutils openssh-server openssh-clients
systemctl enable sshd
systemctl start sshd

CentOS 8 / Rocky / Alma

dnf install -y curl policycoreutils openssh-server
systemctl enable sshd
systemctl start sshd

三、安装 GitLab CE(官方推荐方式)

1️⃣ 添加 GitLab 官方仓库

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

2️⃣ 安装 GitLab CE

✅ 默认使用域名(推荐)

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

✅ 使用服务器 IP

EXTERNAL_URL="http://192.168.1.100" yum install -y gitlab-ce

安装过程会较慢,耐心等待。


四、配置 GitLab

1️⃣ 初始化配置

gitlab-ctl reconfigure

✅ 这一步会:

  • 生成配置
  • 启动所有服务
  • 初始化数据库

2️⃣ 查看 GitLab 状态

gitlab-ctl status

五、访问 GitLab

浏览器访问:

http://服务器IP

首次登录:

  • 用户名:root
  • 密码在:
cat /etc/gitlab/initial_root_password

⚠️ 该文件 24 小时后自动删除


六、修改 GitLab 配置(常见)

编辑配置文件:

vim /etc/gitlab/gitlab.rb

示例:修改访问地址

external_url 'http://gitlab.example.com'

改完后执行

gitlab-ctl reconfigure

七、常用管理命令

gitlab-ctl start        # 启动
gitlab-ctl stop         # 停止
gitlab-ctl restart      # 重启
gitlab-ctl status       # 状态
gitlab-ctl tail         # 查看日志

八、配置 HTTPS(可选但推荐)

1️⃣ 使用 Let’s Encrypt

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

然后:

gitlab-ctl reconfigure

九、配置 SMTP 邮件(可选)

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "123456@qq.com"
gitlab_rails['smtp_password'] = "授权码"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true

十、常见问题

❌ 502 错误

  • 内存不足
  • gitlab-ctl reconfigure 未完成
  • 端口被占用

❌ 访问慢

  • 关闭 Prometheus(消耗资源)
prometheus_monitoring['enable'] = false

十一、要不要我帮你?

我可以继续帮你:

  • ✅ 做 生产级 GitLab 部署方案
  • CentOS 7 / 8 指定版本
  • Docker 方式部署
  • GitLab + CI/CD 配置
  • 迁移 / 备份 / 升级

你可以直接告诉我:

我的是 CentOS 几?内存多少?是否生产环境?

0