Linux中GitLab网络配置详细步骤
在配置GitLab网络前,需确保服务器满足以下基础条件:
若服务器需固定IP访问,需修改网络接口配置文件(以CentOS为例,Ubuntu路径为/etc/netplan/*.yaml):
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
修改以下参数(根据实际网络环境调整):
BOOTPROTO=static # 改为静态IP
ONBOOT=yes # 开机自启
IPADDR=192.168.1.100 # 静态IP地址
NETMASK=255.255.255.0 # 子网掩码
GATEWAY=192.168.1.1 # 网关地址
DNS1=8.8.8.8 # DNS服务器
保存后重启网络服务:
sudo systemctl restart network
验证IP是否生效:
ip addr show eth0
GitLab的主网络配置文件为/etc/gitlab/gitlab.rb,需修改以下关键参数:
sudo vi /etc/gitlab/gitlab.rb
设置外部访问URL(必选):
external_url 'http://your_server_ip' # 替换为服务器IP或域名(如http://gitlab.example.com)
若需指定端口(默认80),可修改为:
external_url 'http://your_server_ip:8080' # 自定义端口需与后续SSH端口一致
配置SSH访问(可选但推荐): 若修改了GitLab的SSH端口(默认22),需同步更新以下参数:
gitlab_rails['gitlab_ssh_host'] = 'your_server_ip' # SSH访问的IP/域名
gitlab_rails['gitlab_shell_ssh_port'] = 2222 # 自定义SSH端口(需与防火墙开放端口一致)
保存并应用配置:
sudo gitlab-ctl reconfigure # 重新生成GitLab配置文件
sudo gitlab-ctl restart # 重启GitLab服务使配置生效
若服务器启用了防火墙(如CentOS的firewalld、Ubuntu的ufw),需开放GitLab所需端口:
# 开放HTTP(80)、HTTPS(443)、SSH(22)端口(默认)
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
# 若使用了自定义SSH端口(如2222),需额外开放
sudo firewall-cmd --permanent --zone=public --add-port=2222/tcp
# 重新加载防火墙规则
sudo firewall-cmd --reload
验证端口是否开放:
sudo firewall-cmd --zone=public --list-ports
为提升安全性,建议为GitLab配置SSL证书(可使用Let’s Encrypt免费证书):
安装Certbot(以CentOS为例):
sudo yum install -y epel-release
sudo yum install -y certbot python3-certbot-nginx
获取SSL证书:
sudo certbot certonly --standalone -d your_domain_or_ip
证书会保存在/etc/letsencrypt/live/your_domain_or_ip/目录下。
修改GitLab配置:
编辑/etc/gitlab/gitlab.rb,添加证书路径:
nginx['ssl_certificate'] = "/etc/letsencrypt/live/your_domain_or_ip/fullchain.pem"
nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/your_domain_or_ip/privkey.pem"
nginx['listen_port'] = 443 # 强制使用HTTPS端口
nginx['redirect_http_to_https'] = true # 自动跳转HTTP到HTTPS
应用配置并重启:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
此时可通过https://your_domain_or_ip访问GitLab。
http://your_server_ip(或HTTPS域名),若能看到GitLab登录页面,则说明网络配置成功。git clone ssh://git@your_server_ip:2222/root/test.git # 替换为你的服务器IP和项目路径
若能成功克隆,则SSH配置生效。external_url配置是否正确。gitlab_rails['gitlab_shell_ssh_port']与防火墙开放的端口一致,且sshd服务已启动。