温馨提示×

如何安装CentOS GitLab

小樊
46
2026-07-20 03:39:02
栏目: 智能运维

下面给你一份**在 CentOS 上安装 GitLab(CE 社区版)**的完整、常用做法,适用于 CentOS 7 / CentOS 8 Stream。如果你用的是其他版本,可以再告诉我。


一、系统要求(建议)

  • 内存:≥ 4GB(2GB 也能跑,但很卡)
  • CPU:2 核以上
  • 磁盘:至少 20GB
  • 系统:CentOS 7 / 8

二、安装前准备

1️⃣ 更新系统

sudo yum update -y

2️⃣ 安装依赖

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

3️⃣ 启动 SSH 和 Postfix

sudo systemctl enable sshd
sudo systemctl start sshd

sudo systemctl enable postfix
sudo systemctl start postfix

三、安装 GitLab CE

方式一(推荐):使用官方仓库

1️⃣ 添加 GitLab 仓库

curl -fsSL https://packages.gitlab.com/setup.sh | sudo bash

国内服务器如果慢,可先配置代理或使用清华镜像(下面会讲)

2️⃣ 安装 GitLab

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

注意

  • EXTERNAL_URL 填你的 服务器 IP 或域名
  • 例如:
sudo EXTERNAL_URL="http://192.168.1.100" yum install -y gitlab-ce

方式二:手动下载 RPM(适合离线)

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-16.7.0-ce.0.el7.x86_64.rpm
sudo rpm -ivh gitlab-ce-*.rpm

四、初始化 GitLab

sudo gitlab-ctl reconfigure

⏳ 第一次会耗时 2–5 分钟


五、访问 GitLab

浏览器访问:

http://服务器IP

首次登录:

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

⚠️ 密码 24 小时后会被自动删除


六、常用管理命令

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

七、防火墙配置(如果无法访问)

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

八、GitLab 配置优化(可选)

编辑配置文件:

sudo vi /etc/gitlab/gitlab.rb

常用配置:

external_url 'http://gitlab.example.com'
gitlab_rails['time_zone'] = 'Asia/Shanghai'

修改后执行:

sudo gitlab-ctl reconfigure

九、国内加速(非常重要)

使用清华镜像

curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/ | cat

或在 /etc/gitlab/gitlab.rb 中:

gitlab_rails['registry_mirror'] = "https://mirrors.tuna.tsinghua.edu.cn"

十、常见问题

❓ 页面 502

  • 内存不足
  • 端口被占用
  • 执行:
gitlab-ctl restart

❓ 中文界面

登录后 → Preferences → Localization → 简体中文


如果你告诉我:

  • ✅ CentOS 版本
  • ✅ 是否用域名
  • ✅ 是否要 HTTPS
  • ✅ 是否内网使用

我可以给你定制版安装方案(比如 Docker / HTTPS / 企业级优化)

0