在CentOS上定制GitLab需通过安装配置基础环境→调整核心参数→扩展功能模块三个阶段完成,以下是详细步骤:
更新系统并安装依赖
运行sudo yum update -y更新系统;安装GitLab运行必需的依赖包(包括SSH服务、防火墙工具、邮件服务等):
sudo yum install -y curl policycoreutils-python-utils openssh-server postfix perl。
配置防火墙
允许HTTP(80端口)、HTTPS(443端口)流量通过防火墙,确保Web界面可访问:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
```。
添加GitLab官方仓库
通过curl命令添加GitLab的RPM镜像源,确保后续能获取最新版本:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash。
安装GitLab社区版(CE)
使用YUM包管理器安装GitLab CE(免费版),若需企业版(EE)可替换包名:
sudo yum install -y gitlab-ce。
修改外部访问URL
编辑配置文件/etc/gitlab/gitlab.rb,设置external_url为服务器IP或域名(如http://192.168.1.100或https://gitlab.example.com),这是访问GitLab的入口:
sudo vi /etc/gitlab/gitlab.rb → 找到external_url行并修改 → 保存退出。
应用配置并启动服务
运行sudo gitlab-ctl reconfigure使配置生效(会自动初始化数据库、设置目录权限等);启动GitLab服务并设置开机自启:
sudo gitlab-ctl start
sudo systemctl enable gitlab-runsvdir.service
```。
访问GitLab界面
在浏览器中输入external_url设置的地址(如http://192.168.1.100),首次访问需设置管理员账户(root)的密码,完成初始配置。
自定义Web界面
/etc/gitlab/gitlab.rb中通过gitlab_rails['custom_theme']或gitlab_rails['custom_css']指定自定义样式文件路径(需提前将文件上传至服务器);nginx['listen_port'](如改为8080)或unicorn['port'](如改为8081),需同步调整防火墙规则允许新端口。配置邮件通知
在/etc/gitlab/gitlab.rb中启用SMTP服务,填写邮件服务器信息(以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" # 需使用Gmail应用专用密码
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false
gitlab_rails['gitlab_email_from'] = "your_email@gmail.com"
运行sudo gitlab-ctl reconfigure使配置生效,后续用户注册、合并请求等事件将发送邮件通知。
设置CI/CD流水线
.gitlab-ci.yml文件,定义流水线阶段(如build、test、deploy)和任务(如运行测试脚本、部署到服务器);集成外部服务
/etc/gitlab/gitlab.rb中配置LDAP服务器信息(如OpenLDAP),实现用户通过LDAP账号登录GitLab;gitlab-rake gitlab:backup:create命令备份GitLab数据(默认存储在/var/opt/gitlab/backups目录),避免数据丢失;unicorn['worker_processes'])和数据库连接数(如postgresql['max_connections']),提升系统性能。