如何在CentOS?7上搭建GitLab服務(wù)器(完整指南)
前言

GitLab是一個強大的DevOps平臺,集代碼托管、CI/CD、項目管理于一體。本文將詳細介紹在CentOS 7上搭建GitLab企業(yè)級服務(wù)器的完整流程,包含關(guān)鍵配置和避坑指南。
一、環(huán)境準備
1.1 系統(tǒng)要求
- 最低配置:
- 4核CPU / 4GB內(nèi)存(推薦8GB+)
- 50GB+磁盤空間
- CentOS 7.6+(64位)
?? 注意:內(nèi)存不足會導(dǎo)致GitLab運行緩慢甚至崩潰!
服務(wù)器配置參考:
| 場景 | CPU | 內(nèi)存 | 存儲 |
|---|---|---|---|
| 小型團隊 | 4核 | 8GB | 100GB |
| 中型企業(yè) | 8核 | 16GB | 500GB |
| 大型部署 | 16核 | 32GB | 1TB+ |
1.2基礎(chǔ)服務(wù)配置
# 安裝SSH服務(wù)、curl工具和系統(tǒng)管理工具 sudo yum install -y curl policycoreutils-python openssh-server # 設(shè)置SSH服務(wù)開機自啟動 sudo systemctl enable sshd # 啟動SSH服務(wù)(允許遠程連接) sudo systemctl start sshd # 安裝防火墻管理工具 sudo yum install firewalld systemd -y # 啟動防火墻服務(wù) sudo systemctl start firewalld # 添加HTTP服務(wù)到防火墻(--permanent表示永久生效) sudo firewall-cmd --permanent --add-service=http # 添加HTTPS服務(wù)到防火墻 sudo firewall-cmd --permanent --add-service=https # 添加SSH服務(wù)到防火墻 sudo firewall-cmd --permanent --add-service=ssh # 重新加載防火墻配置(使更改生效) sudo systemctl reload firewalld # 安裝郵件服務(wù)(用于GitLab通知) sudo yum install postfix -y # 設(shè)置郵件服務(wù)開機自啟動 sudo systemctl enable postfix # 啟動郵件服務(wù) sudo systemctl start postfix # 安裝vim編輯器(配置文件修改) sudo yum install -y vim # 安裝wget下載工具(獲取安裝包) sudo yum install -y wget
二、GitLab安裝與配置
2.1 使用國內(nèi)鏡像加速安裝
# 下載清華鏡像站提供的GitLab RPM包(版本14.3.2) wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-14.3.2-ce.0.el7.x86_64.rpm # 使用RPM包管理器安裝GitLab rpm -i gitlab-ce-14.3.2-ce.0.el7.x86_64.rpm
2.2 關(guān)鍵配置修改
# 編輯GitLab主配置文件 vim /etc/gitlab/gitlab.rb
進入gitlab.rb文件后,查找內(nèi)容
# 修改以下配置項(示例): #external_url ‘http://IP:Port' #external_url ‘http://域名' external_url 'http://your-server-ip:1000' # 使用自定義端口 gitlab_rails['smtp_enable'] = true # 啟用郵件發(fā)送(可選) gitlab_rails['smtp_address'] = "smtp.exmail.qq.com" # 郵件服務(wù)器地址(可選) gitlab_rails['time_zone'] = 'Asia/Shanghai' # 設(shè)置時區(qū)(可選)
2.3 防火墻與初始化
# 添加自定義端口到防火墻(示例端口1000) firewall-cmd --permanent --add-port=1000/tcp # 重新加載防火墻配置 sudo systemctl reload firewalld # 重新配置GitLab(應(yīng)用配置更改) gitlab-ctl reconfigure # 重啟GitLab服務(wù) gitlab-ctl restart
2.4 密碼重置方法
# 進入GitLab控制臺 sudo gitlab-rails console -e production # 執(zhí)行密碼重置命令序列 user = User.where(id: 1).first # 獲取root用戶 user.password = 'YourNewPassword123!' # 設(shè)置新密碼 user.password_confirmation = 'YourNewPassword123!' # 確認密碼 user.save! # 保存更改 exit # 退出
2.5重啟Gitlab
# 重啟GitLab服務(wù) gitlab-ctl restart
三、故障排除指南(附解決方案)
3.1 502錯誤(最常見)
原因分析:
- 內(nèi)存不足(GitLab至少需要4GB)
- Unicorn服務(wù)崩潰
3.2 422頁面錯誤
原因分析:
- CSRF令牌驗證失敗
- 代理服務(wù)器配置錯誤
四 GitLab服務(wù)管理命令
# 啟動所有服務(wù) gitlab-ctl start # 停止所有服務(wù) gitlab-ctl stop # 重啟所有服務(wù) gitlab-ctl restart # 查看服務(wù)狀態(tài) gitlab-ctl status # 查看實時日志(Nginx) gitlab-ctl tail nginx # 檢查系統(tǒng)配置 gitlab-rake gitlab:check SANITIZE=true
五 實操截圖
1.安裝SSH服務(wù)、curl工具和系統(tǒng)管理工具
# 安裝SSH服務(wù)、curl工具和系統(tǒng)管理工具 sudo yum install -y curl policycoreutils-python openssh-server

安裝成功會出現(xiàn)如下圖所示

2.設(shè)置SSH服務(wù)
# 設(shè)置SSH服務(wù)開機自啟動 sudo systemctl enable sshd # 啟動SSH服務(wù)(允許遠程連接) sudo systemctl start sshd # 安裝防火墻管理工具 sudo yum install firewalld systemd -y

安裝成功會出現(xiàn)如下圖所示

# 啟動防火墻服務(wù) sudo systemctl start firewalld # 添加HTTP服務(wù)到防火墻(--permanent表示永久生效) sudo firewall-cmd --permanent --add-service=http # 添加HTTPS服務(wù)到防火墻 sudo firewall-cmd --permanent --add-service=https # 添加SSH服務(wù)到防火墻 sudo firewall-cmd --permanent --add-service=ssh # 重新加載防火墻配置(使更改生效) sudo systemctl reload firewalld

3.安裝Postfix
# 安裝郵件服務(wù)(用于GitLab通知) sudo yum install postfix -y

# 設(shè)置郵件服務(wù)開機自啟動 sudo systemctl enable postfix # 啟動郵件服務(wù) sudo systemctl start postfix
4.安裝vim編輯器和wget下載器
# 安裝vim編輯器(配置文件修改)
sudo yum install -y vim
# 安裝wget下載工具(獲取安裝包)
sudo yum install -y wget
5.添加GitLab鏡像源并安裝gitlab
# 下載清華鏡像站提供的GitLab RPM包(版本14.3.2)
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-14.3.2-ce.0.el7.x86_64.rpm
# 使用RPM包管理器安裝GitLab
rpm -i gitlab-ce-14.3.2-ce.0.el7.x86_64.rpm
6.關(guān)鍵配置修改
# 編輯GitLab主配置文件
vim /etc/gitlab/gitlab.rb
# 修改以下配置項(示例):
#external_url ‘http://IP:Port’
#external_url ‘http://域名’
external_url 'http://your-server-ip:1000' # 使用自定義端口
7.防火墻與初始化
# 添加自定義端口到防火墻(示例端口1000)
firewall-cmd --permanent --add-port=1000/tcp
# 重新加載防火墻配置
sudo systemctl reload firewalld
# 重新配置GitLab(應(yīng)用配置更改) 重置過程需要一些時間等待
gitlab-ctl reconfigure

重置成功后,最后幾行的信息如下圖所示
8.重啟GitLab服務(wù)
# 重啟GitLab服務(wù)
gitlab-ctl restart

9.訪問GitLab頁面
訪問方式:IP地址+端口號
訪問地址是來源
#external_url ‘http://IP:Port’
#external_url ‘http://域名’
external_url 'http://your-server-ip:1000' # 使用自定義端口

10.密碼重置方法
# 進入GitLab控制臺
sudo gitlab-rails console -e production
# 執(zhí)行密碼重置命令序列
user = User.where(id: 1).first # 獲取root用戶
user.password = 'dafu200..' # 設(shè)置新密碼
user.password_confirmation = 'dafu200..' # 確認密碼
user.save! # 保存更改
exit # 退出
11.重置完成登錄

12.創(chuàng)建項目倉庫




完美結(jié)束,接下來創(chuàng)建分支
到此這篇關(guān)于如何在CentOS 7上搭建GitLab服務(wù)器(完整指南)的文章就介紹到這了,更多相關(guān)centos GitLab服務(wù)器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Zookeeper3.5.7版本之集群部署過程(linux環(huán)境-centos7)
本文詳細介紹了如何在三臺CentOS?7虛擬機上搭建Zookeeper?3.5.7集群,包括下載JDK11、下載和安裝Zookeeper、配置Zookeeper集群、在防火墻中打開端口以及啟動和停止Zookeeper服務(wù)2026-03-03
Ubuntu服務(wù)器關(guān)機后繼續(xù)執(zhí)行命令的三種方案
文章介紹了三種在電腦關(guān)閉后讓遠程命令持續(xù)執(zhí)行的方法:nohup、tmux和systemd,nohup是最簡單的后臺執(zhí)行方式,但不支持交互,tmux是最靈活的交互式解決方案,可以恢復(fù)會話,systemd是最穩(wěn)定的自動化守護進程,適用于長期任務(wù),需要的朋友可以參考下2026-01-01
Ubuntu20.04安裝Python3的虛擬環(huán)境教程詳解
這篇文章主要介紹了Ubuntu20.04安裝Python3的虛擬環(huán)境,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
詳解如何在Ubuntu 20.04上安裝Xrdp服務(wù)器(遠程桌面)
這篇文章主要介紹了詳解如何在Ubuntu 20.04上安裝Xrdp服務(wù)器(遠程桌面),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

