温馨提示×

linux gitlab怎样搭建

小樊
36
2026-10-25 01:58:25
栏目: 智能运维

下面给你一份**在 Linux 上搭建 GitLab(社区版 CE)**的实用指南,以 Ubuntu / Debian 和 CentOS / RHEL 为例,适合自建服务器或云主机。


一、准备工作

1. 服务器要求

  • 系统:Ubuntu 20.04/22.04 或 CentOS 7/8
  • 内存:≥ 4GB(推荐 4–8GB)
  • CPU:≥ 2 核
  • 磁盘:≥ 20GB
  • 已配置 域名(可选但推荐,如 git.example.com)

二、Ubuntu / Debian 安装 GitLab CE

1. 安装依赖

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

postfix 可选(用于邮件),也可用 SMTP


2. 添加 GitLab 源

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

3. 安装 GitLab

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

如果是 IP 访问:

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

4. 启动并访问

sudo gitlab-ctl reconfigure

浏览器访问:

http://git.example.com

首次登录账号:

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

三、CentOS / RHEL 安装 GitLab CE

1. 安装依赖

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

2. 添加 GitLab 源

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

3. 安装 GitLab

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

4. 初始化

sudo gitlab-ctl reconfigure

四、常用管理命令

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

五、基础配置(重要)

1. 修改配置文件

sudo vim /etc/gitlab/gitlab.rb

常见配置:

external_url 'http://git.example.com'
gitlab_rails['gitlab_email_from'] = 'gitlab@example.com'

修改后执行:

sudo gitlab-ctl reconfigure

2. 开启 HTTPS(推荐)

使用 Let’s Encrypt:

letsencrypt['enable'] = true
letsencrypt['auto_renew'] = true

六、常见问题

1. 访问慢 / 502

  • 内存不足
  • 未执行 gitlab-ctl reconfigure

2. 端口冲突

修改:

nginx['listen_port'] = 8080

3. 忘记 root 密码

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

七、轻量替代方案(可选)

如果你只是想要 Git 服务:

  • Gitea(更轻)
  • GitLab Runner(CI/CD 单独装)

如果你告诉我:

  • 使用的 Linux 发行版
  • 是否要 HTTPS / 域名 / CI

我可以给你一份定制化搭建方案。

0 踩