Linux下GitLab的网络配置指南
导读:Linux下GitLab网络配置指南 一、基础环境准备 在配置GitLab网络前,需完成以下基础准备工作: 操作系统:推荐使用Ubuntu 22.04+或CentOS Stream 9+等主流Linux发行版; 系统要求:至少2GB RA...
Linux下GitLab网络配置指南
一、基础环境准备
在配置GitLab网络前,需完成以下基础准备工作:
- 操作系统:推荐使用Ubuntu 22.04+或CentOS Stream 9+等主流Linux发行版;
- 系统要求:至少2GB RAM(推荐4GB+)、20GB可用存储空间(根据项目规模调整);
- 网络连通性:确保服务器已接入网络,可通过
ping www.baidu.com测试外网访问; - 依赖安装:安装GitLab运行所需的依赖包(如
curl、openssh-server、ca-certificates等)。
二、配置静态IP(可选但推荐)
若服务器需固定IP访问,需修改网络配置文件设置静态IP:
- Ubuntu/Debian:编辑
/etc/netplan/01-netcfg.yaml(文件名因系统而异),添加以下内容:应用配置:network: version: 2 ethernets: eth0: # 替换为实际网卡名称(通过`ip addr`查看) dhcp4: no addresses: [192.168.1.100/24] # 静态IP及子网掩码 gateway4: 192.168.1.1 # 网关地址 nameservers: addresses: [8.8.8.8, 8.8.4.4] # DNS服务器sudo netplan apply。 - CentOS/RHEL:编辑
/etc/sysconfig/network-scripts/ifcfg-eth0(网卡名称同上),修改以下参数:重启网络服务:BOOTPROTO=static ONBOOT=yes IPADDR=192.168.1.100 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 DNS1=8.8.8.8 DNS2=8.8.4.4sudo systemctl restart network。
三、安装GitLab并配置external_url
- 添加GitLab软件源:
- Ubuntu:运行
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash; - CentOS:运行
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash。
- Ubuntu:运行
- 安装GitLab Community Edition(CE):
- Ubuntu:
sudo apt install gitlab-ce; - CentOS:
sudo yum install gitlab-ce。
- Ubuntu:
- 配置external_url:
编辑GitLab主配置文件/etc/gitlab/gitlab.rb,找到external_url参数,设置为服务器IP或域名(如http://192.168.1.100或http://gitlab.example.com)。若需修改默认端口(如HTTP用8080),可添加端口后缀:http://192.168.1.100:8080。
四、配置防火墙与端口转发
GitLab需开放以下端口供外部访问:
- HTTP(80):网页访问;
- HTTPS(443):加密网页访问(可选);
- SSH(22):代码推送/拉取。
Ubuntu(ufw)
sudo ufw allow http # 开放80端口
sudo ufw allow https # 开放443端口
sudo ufw allow ssh # 开放22端口
sudo ufw enable # 启用防火墙
CentOS(firewalld)
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --permanent --zone=public --add-service=ssh
sudo firewall-cmd --reload # 重新加载防火墙规则
NAT设备端口转发(若有)
若服务器位于内网,需在路由器或防火墙中配置端口转发,将公网IP的80/443/22端口映射到服务器内网IP的对应端口。
五、配置HTTPS(可选但推荐)
为提升安全性,建议为GitLab配置SSL证书(如Let’s Encrypt免费证书):
- 安装Certbot:
- Ubuntu:
sudo apt install certbot python3-certbot-nginx; - CentOS:
sudo yum install certbot python3-certbot-nginx。
- Ubuntu:
- 获取证书:
证书会保存在sudo certbot certonly --standalone -d gitlab.example.com # 替换为你的域名/etc/letsencrypt/live/gitlab.example.com/目录下。 - 配置GitLab使用证书:
编辑/etc/gitlab/gitlab.rb,添加以下参数:nginx['ssl_certificate'] = "/etc/letsencrypt/live/gitlab.example.com/fullchain.pem" nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/gitlab.example.com/privkey.pem" external_url 'https://gitlab.example.com' # 强制使用HTTPS - 重配置并重启:
sudo gitlab-ctl reconfigure sudo gitlab-ctl restart
六、验证网络配置
- 重启GitLab服务:
sudo gitlab-ctl restart - 访问GitLab:
在浏览器中输入http://your_server_ip或https://your_domain,若看到GitLab登录页面,说明配置成功。首次登录需设置管理员密码(默认用户名为root)。
七、常见问题排查
- 无法访问:检查防火墙是否开放对应端口,或服务器网络是否连通;
- 端口冲突:若80端口被占用(如Nginx/Apache),可修改GitLab的Nginx端口(编辑
/etc/gitlab/gitlab.rb,添加nginx['listen_port'] = 8080,然后重配置); - SSH连接失败:确认
gitlab_rails['gitlab_shell_ssh_port']与/etc/ssh/sshd_config中的Port一致,且防火墙开放了该端口。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Linux下GitLab的网络配置指南
本文地址: https://pptw.com/jishu/745294.html
