温馨提示×

ubuntu与gitlab网络设置指南

小樊
37
2025-10-29 13:12:13
栏目: 智能运维

Ubuntu与GitLab网络设置指南

一、基础环境准备

在开始配置前,需确保Ubuntu服务器满足以下条件:

  • 系统要求:Ubuntu 20.04及以上版本(推荐LTS版);
  • 资源要求:至少2GB RAM(推荐4GB+)、20GB可用存储空间;
  • 网络要求:服务器已连通外网(能访问gitlab.com),具备固定内网IP或域名(可选但推荐)。

关键操作

# 更新系统软件包列表
sudo apt update && sudo apt upgrade -y

二、安装GitLab及依赖

1. 添加GitLab官方软件源

通过官方脚本添加GitLab的APT仓库,确保软件包来源可信:

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash

2. 安装GitLab Community Edition(CE)

安装GitLab主程序及必要依赖(如curlopenssh-serverca-certificates):

sudo apt install gitlab-ce -y

3. 配置外部访问URL

编辑GitLab主配置文件/etc/gitlab/gitlab.rb,设置external_url为服务器的公网IP域名(若未配置域名,可先用IP替代):

sudo vim /etc/gitlab/gitlab.rb
# 找到以下行并修改(示例:使用IP)
external_url 'http://your_server_ip'
# 保存退出(vim中按:wq)

4. 重新配置并启动GitLab

应用配置变更并启动GitLab服务:

sudo gitlab-ctl reconfigure  # 重新加载配置
sudo gitlab-ctl restart     # 重启服务

三、网络访问配置

1. 配置Ubuntu防火墙(UFW)

允许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

2. 配置端口转发(若有NAT设备)

若服务器位于内网,需在路由器或防火墙中设置端口转发,将公网IP的80/443端口映射到服务器的内网IP对应端口(如192.168.1.100:80)。

四、可选:配置HTTPS(提升安全性)

使用Let’s Encrypt免费SSL证书,实现GitLab的HTTPS加密访问:

1. 安装Certbot工具

sudo apt install certbot python3-certbot-nginx -y

2. 获取并安装SSL证书

sudo certbot --nginx -d your_domain.com  # 替换为你的域名

按提示完成证书申请(需验证域名所有权)。

3. 更新GitLab配置

修改/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
# 保存退出

4. 重新加载配置

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

五、验证网络访问

在浏览器中输入http://your_server_ip(或配置的域名),若看到GitLab登录页面,说明网络配置成功。首次登录需修改root用户的默认密码(密码重置命令:sudo cat /etc/gitlab/initial_root_password)。

六、常见问题排查

1. 无法访问GitLab页面

  • 检查服务状态sudo gitlab-ctl status(确认nginxgitlab-workhorse等服务运行正常);
  • 查看日志sudo less /var/log/gitlab/nginx/error.log(分析具体错误原因);
  • 测试端口连通性telnet your_server_ip 80(若无法连接,需检查防火墙或端口转发设置)。

2. 依赖安装失败

若安装过程中出现依赖错误,运行以下命令修复:

sudo apt install -f  # 自动修复缺失的依赖

通过以上步骤,即可完成Ubuntu环境下GitLab的网络配置,实现内外网访问。根据实际需求调整端口、域名或HTTPS设置,确保GitLab服务稳定运行。

0