上一篇
如何快速高效将GitLab配置到服务器?详细步骤解析!
- 行业动态
- 2025-04-21
- 5
在服务器部署GitLab需先安装依赖包,下载官方仓库并配置安装,通过修改配置文件绑定域名与端口,开放防火墙规则,设置SSL证书实现HTTPS访问,初始化完成后,通过网页端完成管理员账号设置及仓库管理,即可实现本地化代码托管与协作开发。
准备工作
服务器要求
- 操作系统:推荐使用 Ubuntu 20.04/22.04 或 CentOS 7/8。
- 内存:建议至少 4GB(GitLab 官方最低要求为 4GB,实际需根据项目规模调整)。
- 存储:预留 20GB 以上磁盘空间,用于代码存储和运行日志。
- 网络:开放 HTTP/HTTPS 端口(80/443)及 SSH 端口(22)。
环境依赖
- 安装
curl
和openssh-server
:sudo apt update && sudo apt install -y curl openssh-server
- 安装
安装 GitLab
添加 GitLab 官方仓库
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
执行安装命令
- Ubuntu/Debian:
sudo EXTERNAL_URL="http://你的域名或IP" apt install gitlab-ce
- CentOS/RHEL:
sudo EXTERNAL_URL="http://你的域名或IP" yum install gitlab-ce
- Ubuntu/Debian:
验证安装
运行sudo gitlab-ctl status
,若显示所有服务为 run 状态,则安装成功。
基础配置
配置文件路径
修改/etc/gitlab/gitlab.rb
,核心参数如下:external_url 'http://你的域名或IP' # 替换为实际域名或IP gitlab_rails['time_zone'] = 'Asia/Shanghai' # 设置时区
初始化配置
sudo gitlab-ctl reconfigure
访问 GitLab
浏览器输入http://你的域名或IP
,首次访问需设置管理员密码(账户名:root
)。
高级配置(HTTPS 与防火墙)
启用 HTTPS
- 申请免费 SSL 证书(以 Let’s Encrypt 为例):
sudo apt install certbot sudo certbot certonly --standalone -d 你的域名
- 修改
gitlab.rb
:external_url 'https://你的域名' nginx['redirect_http_to_https'] = true nginx['ssl_certificate'] = "/etc/letsencrypt/live/你的域名/fullchain.pem" nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/你的域名/privkey.pem"
- 重新加载配置:
sudo gitlab-ctl reconfigure
- 申请免费 SSL 证书(以 Let’s Encrypt 为例):
配置防火墙
sudo ufw allow http sudo ufw allow https sudo ufw allow ssh sudo ufw enable
部署到网站
域名绑定
- 在域名服务商处添加 A 记录,将域名解析到服务器 IP。
- 若使用云服务器(如阿里云、AWS),需在安全组中开放 80/443 端口。
验证访问
- 浏览器输入域名,应显示 GitLab 登录页。
- 测试 SSH 克隆功能:
git clone git@你的域名:用户名/仓库名.git
维护与优化
定期备份
sudo gitlab-rake gitlab:backup:create
- 备份文件默认存储在
/var/opt/gitlab/backups
。
- 备份文件默认存储在
性能优化
- 调整 Sidekiq 线程数(根据 CPU 核心数):
sidekiq['max_concurrency'] = 10
- 启用缓存(需安装 Redis):
gitlab_rails['redis_enable'] = true
- 调整 Sidekiq 线程数(根据 CPU 核心数):
监控与日志
- 查看实时日志:
sudo gitlab-ctl tail
- 使用 Prometheus 监控资源占用(默认已集成)。
- 查看实时日志:
常见问题
502 错误
- 检查内存是否不足:运行
free -m
,若剩余内存低于 2GB,需升级服务器配置。 - 重启 GitLab:
sudo gitlab-ctl restart
。
- 检查内存是否不足:运行
无法推送代码
- 检查 SSH 密钥是否添加到 GitLab 账户。
- 验证防火墙是否开放 22 端口。
证书过期
- 更新 Let’s Encrypt 证书:
sudo certbot renew --dry-run
- 更新 Let’s Encrypt 证书:
引用说明
- GitLab 官方文档:https://docs.gitlab.com/ee/install/
- Let’s Encrypt 证书申请指南:https://letsencrypt.org/zh-cn/getting-started/