优化 Debian 上的 GitLab,通常可以从 系统资源、GitLab 配置、数据库、存储、网络和安全 等方面入手。下面按“由浅入深”的方式给你一套实用优化方案(适用于 GitLab CE/EE,社区版和企业版通用)。
GitLab 比较吃资源,建议最低配置:
| 项目 | 建议 |
|---|---|
| CPU | ≥ 4 核 |
| 内存 | ≥ 8GB(推荐 16GB) |
| 磁盘 | SSD(优先 NVMe) |
| 系统 | Debian 11 / 12(64 位) |
确认内存是否足够:
free -h
如果内存 < 16GB,建议开启 swap:
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
GitLab 会大量使用文件句柄:
ulimit -n
建议设置为:
65535
编辑:
nano /etc/security/limits.conf
添加:
* soft nofile 65535
* hard nofile 65535
配置文件路径:
/etc/gitlab/gitlab.rb
修改后一定要执行:
gitlab-ctl reconfigure
gitlab-ctl restart
如果你只是用 Git + CI,不需要全部功能,可以关闭:
# 禁用一些组件
gitlab_rails['gitlab_email_enabled'] = false
prometheus_monitoring['enable'] = false
grafana['enable'] = false
如果你的机器资源较少,可以减少 Sidekiq 并发:
sidekiq['max_concurrency'] = 10
GitLab 自带自动调优:
postgresql['autotune'] = true
postgresql['shared_buffers'] = "2GB"
postgresql['work_mem'] = "16MB"
postgresql['maintenance_work_mem'] = "256MB"
postgresql['effective_cache_size'] = "4GB"
GitLab 15+ 默认使用 Puma
puma['worker_processes'] = 2
puma['min_threads'] = 1
puma['max_threads'] = 2
公式参考:
worker_processes ≈ (CPU 核心数 * 1.5)
GitLab 对磁盘 IO 要求高,尤其是:
查看磁盘类型:
lsblk -d -o name,rota
rota=1 是机械盘(不推荐)
限制仓库大小(防止膨胀):
gitlab_rails['git_max_size'] = 209715200 # 200MB
定期清理:
gitlab-rake gitlab:cleanup:project_uploads
或设置自动过期:
gitlab_rails['artifacts_lifetime'] = '7d'
GitLab 自带 Nginx,可微调:
nginx['worker_processes'] = 2
nginx['worker_connections'] = 1024
nginx['enable_gzip'] = true
letsencrypt['enable'] = true
gitlab-ctl top
htop
iotop
gitlab-ctl tail
gitlab-ctl tail nginx
gitlab-ctl tail sidekiq
如果你的服务器资源很少(< 8GB 内存),可以考虑:
puma['worker_processes'] = 2
sidekiq['max_concurrency'] = 10
postgresql['shared_buffers'] = "2GB"
prometheus_monitoring['enable'] = false
grafana['enable'] = false
我可以给你 更精确的优化方案,只需要你提供:
gitlab-rake gitlab:env:info)我可以直接帮你 生成一份最优 gitlab.rb 配置。