在Linux中搭建GitLab服务器需要经过以下几个步骤:
1. 准备工作
- 操作系统:确保你有一个Linux服务器,推荐使用Ubuntu或CentOS。
- 依赖包:安装必要的依赖包。
在Ubuntu上:
sudo apt-get update sudo apt-get install -y curl openssh-server ca-certificates tzdata perl
在CentOS上:
sudo yum update sudo yum install -y curl policycoreutils openssh-server perl
2. 添加GitLab仓库
使用官方提供的脚本添加GitLab仓库。
在Ubuntu上:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
在CentOS上:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/el/$releasever/gitlab-ce.repo -o /etc/yum.repos.d/gitlab-ce.repo
3. 安装GitLab
根据你的操作系统选择相应的安装命令。
在Ubuntu上:
sudo EXTERNAL_URL="http://your_server_ip" apt-get install gitlab-ce
在CentOS上:
sudo yum install -y gitlab-ce
4. 配置GitLab
编辑GitLab的配置文件 /etc/gitlab/gitlab.rb
,设置外部URL和其他配置。
external_url 'http://your_server_ip'
5. 重新配置并重启GitLab
运行以下命令以应用配置更改并重启GitLab服务。
sudo gitlab-ctl reconfigure sudo gitlab-ctl restart
6. 访问GitLab
打开浏览器并访问 http://your_server_ip
,你应该会看到GitLab的登录页面。默认情况下,用户名是 root
,密码是在安装过程中生成的。
7. 重置密码
如果你忘记了密码,可以通过SSH登录到服务器并重置密码。
sudo gitlab-rails console
在控制台中输入以下命令来重置密码:
user = User.where(id: 1).first user.password = 'new_password' user.password_confirmation = 'new_password' user.save!
8. 配置防火墙
确保你的防火墙允许HTTP(80)和HTTPS(443)流量。
在Ubuntu上:
sudo ufw allow 80 sudo ufw allow 443
在CentOS上:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
9. 配置SSL(可选)
为了提高安全性,建议配置SSL证书。你可以使用Let’s Encrypt免费获取SSL证书。
安装Certbot:
sudo apt-get install certbot python3-certbot-nginx # Ubuntu sudo yum install certbot python3-certbot-nginx # CentOS
获取并配置SSL证书:
sudo certbot --nginx -d your_domain.com
按照提示完成配置。
10. 配置GitLab Runner(可选)
如果你需要使用GitLab Runner来运行CI/CD任务,可以按照官方文档进行配置。
通过以上步骤,你应该能够在Linux服务器上成功搭建GitLab服务器。