温馨提示×

怎么配置linux的gitlab

小樊
39
2026-10-04 11:56:48
栏目: 智能运维

下面按最常见场景给你讲:在 Linux 服务器上部署 GitLab(自建 GitLab 服务),而不是“配置 Git 客户端连接 GitLab”。
如果你只是想用 git 连接 GitLab,可以跳到最后一节。


一、Linux 上安装 GitLab(以 Ubuntu / CentOS 为例)

1️⃣ 系统要求

  • 内存:至少 4GB(推荐 8GB)
  • 磁盘:20GB+
  • 系统:Ubuntu 20.04/22.04 或 CentOS 7/8

二、Ubuntu 安装 GitLab(官方推荐方式)

1️⃣ 安装依赖

sudo apt update
sudo apt install -y curl openssh-server ca-certificates postfix

postfix 选 Internet Site 即可


2️⃣ 添加 GitLab 源

curl -fsSL https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash

3️⃣ 安装 GitLab

sudo EXTERNAL_URL="http://gitlab.example.com" apt install gitlab-ee

✅ 如果是本机测试:

sudo EXTERNAL_URL="http://localhost" apt install gitlab-ee

4️⃣ 初始化配置

sudo gitlab-ctl reconfigure

5️⃣ 启动并访问

sudo gitlab-ctl start

浏览器访问:

http://服务器IP

首次登录账号:

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

三、CentOS 安装 GitLab

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

curl -fsSL https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
sudo EXTERNAL_URL="http://gitlab.example.com" yum install gitlab-ee
sudo gitlab-ctl reconfigure

四、常见配置(/etc/gitlab/gitlab.rb)

修改访问地址

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

修改端口

external_url 'http://192.168.1.100:8080'

改完执行:

sudo 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'] = "xxx@qq.com"
gitlab_rails['smtp_password'] = "授权码"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true

五、常用管理命令

gitlab-ctl status
gitlab-ctl stop
gitlab-ctl start
gitlab-ctl restart
gitlab-ctl tail

六、如果你只是“配置 Git 连接 GitLab”

1️⃣ 安装 git

sudo apt install git

2️⃣ 配置用户信息

git config --global user.name "yourname"
git config --global user.email "you@example.com"

3️⃣ 生成 SSH 密钥

ssh-keygen -t ed25519
cat ~/.ssh/id_ed25519.pub

复制到 GitLab → User Settings → SSH Keys

4️⃣ 克隆项目

git clone git@gitlab.com:username/project.git

七、常见问题

❓ 内存不足

  • 关闭一些服务或加 swap

❓ 访问慢

  • 禁用 Prometheus:
prometheus['enable'] = false

❓ 忘记 root 密码

sudo gitlab-rake "gitlab:password:reset[root]"

如果你告诉我:

  • 用的是 Ubuntu 还是 CentOS
  • 是 本机测试还是服务器
  • 想要 HTTPS / 域名 / Docker 版

我可以直接给你一份精确配置方案。

0 踩