linux

Linux下GitLab的网络配置指南

小樊
45
2025-11-06 20:34:28
栏目: 智能运维

Linux下GitLab网络配置指南

一、基础环境准备

在配置GitLab网络前,需完成以下基础准备工作:

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

若服务器需固定IP访问,需修改网络配置文件设置静态IP:

三、安装GitLab并配置external_url

  1. 添加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
  2. 安装GitLab Community Edition(CE)
    • Ubuntusudo apt install gitlab-ce
    • CentOSsudo yum install gitlab-ce
  3. 配置external_url
    编辑GitLab主配置文件/etc/gitlab/gitlab.rb,找到external_url参数,设置为服务器IP或域名(如http://192.168.1.100http://gitlab.example.com)。若需修改默认端口(如HTTP用8080),可添加端口后缀:http://192.168.1.100:8080

四、配置防火墙与端口转发

GitLab需开放以下端口供外部访问:

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免费证书):

  1. 安装Certbot
    • Ubuntusudo apt install certbot python3-certbot-nginx
    • CentOSsudo yum install certbot python3-certbot-nginx
  2. 获取证书
    sudo certbot certonly --standalone -d gitlab.example.com  # 替换为你的域名
    
    证书会保存在/etc/letsencrypt/live/gitlab.example.com/目录下。
  3. 配置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
    
  4. 重配置并重启
    sudo gitlab-ctl reconfigure
    sudo gitlab-ctl restart
    

六、验证网络配置

  1. 重启GitLab服务
    sudo gitlab-ctl restart
    
  2. 访问GitLab
    在浏览器中输入http://your_server_iphttps://your_domain,若看到GitLab登录页面,说明配置成功。首次登录需设置管理员密码(默认用户名为root)。

七、常见问题排查

0
看了该问题的人还看了