温馨提示×

Linux中GitLab怎样进行网络配置

小樊
62
2025-09-23 06:21:39
栏目: 智能运维

Linux中GitLab网络配置详细步骤

一、前置准备

在配置GitLab网络前,需确保服务器满足以下基础条件:

  • 系统要求:使用Ubuntu(22.04+)、CentOS(Stream 8/9)等主流Linux发行版;
  • 资源要求:至少2GB内存(推荐4GB+)、20GB可用存储空间;
  • 网络连通性:服务器已接入网络,可通过SSH远程访问。

二、配置静态IP(可选但推荐)

若服务器需固定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核心网络配置

GitLab的主网络配置文件为/etc/gitlab/gitlab.rb,需修改以下关键参数:

sudo vi /etc/gitlab/gitlab.rb
  1. 设置外部访问URL(必选):

    external_url 'http://your_server_ip'  # 替换为服务器IP或域名(如http://gitlab.example.com)
    

    若需指定端口(默认80),可修改为:

    external_url 'http://your_server_ip:8080'  # 自定义端口需与后续SSH端口一致
    
  2. 配置SSH访问(可选但推荐): 若修改了GitLab的SSH端口(默认22),需同步更新以下参数:

    gitlab_rails['gitlab_ssh_host'] = 'your_server_ip'  # SSH访问的IP/域名
    gitlab_rails['gitlab_shell_ssh_port'] = 2222  # 自定义SSH端口(需与防火墙开放端口一致)
    
  3. 保存并应用配置

    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

五、配置HTTPS(可选但强烈推荐)

为提升安全性,建议为GitLab配置SSL证书(可使用Let’s Encrypt免费证书):

  1. 安装Certbot(以CentOS为例):

    sudo yum install -y epel-release
    sudo yum install -y certbot python3-certbot-nginx
    
  2. 获取SSL证书

    sudo certbot certonly --standalone -d your_domain_or_ip
    

    证书会保存在/etc/letsencrypt/live/your_domain_or_ip/目录下。

  3. 修改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
    
  4. 应用配置并重启

    sudo gitlab-ctl reconfigure
    sudo gitlab-ctl restart
    

    此时可通过https://your_domain_or_ip访问GitLab。

六、验证网络配置

  1. 通过浏览器访问:输入http://your_server_ip(或HTTPS域名),若能看到GitLab登录页面,则说明网络配置成功。
  2. 通过SSH克隆仓库:测试SSH访问是否正常:
    git clone ssh://git@your_server_ip:2222/root/test.git  # 替换为你的服务器IP和项目路径
    
    若能成功克隆,则SSH配置生效。

七、常见问题排查

  • 无法访问GitLab:检查防火墙是否开放了对应端口,或external_url配置是否正确。
  • SSH连接失败:确认gitlab_rails['gitlab_shell_ssh_port']与防火墙开放的端口一致,且sshd服务已启动。
  • 证书错误:确保SSL证书路径正确,且证书未过期。

0