GitLab 是一款開源的代碼托管平臺,集代碼倉庫、持續(xù)集成與部署、代碼審查等功能于一體。本文將指導(dǎo)你在 CentOS 系統(tǒng) 上搭建 GitLab。
一、準(zhǔn)備工作
1. 系統(tǒng)要求
操作系統(tǒng):CentOS 7 或 CentOS 8
硬件要求:
CPU:2 核或以上
內(nèi)存:4GB 或以上
磁盤:20GB 可用空間
2. 配置基礎(chǔ)環(huán)境
執(zhí)行以下命令,更新系統(tǒng)并安裝基礎(chǔ)工具:
yum update -y
yum install -y curl policycoreutils-python openssh-server perl
二、安裝所需組件
1. 安裝 PostgreSQL
GitLab 使用 PostgreSQL 作為默認(rèn)數(shù)據(jù)庫。安裝并初始化:
yum install -y postgresql-server
postgresql-setup initdb
systemctl enable postgresql
systemctl start postgresql
2. 安裝 Redis
Redis 用于緩存和其他 GitLab 服務(wù):
yum install -y redis
systemctl enable redis
systemctl start redis
三、安裝 GitLab
1. 添加 GitLab 軟件源
使用官方腳本配置 GitLab 社區(qū)版(CE)軟件源:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
2. 安裝 GitLab
安裝 GitLab CE 并指定外部訪問地址:
EXTERNAL_URL="http://your-domain-or-ip" yum install -y gitlab-ce
3. 配置數(shù)據(jù)存儲路徑
修改 GitLab 配置文件/etc/gitlab/gitlab.rb:
git_data_dirs({
"default" => {
"path" => "/data/gitlab/git-data"
}
})
gitlab_rails['backup_path'] = "/data/gitlab/backups"
4. 啟動 GitLab
應(yīng)用配置并啟動 GitLab:
gitlab-ctl reconfigure
gitlab-ctl restart
5. 查看初始密碼
GitLab 在初始化時會生成一個管理員賬號密碼,查看文件即可:
cat /etc/gitlab/initial_root_password
四、訪問 GitLab
在瀏覽器中輸入 http://your-domain-or-ip
使用 root 賬戶和生成的初始密碼登錄
登錄后可創(chuàng)建項目、管理用戶、配置 CI/CD 等
五、GitLab 常用命令
1. 服務(wù)管理
啟動 GitLab:gitlab-ctl start
停止 GitLab:gitlab-ctl stop
重啟 GitLab:gitlab-ctl restart
檢查服務(wù)狀態(tài):gitlab-ctl status
2. 配置與日志
重新配置 GitLab:gitlab-ctl reconfigure
查看配置文件:cat /etc/gitlab/gitlab.rb
查看日志:less /var/log/gitlab/gitlab-rails/production.log
3. 數(shù)據(jù)備份與還原
手動備份:gitlab-rake gitlab:backup:create
還原備份:gitlab-rake gitlab:backup:restore
清理舊備份文件:gitlab-ctl cleanup
六、GitLab 組件說明
GitLab 是一個復(fù)雜的集成系統(tǒng),主要組件如下:
組件
功能說明
Puma
運(yùn)行 GitLab Rails 的 Web 應(yīng)用服務(wù)器
Redis
用于緩存和會話管理
PostgreSQL
數(shù)據(jù)庫服務(wù),存儲 GitLab 的核心數(shù)據(jù)
Nginx
靜態(tài)文件服務(wù)和反向代理
Sidekiq
后臺任務(wù)處理
Prometheus
系統(tǒng)監(jiān)控與報警工具
Gitaly
提供 Git 倉庫服務(wù),支持 Git 操作
七、GitLab 目錄結(jié)構(gòu)
目錄位置
說明
/var/opt/gitlab/git-data/repositories倉庫存儲目錄
/opt/gitlabGitLab 安裝路徑
/etc/gitlab配置文件路徑
/var/log/gitlabGitLab 日志存儲路徑
/var/opt/gitlab/backups備份文件存儲目錄
八、最佳實(shí)踐
1. 定期備份
添加定時任務(wù),每天自動備份:
crontab -e
# 添加以下內(nèi)容,每天凌晨備份
0 0 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
2. 啟用日志輪轉(zhuǎn)
防止日志文件過大:
gitlab-ctl logrotate
3. 優(yōu)化性能
根據(jù)服務(wù)器配置,調(diào)整以下參數(shù):
puma['worker_processes']:設(shè)置為 CPU 核心數(shù)
sidekiq['concurrency']:設(shè)置合適的并發(fā)數(shù)
4. 配置 SSL
啟用 HTTPS 確保傳輸安全:
在配置文件/etc/gitlab/gitlab.rb 中配置 SSL 證書路徑
重啟 GitLab:gitlab-ctl reconfigure
九、總結(jié)
本文介紹了在 CentOS 系統(tǒng)上安裝和配置 GitLab 的完整流程,包括組件介紹、目錄結(jié)構(gòu)以及最佳實(shí)踐配置。完成這些步驟后,你將擁有一個功能強(qiáng)大的代碼托管平臺。如果有其他問題,歡迎在評論區(qū)討論!