0%

Ubuntu搭建Gitlab并配置Nginx反向代理

这篇教程是 Ubuntu 搭建Gitlab服务器的教程。

安装依赖

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

接下来添加Gitlab的GPG公钥用于安装Gitlab

1
curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/null

添加清华tuna镜像用于加快下载

1
2
3
sudo vim /etc/apt/sources.list.d/gitlab-ce.list
# 写入 deb Index of /gitlab-ce/ubuntu/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror xenial main
# 在网站中根据你的版本,选择对于的内容写入/etc/apt/sources.list.d/gitlab-ce.list,详见https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/

安装Gitlab-CE

1
2
3
4
5
6
7
8
sudo apt-get update
sudo apt-get install gitlab-ce

# 执行配置
sudo gitlab-ctl reconfigure

# 启动Gitlab
sudo gitlab-ctl start

修改Gitlab的配置

由于每次修改配置后加载配置的时间非常长,所以建议一次修改完所有配置再执行 sudo gitlab-ctl reconfigure

修改root密码

1
2
3
4
5
sudo gitlab-rails console -e production

user = User.where(username:"root").first
user.password = "你的密码"
> user.save

修改端口地址

1
2
3
4
5
6
7
8
sudo vim /etc/gitlab/gitlab.rb 

# 因为要使用nginx反向代理,所以直接修改如下属性:
# external_url修改为外网访问的地址
# nginx['listen_port']修改为gitlab实际监听的端口,后续配置nginx反向代理到该端口
external_url 'https://gitlab.your_domain.com'
nginx['listen_port'] = 80
nginx['listen_https'] = false

修改完成后记得使用 sudo gitlab-ctl reconfigure 加载新配置

Nginx反向代理配置

这里给出一份示例配置,ssl证书需要自己申请

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
server {
add_header X-Served-By $host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
# 下面的代码中 gitlab_ip修改为gitlab所处的ip地址
# gitlab.your_domain.com 修改为外网访问的地址
# listen_port 修改为gitlab配置中的 nginx['listen_port']
proxy_pass http://gitlab_ip:listen_port$request_uri;
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name gitlab.your_domain.com;
# Let's Encrypt SSL
include conf.d/include/letsencrypt-acme-challenge.conf;
include conf.d/include/ssl-ciphers.conf;
ssl_certificate /etc/letsencrypt/live/gitlab/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/gitlab/privkey.pem;
access_log /data/logs/gitlab_access.log proxy;
error_log /data/logs/gitlab_error.log warn;
}

随后重新加载nginx配置即可访问Gitlab啦