温馨提示×

CentOS怎么配置GitLab

小樊
39
2026-08-18 17:10:55
栏目: 智能运维

下面给你一份**在 CentOS 上安装和配置 GitLab(CE 社区版)**的完整流程,适用于 CentOS 7 / CentOS 8 Stream,并包含常见配置项。


一、环境准备

1. 系统要求

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

2. 关闭 SELinux(建议)

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

3. 开放端口

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

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

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

说明

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

四、初始化 GitLab

gitlab-ctl reconfigure

⏳ 首次执行时间较长,耐心等待。


五、访问 GitLab

浏览器访问:

http://服务器IP

首次登录:

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

⚠️ 密码只在 24 小时内有效,建议登录后立即修改。


六、常用配置(重点)

1. 修改访问地址

编辑配置文件:

vim /etc/gitlab/gitlab.rb

修改:

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

生效:

gitlab-ctl reconfigure

2. 配置 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

生效:

gitlab-ctl reconfigure

测试邮件:

gitlab-rails console
Notify.test_email('test@example.com', 'Test', 'Hello').deliver_now

3. 修改 GitLab 数据目录(可选)

git_data_dirs({
  "default" => {
    "path" => "/data/gitlab/git-data"
  }
})

七、常用管理命令

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

八、汉化(可选)

方法一:官方支持(推荐)

GitLab 自带中文:

右上角头像 → Preferences → Localization → 简体中文

方法二:第三方汉化(不推荐)

存在兼容风险,不建议生产环境使用。


九、常见问题

1. 访问 502

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

2. 忘记 root 密码

gitlab-rails console
user = User.find_by(username: 'root')
user.password = 'NewPassword123'
user.password_confirmation = 'NewPassword123'
user.save!

十、生产环境建议

✅ 使用 HTTPS
✅ 定期备份
✅ 至少 8GB 内存
✅ 使用独立服务器,不要和数据库混用


如果你愿意,我可以帮你:

  • 写一份 CentOS 7/8 一键安装脚本
  • 配置 HTTPS(Let’s Encrypt)
  • 对接 LDAP / 钉钉 / Jenkins
  • 迁移 GitLab 到新服务器

直接告诉我你的 CentOS 版本 + 是否生产环境即可。

0