温馨提示×

centos gitlab如何配置

小樊
36
2026-08-03 15:50:42
栏目: 智能运维

下面给你一份**在 CentOS 上配置 GitLab(CE 社区版)**的完整流程,适用于 CentOS 7 / CentOS 8 Stream,从安装到基础配置、常用优化都会讲到。


一、环境准备

1. 系统要求

  • 内存:≥ 4GB(推荐 8GB)
  • CPU:≥ 2 核
  • 磁盘:≥ 20GB
  • 系统:CentOS 7 / CentOS 8 Stream

2. 关闭 SELinux(可选但推荐)

setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

3. 开放端口

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

二、安装 GitLab CE

1. 安装依赖

yum install -y curl policycoreutils openssh-server \
    openssh-clients postfix wget

启动 SSH 和 Postfix:

systemctl enable sshd
systemctl start sshd

systemctl enable postfix
systemctl start postfix

2. 添加 GitLab 官方仓库(推荐)

CentOS 7

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

CentOS 8

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

3. 安装 GitLab CE

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

说明

  • EXTERNAL_URL 是 GitLab 访问地址
  • 如果是内网 IP:
EXTERNAL_URL="http://192.168.1.100"

安装完成后会看到:

Thank you for installing GitLab!

三、基础配置

1. 修改配置文件

vim /etc/gitlab/gitlab.rb

常见配置项:

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

# 邮箱配置(可选)
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "xxx@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

2. 重新加载配置

gitlab-ctl reconfigure

⚠️ 这一步必须执行


四、访问 GitLab

浏览器访问:

http://服务器IP

首次登录会要求设置 root 密码

默认用户:

用户名:root
密码:你设置的密码

五、常用管理命令

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

六、中文界面设置

  1. 登录 GitLab
  2. 右上角头像 → Preferences
  3. Localization → 选择 简体中文
  4. 保存并刷新

七、GitLab 数据目录说明

目录 作用
/var/opt/gitlab 数据目录
/etc/gitlab 配置文件
/var/log/gitlab 日志
/opt/gitlab 程序目录

八、常见问题

1. 访问 502

  • 内存不足(最常见)
  • 端口被占用
  • 查看日志:
gitlab-ctl tail

2. 忘记 root 密码

gitlab-rails console
user = User.where(id: 1).first
user.password = 'newpassword'
user.password_confirmation = 'newpassword'
user.save!

九、生产环境建议

✅ 使用 域名 + HTTPS
✅ 定期备份:

gitlab-rake gitlab:backup:create

✅ 关闭注册(Settings → General → Sign-up restrictions)


如果你需要:

  • GitLab + HTTPS(Let’s Encrypt)
  • GitLab Runner 配置
  • LDAP / 企业微信 / 钉钉集成
  • GitLab 升级 / 迁移

告诉我你的 CentOS 版本 + 使用场景,我可以给你更精确的配置方案。

0