Gitlab 私有化部署

最后更新

2024-08-12

官方安装包

安装必要依赖

1
2
sudo apt-get update
sudo apt-get install -y curl openssh-server ca-certificates tzdata perl

安装邮件服务,配置项选择 Internet Site,mail name 填写当前服务器 DNS.

1
sudo apt-get install -y postfix

添加仓库

1
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash

安装, 填写需要访问的域名

1
sudo apt-get install gitlab-ee

前置nginx 配置, 非 gitlab 内部 nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
stream {
server {
listen 24922;
proxy_pass 192.168.48.227:22;
}
}

http {
server {
listen 9348 ssl;
listen [::]:9348 ssl;
http2 on;
server_name gitlab.iftrue.club gitlab.iftrue.me;

location / {
proxy_pass http://192.168.48.227;

proxy_read_timeout 300s;
proxy_connect_timeout 300s;
proxy_redirect off;

# Pass along essential headers
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Ssl on; #
}
}
}

修改配置文件 /etc/gitlab/gitlab.rb

1
2
3
4
5
6
7
8
9
10
11
# 修改外部访问地址,用于项目的下载地址,需要有完整的协议和端口号
external_url "https://gitlab.iftrue.me"

# 修改ssh端口,用于 ssh 克隆项目

gitlab_rails['gitlab_shell_ssh_port'] = 24922

# 保存后应用配置
sudo gitlab-ctl stop
sudo gitlab-ctl reconfigure
sudo gitlab-ctl start

如果在 Gitlab 前面有统一的反向代理,无需 Gitlab 本身处理 SSL 证书,可以将 Gitlab 的 nginx 配置为 80 端口。[文档]

1
2
3
4
5
nginx['enable'] = true
nginx['listen_port'] = 80
nginx['listen_https'] = false

sudo gitlab-ctl reconfigure

Docker 方式安装

只需要准备好证书文件,配置.yml 文件即可使用

/docker/gitlab/docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
version: '3.6'
services:
gitlab:
image: gitlab/gitlab-ee:latest
container_name: gitlab
restart: always
# 必填:访问gitlab的域名
hostname: 'gitlab.iftrue'
environment:
GITLAB_OMNIBUS_CONFIG: |
# 必填: 外部访问gitlab的地址
# 用于内部生成外部访问的链接,例如 clone地址
# 即使通过nginx代理访问gitlab, 协议也必须相同
external_url 'https://gitlab.iftrue.com'
# 首次登录时的免密
gitlab_rails['initial_root_password']='xxxx'
# ssh 端口
gitlab_rails['gitlab_shell_ssh_port'] = 24922
ports:
# 外部和内部端口必须与external_url端口相同
- '9348:9348'
- '24922:22'
volumes:
- './config:/etc/gitlab'
- './logs:/var/log/gitlab'
- './data:/var/opt/gitlab'
shm_size: '256m'
# 设置日志大小,避免磁盘写满
logging:
driver: "json-file"
options:
max-size: "50m" # 单个日志文件最大为 50MB
max-file: "5" # 最多保留 5 个日志文件

启动服务,需要等待一段时间,观察 docker 状态是否是 healthy

1
2
3
#  拉取最新镜像
docker compose pull
docker compose up -d

获取/修改 初始密码

1
docker exec -it  gitlab /bin/bash

查看初始密码,安装 gitlab 后 24 小时会自动删除

1
cat /etc/gitlab/initial_root_password

修改初始密码

1
2
3
4
5
gitlab-rails console                   # 进入命令行
u=User.where(id:1).first # 查找root用户
u.password='12345678' # 修改密码
u.password_confirmation='12345678' # 确认密码
u.save # 保存配置

nginx 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name gitlab.iftrue.me;

location / {
# 如果 external_url 设置了 https 就要访问https地址
# 可以选择关闭强制https跳转的配置
proxy_pass https://192.168.48.213:9348;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}


# 因为社区版的 nginx 不支持 tcp 流量转发,因此下面配置无效
# 可以使用防火墙进行转发

# server {
# listen 24922 ssl;
# listen [::]:24922 ssl;
# server_name gitlab.iftrue.me;
# location / {
# proxy_pass https://192.168.48.213:24922;
# }
# }
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2025 SunZhiqi

此时无声胜有声!

支付宝
微信