Linux下GitLab配置技巧汇总
安装GitLab前需确保系统具备必要依赖,避免后续配置出错。对于Ubuntu/Debian系统,执行sudo apt-get install -y curl openssh-server ca-certificates postfix;对于CentOS/RHEL系统,执行sudo yum install -y curl policycoreutils-python openssh-server postfix。其中postfix用于发送邮件通知(如代码推送、问题提醒),安装后可自动启动。
编辑GitLab主配置文件/etc/gitlab/gitlab.rb,设置external_url参数指定实例访问地址(如http://your-server-ip或https://your-domain.com)。该配置会影响GitLab的Web访问路径、API端点及邮件通知中的链接,需与服务器实际IP/域名一致。修改后需运行sudo gitlab-ctl reconfigure使配置生效。
为及时接收代码变更、问题跟踪等通知,需配置SMTP服务。在/etc/gitlab/gitlab.rb中添加以下参数(以Gmail为例):
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.gmail.com"
gitlab_rails['smtp_port'] = 587
gitlab_rails['smtp_user_name'] = "your-email@gmail.com"
gitlab_rails['smtp_password'] = "your-app-password" # 使用应用专用密码(非登录密码)
gitlab_rails['smtp_domain'] = "smtp.gmail.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false
修改后运行sudo gitlab-ctl reconfigure即可启用邮件功能。
为保障数据传输安全,建议配置SSL证书。可使用Let’s Encrypt获取免费证书,步骤如下:
sudo apt-get install certbot python-certbot-nginx(Ubuntu/Debian);sudo certbot certonly --standalone -d your-domain.com;/etc/gitlab/gitlab.rb中添加:nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/letsencrypt/live/your-domain.com/fullchain.pem"
nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/your-domain.com/privkey.pem"
sudo gitlab-ctl reconfigure && sudo gitlab-ctl restart。/var/opt/gitlab)挂载至SSD,显著提升克隆、推送速度;/etc/gitlab/gitlab.rb):postgresql['shared_buffers'] = "4GB"(根据服务器内存调整)、postgresql['max_connections'] = 100;redis['maxmemory'] = "2GB"限制内存使用,避免缓存占用过多资源。GitLab内置CI/CD功能,通过项目根目录的.gitlab-ci.yml文件定义自动化流程。例如,实现代码构建、测试、部署的流水线:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the project..."
- mvn clean package # 示例:Maven构建Java项目
test_job:
stage: test
script:
- echo "Running tests..."
- mvn test # 示例:运行单元测试
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- scp target/*.jar user@server:/app # 示例:部署到服务器
only:
- main # 仅main分支触发部署
配置完成后,GitLab Runner会自动执行流水线任务。
sudo ufw allow 22/tcp && sudo ufw allow 80/tcp && sudo ufw allow 443/tcp && sudo ufw enable;GitLab提供内置备份工具,定期备份数据可防止数据丢失。
sudo gitlab-rake gitlab:backup:create,备份文件默认保存在/var/opt/gitlab/backups目录,文件名格式为timestamp_gitlab_backup.tar;/var/opt/gitlab/backups目录,执行sudo gitlab-rake gitlab:backup:restore BACKUP=timestamp(替换为实际备份文件名),恢复后需运行sudo gitlab-ctl reconfigure。