Ubuntu配置GitLab环境详细步骤
gitlab),方便后续配置域名解析。sudo hostnamectl set-hostname gitlab && bash # 立即生效
echo "127.0.0.1 gitlab" | sudo tee -a /etc/hosts # 绑定主机名与IP
sudo systemctl disable --now ufw # 禁用ufw防火墙
sudo apt update && sudo apt upgrade -y
GitLab运行需依赖SSH(远程访问)、CA证书(HTTPS加密)、时区数据(时间同步)等工具,通过以下命令安装:
sudo apt install -y curl openssh-server ca-certificates tzdata perl
注:
openssh-server用于GitLab的SSH协议访问(默认端口22);postfix为邮件服务(可选,若需邮件通知需额外配置)。
GitLab未包含在Ubuntu默认源中,需通过官方脚本添加专用存储库,以获取最新稳定版本的安装包。
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
注:若需安装企业版(GitLab EE),将
gitlab-ce替换为gitlab-ee即可。
GitLab需通过外部URL访问(如服务器IP或域名),修改默认配置以适配你的环境:
EXTERNAL_URL="http://your_server_ip" # 替换为实际IP或域名(如http://192.168.1.100)
注:若使用域名,需提前解析到服务器IP,并配置SSL证书(可选但推荐)。
通过apt命令安装GitLab CE,系统会自动解决依赖关系:
sudo apt install gitlab-ce -y
注:若需指定版本(如17.5.2),可使用
sudo apt install gitlab-ce=17.5.2-ce.0(需提前查询可用版本)。
安装完成后,需运行reconfigure命令应用配置(如外部URL、服务端口等),生成必要的系统文件:
sudo gitlab-ctl reconfigure
此命令会自动配置SSH端口、Web服务端口、数据库连接等,耗时约1-2分钟。
启动GitLab相关服务(包括Web服务器、数据库、Git服务等),并设置为开机自启动:
sudo gitlab-ctl start # 启动服务
sudo systemctl enable gitlab-runsvdir.service # 开机自启动
确认GitLab各组件是否正常运行(应显示run状态):
sudo gitlab-ctl status
示例输出:
run: alertmanager: (pid 1234) 1m; run: log: (pid 1235) 1m
run: gitaly: (pid 1236) 1m; run: log: (pid 1237) 1m
run: gitlab-exporter: (pid 1238) 1m; run: log: (pid 1239) 1m
run: gitlab-workhorse: (pid 1240) 1m; run: log: (pid 1241) 1m
run: logrotate: (pid 1242) 1m; run: log: (pid 1243) 1m
run: postgres: (pid 1244) 1m; run: log: (pid 1245) 1m
run: prometheus: (pid 1246) 1m; run: log: (pid 1247) 1m
run: redis: (pid 1248) 1m; run: log: (pid 1249) 1m
run: sidekiq: (pid 1250) 1m; run: log: (pid 1251) 1m
run: unicorn: (pid 1252) 1m; run: log: (pid 1253) 1m
确认安装的GitLab版本,确保符合预期:
gitlab-ce --version
示例输出:
gitlab-ce 17.5.2-ce.0
在浏览器中输入配置的外部URL(如http://your_server_ip),进入GitLab登录页面。
root,系统会提示设置新密码(需包含大小写字母、数字和特殊字符,长度≥8位)。root账号及设置的密码登录。Settings → Appearance,选择“中文(简体)”即可切换界面语言。/etc/gitlab/gitlab.rb文件,取消以下注释并修改为你的SMTP信息:gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.example.com"
gitlab_rails['smtp_port'] = 587
gitlab_rails['smtp_user_name'] = "your_email@example.com"
gitlab_rails['smtp_password'] = "your_password"
gitlab_rails['smtp_domain'] = "example.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['gitlab_email_from'] = "your_email@example.com"
保存后运行sudo gitlab-ctl reconfigure使配置生效。sudo gitlab-ctl stopsudo gitlab-ctl restartsudo gitlab-ctl tail(实时日志)或sudo less /var/log/gitlab/gitlab-rails/production.log(历史日志)sudo gitlab-rails console production # 进入Rails控制台
user = User.find_by(username: 'root') # 查找root用户
user.password = 'new_password' # 设置新密码
user.password_confirmation = 'new_password' # 确认新密码
user.save!
exit # 退出控制台
postgresql、redis)或升级硬件。sudo gitlab-rake gitlab:backup:create # 创建备份(默认路径:/var/opt/gitlab/backups)
ufw),仅允许HTTP(80)、HTTPS(443)、SSH(22)端口访问;配置SSL证书(如Let’s Encrypt),启用HTTPS加密传输。