温馨提示×

Debian怎么修改GitLab端口

小樊
41
2026-08-28 19:02:13
栏目: 智能运维

在 Debian 上修改 GitLab 端口,取决于你是通过 Omnibus 包 安装的 GitLab(最常见)。下面按常见场景说明。


一、确认 GitLab 安装方式(Omnibus)

dpkg -l | grep gitlab

如果看到 gitlab-eegitlab-ce,就是 Omnibus 安装,配置文件在:

/etc/gitlab/gitlab.rb

二、修改 GitLab 端口(Omnibus)

1️⃣ 编辑配置文件

sudo nano /etc/gitlab/gitlab.rb

找到或添加以下内容(以改成 8081 为例):

external_url 'http://gitlab.example.com:8081'
# 或 IP
external_url 'http://192.168.1.100:8081'

如果你只想改 Nginx 监听端口,而不想改 external_url,也可以这样:

nginx['listen_port'] = 8081

✅ 推荐方式:直接改 external_url(最规范)


2️⃣ 重新配置 GitLab

sudo gitlab-ctl reconfigure

这一步会:

  • 修改 Nginx 配置
  • 重载服务
  • 更新 GitLab 内部 URL

3️⃣ 重启 GitLab(可选)

sudo gitlab-ctl restart

三、防火墙放行端口(非常重要)

如果使用 ufw

sudo ufw allow 8081/tcp
sudo ufw reload

如果使用 iptables

sudo iptables -A INPUT -p tcp --dport 8081 -j ACCEPT

四、访问 GitLab

在浏览器中访问:

http://服务器IP:8081

五、常见问题

✅ 端口被占用

查看端口占用:

sudo ss -tulnp | grep 8081

✅ 修改端口后 Git 克隆地址不正确

执行:

sudo gitlab-ctl reconfigure

然后检查:

git config --global --get remote.origin.url

✅ 使用 HTTPS(443 端口)

external_url 'https://gitlab.example.com'

证书配置:

nginx['ssl_certificate'] = "/etc/gitlab/ssl/server.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/server.key"

六、Docker 安装的 GitLab(补充说明)

如果是 Docker:

docker run -d \
  -p 8081:80 \
  --name gitlab \
  gitlab/gitlab-ee

或修改 docker-compose.yml

ports:
  - "8081:80"

总结

需求 操作
修改 GitLab HTTP 端口 /etc/gitlab/gitlab.rb
生效 gitlab-ctl reconfigure
防火墙 ufw allow 端口
Docker -p 主机端口:容器端口

如果你告诉我:

  • GitLab 版本
  • 是否使用 Docker
  • 是否希望同时开启 HTTPS

我可以给你 更精确的一步步配置方案

0