温馨提示×

linux gitlab如何搭建

小樊
60
2026-09-07 13:02:43
栏目: 智能运维

下面给你一份**在 Linux 上搭建 GitLab(社区版 CE)**的实用指南,适用于 Ubuntu / Debian / CentOS / RHEL 常见发行版。


一、准备工作

1. 服务器建议配置

  • 最低:2 核 CPU、4GB 内存(少于 4G 很容易卡死)
  • 推荐:4 核 CPU、8GB 内存
  • 系统:Ubuntu 20.04 / 22.04、CentOS 7/8

2. 基础环境

# 更新系统
sudo apt update && sudo apt upgrade -y   # Ubuntu/Debian
# 或
sudo yum update -y                       # CentOS

二、安装 GitLab(推荐官方 Omnibus 包)

✅ Ubuntu / Debian

# 安装依赖
sudo apt install -y curl openssh-server ca-certificates postfix

# 添加 GitLab 源
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash

# 安装 GitLab CE
sudo EXTERNAL_URL="http://gitlab.example.com" apt install gitlab-ce

EXTERNAL_URL 改成你的域名或 IP,如:

sudo EXTERNAL_URL="http://192.168.1.100" apt install gitlab-ce

✅ CentOS / RHEL

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

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

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

三、初始化配置

sudo gitlab-ctl reconfigure

完成后访问:

http://你的IP或域名

首次登录:

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

四、常用管理命令

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

五、修改配置(如端口、域名)

编辑:

sudo vim /etc/gitlab/gitlab.rb

示例:

external_url 'http://gitlab.example.com'
nginx['listen_port'] = 8080

修改后:

sudo gitlab-ctl reconfigure

六、开启 HTTPS(可选但推荐)

使用 Let’s Encrypt

external_url 'https://gitlab.example.com'
letsencrypt['enable'] = true

然后:

sudo gitlab-ctl reconfigure

七、防火墙 & 端口

sudo ufw allow 80
sudo ufw allow 443

或 CentOS:

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

八、常见问题

1. 内存不够

  • 关闭部分服务(如 Mattermost)
  • 加 Swap

2. 访问慢

  • 禁用 Prometheus
prometheus_monitoring['enable'] = false

3. 忘记 root 密码

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

九、轻量替代方案(不合适 GitLab 时)

  • Gitea(资源占用极低)
  • Gogs
  • Forgejo

如果你告诉我:

  • 使用的 Linux 发行版
  • 是否 公网 / 内网
  • 是否要 HTTPS / 邮箱

我可以给你一份完全定制化的安装命令。

0 踩