Debian GitLab故障排查方法
日志是定位故障的核心依据。GitLab的日志集中存储在/var/log/gitlab目录下,关键日志包括:
/var/log/gitlab/gitlab-rails/production.log(记录Web请求、数据库操作等);/var/log/gitlab/nginx/error.log(记录HTTP请求错误);/var/log/gitlab/postgresql/postgresql-<version>-main.log(记录数据库错误)。tail -f命令实时跟踪日志,快速定位错误信息(如502错误可能显示“upstream prematurely closed connection”)。通过gitlab-ctl status命令查看GitLab各组件(如nginx、postgresql、unicorn)的运行状态。若组件未运行(显示“down”),可使用gitlab-ctl restart重启服务;若重启失败,需结合日志进一步分析原因(如端口冲突、配置错误)。
GitLab的主配置文件为/etc/gitlab/gitlab.rb,需重点检查以下内容:
external_url:确保设置为正确的访问地址(如http://your_server_ip或带域名的URL);postgresql['host']、postgresql['port']、gitlab_rails['db_password']等参数是否与实际数据库设置一致;nginx['listen_port'](默认80)、ssh_port(默认22)未被系统其他服务占用。sudo gitlab-ctl reconfigure重新加载配置,否则更改不会生效。GitLab对资源要求较高,需使用top、htop或free -h命令监控系统资源:
sudo fallocate -l 2G /swapfile等命令)缓解;gitlab-ctl tail查看具体进程;df -h检查根分区(/)和GitLab数据目录(/var/opt/gitlab)的剩余空间,剩余空间不足(<10%)会导致服务异常。ping命令测试服务器是否能访问外部网络(如ping google.com);sudo ufw status查看防火墙规则,确保允许GitLab所需端口(默认80、443、22)通过(如sudo ufw allow 80/tcp);netstat -tulnp | grep <port>(如netstat -tulnp | grep 80)检查端口是否被其他服务(如Apache)占用,若有冲突需停止冲突服务或修改GitLab端口。gitlab-ctl status确认unicorn服务运行正常;sudo gitlab-ctl tail查看unicorn日志是否有连接错误;unicorn['worker_processes']参数(如设置为CPU核心数的1-2倍),避免进程过多导致资源耗尽。sudo gitlab-rake "gitlab:password:reset[root]"命令重置root密码。gitlab-rake gitlab:check SANITIZE=true命令测试数据库连接,若提示连接错误,需检查:
sudo systemctl status postgresql);pg_hba.conf文件是否允许GitLab服务器IP访问(添加host all all 0.0.0.0/0 md5);gitlab.rb中的数据库密码是否正确。旧版本GitLab可能存在已知bug,需定期更新:
sudo apt-get update
sudo apt-get install gitlab-ce
更新前需备份数据(sudo gitlab-backup create),避免数据丢失。
GitLab依赖多项服务(如curl、openssh-server、ca-certificates、postgresql),需确保这些依赖项已正确安装且版本兼容:
sudo apt-get install -y curl openssh-server ca-certificates postgresql postgresql-contrib
若依赖项缺失,可使用apt自动修复(sudo apt --fix-broken install)。