以下是 GitLab Community Edition (gitlab-ce) 的安装教程,支持 Ubuntu、CentOS 和 Debian 系统:
1. 安装前准备
-
系统要求:确保服务器至少有 4GB 内存,推荐 4 核 CPU + 4GB 内存以上配置。
-
更新系统:
# Ubuntu/Debian sudo apt-get update && sudo apt-get upgrade -y # CentOS sudo yum update -y
-
安装依赖:
# Ubuntu/Debian sudo apt-get install -y curl openssh-server ca-certificates tzdata perl # CentOS sudo yum install -y curl openssh-server openssh-clients postfix cronie policycoreutils-python-utils sudo systemctl enable postfix && sudo systemctl start postfix
2. 添加 GitLab 官方仓库
-
一键添加仓库脚本:
# Ubuntu/Debian (选择你的系统版本,如 "focal" 对应 Ubuntu 20.04) curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash # CentOS (选择版本,如 "el7" 对应 CentOS 7) curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
-
国内镜像加速(可选)
替换为清华镜像源(适用于 Ubuntu/Debian):# 编辑 sources 文件 sudo nano /etc/apt/sources.list.d/gitlab_gitlab-ce.list # 将内容替换为: deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu <系统版本代号> main # 例如 Ubuntu 22.04 使用 "jammy"
3. 安装 GitLab-CE
-
执行安装命令:
# Ubuntu/Debian sudo apt-get install gitlab-ce # CentOS sudo yum install gitlab-ce
4. 配置 GitLab
-
编辑配置文件:
修改external_url
(替换为你的域名或IP):sudo nano /etc/gitlab/gitlab.rb # 示例: external_url 'http://your-domain.com' # 或 'http://<服务器IP>'
-
应用配置:
sudo gitlab-ctl reconfigure # 此过程可能需要几分钟
5. 访问 GitLab
-
开放防火墙端口(如未禁用防火墙):
# Ubuntu/Debian sudo ufw allow http sudo ufw allow https sudo ufw allow OpenSSH # CentOS sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
-
获取初始密码:
首次登录需使用以下文件中的密码:sudo cat /etc/gitlab/initial_root_password
访问
http://your-domain.com
或http://<服务器IP>
,使用用户名root
和初始密码登录。
6. 后续配置(可选)
-
配置 HTTPS:
修改external_url
为https://your-domain.com
,然后运行:sudo nano /etc/gitlab/gitlab.rb # 添加以下配置(使用 Let's Encrypt 自动证书): letsencrypt['enable'] = true letsencrypt['contact_emails'] = ['your-email@example.com'] sudo gitlab-ctl reconfigure
-
备份与恢复:
# 手动备份 sudo gitlab-rake gitlab:backup:create # 恢复备份 sudo gitlab-ctl stop sudo gitlab-rake gitlab:backup:restore BACKUP=备份文件名
-
启动 GitLab 服务:
sudo gitlab-ctl start
-
停止 GitLab 服务:
sudo gitlab-ctl stop
-
重启 GitLab 服务:
sudo gitlab-ctl restart
-
查看服务状态:
sudo gitlab-ctl status
-
重新加载配置(修改配置后使用):
sudo gitlab-ctl reconfigure
常见问题
-
502 错误:服务器资源不足,尝试增加内存或检查服务状态:
sudo gitlab-ctl status
-
无法访问:检查防火墙或安全组是否放行 80/443 端口。
-
修改管理员密码:
sudo gitlab-rails console user = User.find_by_username('root') user.password = '新密码' user.save! exit
通过以上步骤,你可以成功安装并运行 GitLab Community Edition。如需更多高级配置(如邮件通知、CI/CD 等),请参考 官方文档。