更新系统包索引并安装GitLab运行所需的依赖(包括SSH服务、证书工具、时区数据等):
sudo apt update
sudo apt install -y curl openssh-server ca-certificates tzdata perl
注:
openssh-server用于Git的SSH访问,ca-certificates用于HTTPS加密。
通过官方脚本添加GitLab的APT软件源,确保后续能获取最新版本的GitLab Community Edition(CE):
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
注:此脚本会自动添加GitLab的GPG密钥和软件源到系统,避免手动配置的麻烦。
使用APT包管理器安装GitLab CE(社区版),安装过程中会自动解决依赖关系:
sudo apt install -y gitlab-ce
注:若需指定版本(如
17.4.5-ce.0),可通过apt-cache policy gitlab-ce查看可用版本,再用sudo apt install gitlab-ce=<VERSION>安装。
编辑GitLab的主配置文件/etc/gitlab/gitlab.rb,设置外部访问URL(替换为你的服务器IP或域名):
sudo vim /etc/gitlab/gitlab.rb
找到external_url行,修改为:
external_url 'http://your_server_ip' # 若使用域名,替换为'domain.com'
保存退出后,运行以下命令使配置生效:
sudo gitlab-ctl reconfigure
注:此命令会重新生成GitLab的配置文件、初始化数据库、设置服务权限等。
启动GitLab相关服务(包括Web服务器、数据库、SSH服务等),并设置开机自启:
sudo gitlab-ctl start
sudo systemctl enable gitlab
验证服务状态(确保所有服务均为run状态):
sudo gitlab-ctl status
注:首次启动可能需要几分钟,耐心等待。
http://your_server_ip(或配置的域名),进入GitLab登录页面。root,密码需通过以下命令获取:sudo cat /etc/gitlab/initial_root_password
输入密码后即可完成登录。root密码(建议使用强密码,包含大小写字母、数字和符号)。ufw简化防火墙管理(若未安装,运行sudo apt install ufw):sudo ufw allow http
sudo ufw allow https
sudo ufw allow ssh
sudo ufw enable
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"),将公钥(~/.ssh/id_rsa.pub)添加到GitLab账户的SSH密钥设置中,提升访问安全性。/etc/gitlab/gitlab.rb中修改external_url为其他端口(如http://your_server_ip:8080),然后重新运行sudo gitlab-ctl reconfigure。/etc/gitlab/gitlab.rb中配置SMTP设置(参考GitLab官方文档)。gitlab_rails['memory_limit'])或启用缓存(如Redis)。