在CentOS上安装GitLab前,需先安装必要依赖并优化系统环境,确保后续安装顺利:
yum安装curl、policycoreutils、openssh-server、postfix等工具(postfix用于GitLab发送通知邮件)。/etc/sysctl.conf,添加net.core.somaxconn = 65535、net.ipv4.tcp_tw_reuse = 1等参数,提升系统并发处理能力;运行sysctl -p使配置生效。dd、mkswap、swapon命令创建Swap分区,缓解内存压力。curl命令添加GitLab官方YUM源(社区版gitlab-ce或企业版gitlab-ee),确保软件包来源可靠。yum install gitlab-ce命令安装,安装完成后会自动启动服务。/etc/gitlab/gitlab.rb文件,设置external_url为服务器IP或域名(如http://192.168.1.100),此配置决定了GitLab的访问入口。sudo gitlab-ctl reconfigure使配置生效,启动GitLab相关服务(如Nginx、PostgreSQL、Redis)。git init;git remote add origin git@gitlab.example.com:username/project.git(替换为实际URL);git add . → git commit -m "Initial commit" → git push -u origin master。git checkout -b new-branch创建新分支,git checkout branch-name切换分支,git merge branch-name合并分支(推荐使用Git Flow或GitLab Flow规范流程)。.gitlab-ci.yml文件:在项目根目录下创建该文件,定义CI/CD流程(如构建、测试、部署)。示例:stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the project..."
- mvn package
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
.gitlab-ci.yml中的任务(支持Docker、Shell等执行器)。/etc/gitlab/gitlab.rb,调整Unicorn worker数量(unicorn['worker_processes'] = 4,根据CPU核心数调整)、启用HTTP/2(nginx['enable'] = true)、设置页面缓存(gitlab_rails['cache_store'] = :memory_store)。sidekiq['concurrency'] = 25),提升后台任务处理效率。net.ipv4.tcp_max_syn_backlog = 65535)、启用Keep-Alive(nginx['keepalive_timeout'] = 65),减少网络延迟。external_url为https://,并在/etc/gitlab/gitlab.rb中设置证书路径(nginx['ssl_certificate_file']、nginx['ssl_certificate_key_file']),提升数据传输安全性。PasswordAuthentication no),仅允许SSH密钥登录(ssh-keygen -t rsa -b 4096 -C "your_email@example.com",将公钥添加到GitLab账户)。sudo gitlab-rake gitlab:backup:create,默认备份路径为/var/opt/gitlab/backups),恢复时使用sudo gitlab-rake gitlab:backup:restore BACKUP=2025-01-01-12-00-00(替换为实际备份文件名)。top、htop、vmstat等工具监控CPU、内存、磁盘使用情况;或部署Prometheus+Grafana监控套件,实时查看GitLab性能指标(如请求响应时间、数据库负载)。/var/log/gitlab目录,定期清理过期日志(如logrotate工具),避免日志文件占用过多存储空间。sudo gitlab-ctl update命令升级,确保兼容性。