温馨提示×

gitlab linux怎样搭建

小樊
43
2026-09-19 14:53:50
栏目: 智能运维

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


一、准备工作

1. 服务器要求

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

2. 防火墙 & 域名(可选)

  • 开放端口:22(SSH)、80(HTTP)、443(HTTPS)
  • 有域名更好(如 git.example.com)

二、安装 GitLab(以 Ubuntu 为例)

1. 安装依赖

sudo apt update
sudo apt install -y curl openssh-server ca-certificates tzdata perl

2. 添加 GitLab 源

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

3. 安装 GitLab CE

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

如果是本地测试:

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

三、基础配置

1. 修改配置文件

sudo vim /etc/gitlab/gitlab.rb

常见配置:

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

保存后执行:

sudo gitlab-ctl reconfigure

2. 启动 GitLab

sudo gitlab-ctl start

查看状态:

sudo gitlab-ctl status

四、初次访问

浏览器访问:

http://服务器IP

第一次登录:

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

五、常用命令

gitlab-ctl start      # 启动
gitlab-ctl stop       # 停止
gitlab-ctl restart    # 重启
gitlab-ctl status     # 状态
gitlab-ctl reconfigure # 应用配置

日志查看:

sudo gitlab-ctl tail

六、CentOS 安装方式(简要)

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

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

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

七、常见问题

1. 访问很慢

  • 内存不足
  • 可关闭部分服务(如 Mattermost)

2. 端口冲突

修改:

nginx['listen_port'] = 8080

3. 忘记 root 密码

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

八、推荐生产方案

  • 使用 HTTPS + 反向代理(Nginx)
  • 定期备份:
sudo gitlab-rake gitlab:backup:create

如果你告诉我:

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

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

0 踩