在配置GitLab网络前,需完成以下基础准备工作:
ping www.baidu.com测试外网访问;curl、openssh-server、ca-certificates等)。若服务器需固定IP访问,需修改网络配置文件设置静态IP:
/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。/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.4
重启网络服务:sudo systemctl restart network。curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash;curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash。sudo apt install gitlab-ce;sudo yum install gitlab-ce。/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需开放以下端口供外部访问:
sudo ufw allow http # 开放80端口
sudo ufw allow https # 开放443端口
sudo ufw allow ssh # 开放22端口
sudo ufw enable # 启用防火墙
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 # 重新加载防火墙规则
若服务器位于内网,需在路由器或防火墙中配置端口转发,将公网IP的80/443/22端口映射到服务器内网IP的对应端口。
为提升安全性,建议为GitLab配置SSL证书(如Let’s Encrypt免费证书):
sudo apt install certbot python3-certbot-nginx;sudo yum install certbot python3-certbot-nginx。sudo certbot certonly --standalone -d gitlab.example.com # 替换为你的域名
证书会保存在/etc/letsencrypt/live/gitlab.example.com/目录下。/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
sudo gitlab-ctl restart
http://your_server_ip或https://your_domain,若看到GitLab登录页面,说明配置成功。首次登录需设置管理员密码(默认用户名为root)。/etc/gitlab/gitlab.rb,添加nginx['listen_port'] = 8080,然后重配置);gitlab_rails['gitlab_shell_ssh_port']与/etc/ssh/sshd_config中的Port一致,且防火墙开放了该端口。