温馨提示×

怎样在centos用gitlab

小樊
35
2026-09-11 19:09:06
栏目: 智能运维

CentOS 上使用 GitLab,通常有两种常见方式:

  1. 使用 GitLab(作为服务):在 CentOS 上安装并运行 GitLab(自建 GitLab 服务器)
  2. 在 CentOS 上使用 GitLab(作为客户端):只是用 git 命令连接 GitLab 仓库

下面我分别说明。


一、在 CentOS 上使用 GitLab(作为客户端)

如果你只是想 拉代码、推代码,不需要安装 GitLab,只需安装 Git。

1. 安装 Git

sudo yum install -y git

2. 配置 Git

git config --global user.name "你的名字"
git config --global user.email "你的邮箱"

3. 克隆 GitLab 仓库

git clone https://gitlab.com/用户名/项目名.git

或 SSH 方式(推荐):

git clone git@gitlab.com:用户名/项目名.git

4. 配置 SSH(可选但推荐)

ssh-keygen -t rsa -b 4096 -C "你的邮箱"
cat ~/.ssh/id_rsa.pub

把公钥添加到 GitLab → Settings → SSH Keys


二、在 CentOS 上安装 GitLab(自建服务器)

⚠️ 建议:

  • 内存 ≥ 4GB
  • CentOS 7 或 8(CentOS 8 已停止维护,建议 Rocky/Alma)

方法一:使用官方 Omnibus 安装(最简单)

1. 安装依赖

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

2. 安装 Postfix(邮件,可选)

sudo yum install -y postfix
sudo systemctl enable postfix
sudo systemctl start postfix

3. 添加 GitLab 源

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

4. 安装 GitLab

sudo EXTERNAL_URL="http://你的服务器IP" yum install -y gitlab-ee

5. 访问 GitLab

浏览器打开:

http://服务器IP

首次登录设置 root 密码


三、常用管理命令

# 查看状态
sudo gitlab-ctl status

# 重启
sudo gitlab-ctl restart

# 查看日志
sudo gitlab-ctl tail

四、常见问题

1. 端口冲突(如 80 被占用)

修改:

sudo vi /etc/gitlab/gitlab.rb
external_url 'http://IP:端口'

然后:

sudo gitlab-ctl reconfigure

2. 内存不足

可关闭部分服务(如 Prometheus)


五、推荐方案(新手)

  • 学习 / 测试:直接用 gitlab.com
  • 公司内网:用 Docker 版 GitLab
  • 生产环境:Omnibus + Nginx + HTTPS

如果你告诉我:

  • CentOS 版本
  • 是「用 GitLab」还是「搭 GitLab」
  • 是否有公网 IP

我可以给你更精确的命令

0