在开始配置前,需确保Ubuntu服务器满足以下条件:
gitlab.com),具备固定内网IP或域名(可选但推荐)。关键操作:
# 更新系统软件包列表
sudo apt update && sudo apt upgrade -y
通过官方脚本添加GitLab的APT仓库,确保软件包来源可信:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
安装GitLab主程序及必要依赖(如curl、openssh-server、ca-certificates):
sudo apt install gitlab-ce -y
编辑GitLab主配置文件/etc/gitlab/gitlab.rb,设置external_url为服务器的公网IP或域名(若未配置域名,可先用IP替代):
sudo vim /etc/gitlab/gitlab.rb
# 找到以下行并修改(示例:使用IP)
external_url 'http://your_server_ip'
# 保存退出(vim中按:wq)
应用配置变更并启动GitLab服务:
sudo gitlab-ctl reconfigure # 重新加载配置
sudo gitlab-ctl restart # 重启服务
允许GitLab使用的HTTP(80)和HTTPS(443)端口,确保外部流量能访问:
# 查看防火墙状态(确认是否启用)
sudo ufw status
# 允许HTTP端口(80)
sudo ufw allow 80/tcp
# 允许HTTPS端口(443)
sudo ufw allow 443/tcp
# 启用防火墙(若未启用)
sudo ufw enable
若服务器位于内网,需在路由器或防火墙中设置端口转发,将公网IP的80/443端口映射到服务器的内网IP对应端口(如192.168.1.100:80)。
使用Let’s Encrypt免费SSL证书,实现GitLab的HTTPS加密访问:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain.com # 替换为你的域名
按提示完成证书申请(需验证域名所有权)。
修改/etc/gitlab/gitlab.rb,启用SSL并指定证书路径:
sudo vim /etc/gitlab/gitlab.rb
# 修改以下配置
external_url 'https://your_domain.com'
nginx['ssl_certificate'] = "/etc/letsencrypt/live/your_domain.com/fullchain.pem"
nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/your_domain.com/privkey.pem"
nginx['redirect_http_to_https'] = true # 强制跳转HTTPS
# 保存退出
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
在浏览器中输入http://your_server_ip(或配置的域名),若看到GitLab登录页面,说明网络配置成功。首次登录需修改root用户的默认密码(密码重置命令:sudo cat /etc/gitlab/initial_root_password)。
sudo gitlab-ctl status(确认nginx、gitlab-workhorse等服务运行正常);sudo less /var/log/gitlab/nginx/error.log(分析具体错误原因);telnet your_server_ip 80(若无法连接,需检查防火墙或端口转发设置)。若安装过程中出现依赖错误,运行以下命令修复:
sudo apt install -f # 自动修复缺失的依赖
通过以上步骤,即可完成Ubuntu环境下GitLab的网络配置,实现内外网访问。根据实际需求调整端口、域名或HTTPS设置,确保GitLab服务稳定运行。