GitLab教程(2): 在Linux系统上安装GitLab CE
本文将详细介绍如何在Linux系统上安装GitLab Community Edition(社区版),包括多种安装方式和完整的配置步骤。
系统要求
# 最低配置
CPU: 4核
内存: 4GB (推荐8GB+)
存储: 10GB (推荐50GB+)
# 支持的操作系统
- Ubuntu 20.04/22.04/24.04 LTS
- Debian 11/12
- CentOS 7/Stream 8/Stream 9
- Rocky Linux 8/9
- AlmaLinux 8/9
- RHEL 7/8/9
- Amazon Linux 2
# 检查系统配置
cat /etc/os-release
free -h
df -h
nproc
使用官方包安装 (推荐)
Ubuntu/Debian
# 1. 安装依赖
sudo apt-get update
sudo apt-get install -y curl openssh-server ca-certificates tzdata perl
# 2. 添加GitLab仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
# 输出示例
Detected operating system as Ubuntu/jammy.
Checking for curl...
Detected curl...
Checking for gpg...
Detected gpg...
Running apt-get update...
Installing apt-transport-https...
Installing gitlab-ce repository...
# 3. 安装GitLab CE
# 设置访问URL (替换为你的域名或IP)
sudo EXTERNAL_URL="http://gitlab.example.com" apt-get install gitlab-ce
# 安装过程输出
Selecting previously unselected package gitlab-ce.
Preparing to unpack .../gitlab-ce_16.8.1-ce.0_amd64.deb ...
Unpacking gitlab-ce (16.8.1-ce.0) ...
Setting up gitlab-ce (16.8.1-ce.0) ...
*. *.
*** ***
***** *****
.****** *******
******** ********
,,,,,,,,,***********,,,,,,,,,
,,,,,,,,,,,*********,,,,,,,,,,,
.,,,,,,,,,,,*******,,,,,,,,,,,,
,,,,,,,,,*****,,,,,,,,,.
,,,,,,,****,,,,,,
.,,,***,,,,
,*,.
_______ __ __ __
/ ____(_) /_/ / ____ _/ /_
/ / __/ / __/ / / __ `/ __ \
/ /_/ / / /_/ /___/ /_/ / /_/ /
\____/_/\__/_____/\__,_/_.___/
Thank you for installing GitLab!
# 4. 获取初始root密码
sudo cat /etc/gitlab/initial_root_password
# 输出示例
Password: xHk9f2YbPq3mNrTzWvQa
# 注意:此密码文件将在首次配置后24小时自动删除
CentOS/RHEL
# 1. 安装依赖
sudo yum install -y curl policycoreutils-python-utils openssh-server perl
# 启用SSH
sudo systemctl enable sshd
sudo systemctl start sshd
# 配置防火墙
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
# 2. 添加GitLab仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
# 3. 安装GitLab CE
sudo EXTERNAL_URL="http://gitlab.example.com" yum install -y gitlab-ce
# 4. 获取初始密码
sudo cat /etc/gitlab/initial_root_password
使用Docker安装
# 1. 创建数据目录
sudo mkdir -p /srv/gitlab/{config,logs,data}
# 2. 设置环境变量
export GITLAB_HOME=/srv/gitlab
# 3. 运行GitLab容器
docker run -d \
--hostname gitlab.example.com \
--name gitlab \
--restart always \
-p 443:443 \
-p 80:80 \
-p 22:22 \
-v $GITLAB_HOME/config:/etc/gitlab \
-v $GITLAB_HOME/logs:/var/log/gitlab \
-v $GITLAB_HOME/data:/var/opt/gitlab \
--shm-size 256m \
gitlab/gitlab-ce:latest
# 4. 查看容器状态
docker ps
# 输出示例
CONTAINER ID IMAGE STATUS PORTS
a1b2c3d4e5f6 gitlab/gitlab-ce:latest Up 5 minutes (healthy) 0.0.0.0:22->22/tcp, 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp
# 5. 查看初始密码
docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
# 输出示例
Password: abc123XYZ789
# 6. 查看日志
docker logs -f gitlab
使用Docker Compose安装
# docker-compose.yml
version: '3.8'
services:
gitlab:
image: gitlab/gitlab-ce:latest
container_name: gitlab
restart: always
hostname: gitlab.example.com
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'https://gitlab.example.com'
gitlab_rails['gitlab_shell_ssh_port'] = 2222
# 配置SMTP
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.example.com"
gitlab_rails['smtp_port'] = 587
gitlab_rails['smtp_user_name'] = "gitlab@example.com"
gitlab_rails['smtp_password'] = "your-password"
gitlab_rails['smtp_domain'] = "example.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
# 配置时区
gitlab_rails['time_zone'] = 'Asia/Shanghai'
ports:
- "80:80"
- "443:443"
- "2222:22"
volumes:
- ./config:/etc/gitlab
- ./logs:/var/log/gitlab
- ./data:/var/opt/gitlab
shm_size: '256m'
# 启动
docker-compose up -d
# 查看日志
docker-compose logs -f
# 获取初始密码
docker-compose exec gitlab cat /etc/gitlab/initial_root_password
基本配置
修改配置文件
# 编辑配置文件
sudo vim /etc/gitlab/gitlab.rb
# 常用配置项
# 外部访问URL
external_url 'https://gitlab.example.com'
# 时区设置
gitlab_rails['time_zone'] = 'Asia/Shanghai'
# 邮件配置
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.example.com"
gitlab_rails['smtp_port'] = 587
gitlab_rails['smtp_user_name'] = "gitlab@example.com"
gitlab_rails['smtp_password'] = "your-password"
gitlab_rails['smtp_domain'] = "example.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['gitlab_email_from'] = 'gitlab@example.com'
# 禁用注册(生产环境)
gitlab_rails['gitlab_signup_enabled'] = false
# 应用配置
sudo gitlab-ctl reconfigure
# 输出示例
Running handlers:
Running handlers complete
Chef Infra Client finished, 0/726 resources updated in 01 minutes 23 seconds
Notes:
Default admin account has been configured with following details:
Username: root
Password: You didn't opt-in to print initial root password to STDOUT.
gitlab Reconfigured!
配置HTTPS
# 方式1:使用Let's Encrypt自动获取证书
# /etc/gitlab/gitlab.rb
external_url 'https://gitlab.example.com'
letsencrypt['enable'] = true
letsencrypt['contact_emails'] = ['admin@example.com']
sudo gitlab-ctl reconfigure
# 方式2:使用自有证书
# /etc/gitlab/gitlab.rb
external_url 'https://gitlab.example.com'
nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.example.com.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.example.com.key"
# 创建证书目录
sudo mkdir -p /etc/gitlab/ssl
# 复制证书文件
sudo cp your-cert.crt /etc/gitlab/ssl/gitlab.example.com.crt
sudo cp your-key.key /etc/gitlab/ssl/gitlab.example.com.key
sudo chmod 600 /etc/gitlab/ssl/*
sudo gitlab-ctl reconfigure
GitLab管理命令
# 服务管理
sudo gitlab-ctl status # 查看状态
sudo gitlab-ctl start # 启动
sudo gitlab-ctl stop # 停止
sudo gitlab-ctl restart # 重启
sudo gitlab-ctl reconfigure # 应用配置变更
# 状态输出示例
run: alertmanager: (pid 12345) 3600s
run: gitaly: (pid 12346) 3600s
run: gitlab-exporter: (pid 12347) 3600s
run: gitlab-kas: (pid 12348) 3600s
run: gitlab-workhorse: (pid 12349) 3600s
run: logrotate: (pid 12350) 3600s
run: nginx: (pid 12351) 3600s
run: puma: (pid 12352) 3600s
run: redis: (pid 12353) 3600s
run: sidekiq: (pid 12354) 3600s
# 查看日志
sudo gitlab-ctl tail # 所有日志
sudo gitlab-ctl tail nginx # Nginx日志
sudo gitlab-ctl tail puma # 应用日志
sudo gitlab-ctl tail sidekiq # 后台任务日志
# 健康检查
sudo gitlab-rake gitlab:check
# 输出示例
Checking GitLab subtasks ...
Checking GitLab Shell ...
GitLab Shell: ... GitLab Shell version >= 14.31.0 ? ... OK (14.31.0)
...
Checking Sidekiq ...
Sidekiq: ... Running? ... yes
...
Checking GitLab App ...
Git configured correctly? ... yes
...
Checking GitLab subtasks ... Finished
首次登录
# 1. 访问GitLab
# 浏览器打开: https://gitlab.example.com
# 2. 使用root账户登录
用户名: root
密码: (从initial_root_password获取)
# 3. 修改root密码
# 登录后会提示修改密码
# 或者通过命令行修改:
sudo gitlab-rake "gitlab:password:reset[root]"
# 输出示例
Enter password:
Confirm password:
Password successfully updated for user with username root.
# 4. 创建新用户
# Admin Area > Users > New user
# 或命令行:
sudo gitlab-rails console
> user = User.new(username: 'developer', email: 'dev@example.com', name: 'Developer', password: 'password123', password_confirmation: 'password123')
> user.skip_confirmation!
> user.save!
> exit
常见问题
# 问题1: 502错误
# 原因: 服务还在启动中,通常需要3-5分钟
# 解决: 等待几分钟后刷新
# 问题2: 内存不足
# 查看内存使用
free -h
# 调整Puma worker数量
# /etc/gitlab/gitlab.rb
puma['worker_processes'] = 2
# 问题3: 端口被占用
sudo lsof -i :80
sudo lsof -i :22
# 修改GitLab端口
# /etc/gitlab/gitlab.rb
nginx['listen_port'] = 8080
gitlab_rails['gitlab_shell_ssh_port'] = 2222
# 问题4: 忘记root密码
sudo gitlab-rake "gitlab:password:reset[root]"
总结
本文介绍了在Linux上安装GitLab CE的多种方式,包括官方包安装、Docker安装和Docker Compose安装。安装后需要进行基本配置并确保服务正常运行。
下一篇我们将学习GitLab的用户和组管理。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。







