温馨提示×

linux gitlab怎么安装配置

小樊
44
2026-09-01 13:23:31
栏目: 智能运维

下面给你一份**在 Linux 上安装和配置 GitLab(CE 社区版)**的常用做法,以 Ubuntu / DebianCentOS / RHEL 为例,并包含基本配置和常见问题。


一、准备工作

1. 系统要求

  • 内存:至少 4 GB(推荐 4G+,2G 容易卡)
  • CPU:2 核以上
  • 磁盘:20 GB 以上
  • 系统:
    • Ubuntu 20.04 / 22.04
    • CentOS 7 / 8(CentOS 8 已 EOL,建议 Rocky/Alma)

2. 关闭防火墙或开放端口

# 查看端口
sudo netstat -tunlp | grep 80

二、Ubuntu / Debian 安装 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-ce/setup.sh | sudo bash

3. 安装 GitLab

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

如果是本机测试:

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

4. 启动并配置

sudo gitlab-ctl reconfigure
sudo gitlab-ctl status

访问:

http://服务器IP

首次登录账号:

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

三、CentOS / RHEL 安装 GitLab

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/setup.sh | sudo bash

3. 安装 GitLab

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

4. 初始化

sudo gitlab-ctl reconfigure

四、基础配置(重要)

1. 修改配置文件

sudo vim /etc/gitlab/gitlab.rb

常见配置:

external_url 'http://192.168.1.100'

# 修改端口(可选)
# external_url 'http://192.168.1.100:8080'

# 邮箱(可选)
gitlab_rails['smtp_enable'] = true

修改后执行:

sudo gitlab-ctl reconfigure

2. 常用命令

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

五、配置 HTTPS(可选但推荐)

使用自带证书(测试)

external_url 'https://gitlab.example.com'
nginx['redirect_http_to_https'] = true

使用 Let’s Encrypt

letsencrypt['enable'] = true
letsencrypt['contact_emails'] = ['admin@example.com']

然后:

sudo gitlab-ctl reconfigure

六、常见问题

1. 访问很慢

  • 内存不足
  • 可关闭部分服务(如 Mattermost)
mattermost['enable'] = false

2. 端口冲突

修改:

nginx['listen_port'] = 8080

3. 忘记 root 密码

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

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

如果你只是想用 Git + Web:

  • Gitea(更轻)
  • GitLab Runner(CI/CD)

如果你愿意,可以告诉我:

  • 使用的 Linux 发行版
  • 是否 内网 / 公网
  • 是否要 Docker 安装

我可以直接给你一套精准命令

0