gitlab-doc-搭建和使用

安装

推荐安装

1
2
3
4
5
6
# 1.yum安装
curl -sS http://packages.gitlab.cc/install/gitlab-ce/script.rpm.sh | sudo bash
yum install gitlab-ce

# 2.启动
gitlab-ctl reconfigure

推荐安装不可行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 1. 安装配置依赖项
# 如想使用Postfix来发送邮件,在安装期间请选择’Internet Site’. 您也可以用sendmai或者 配置SMTP服务 并 使用SMTP发送邮件.
# 在 Centos 6 系统上, 下面的命令将在系统防火墙里面开放HTTP和SSH端口.

sudo yum install curl openssh-server openssh-clients postfix cronie -y
sudo service postfix start
sudo chkconfig postfix on
sudo lokkit -s http -s ssh

# 2. 添加GitLab仓库,并安装到服务器上
curl -sS http://packages.gitlab.cc/install/gitlab-ce/script.rpm.sh | sudo bash
sudo yum install gitlab-ce

# 如果你不习惯使用命令管道的安装方式, 你可以在这里下载 安装脚本 或者 手动下载您使用的系统相应的安装包(RPM/Deb) 然后安装

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-XXX.rpm
rpm -ivh gitlab-ce-XXX.rpm

# 3.启动GitLab
sudo gitlab-ctl reconfigure

安装参考

https://www.cnblogs.com/UniqueColor/p/7766876.html
https://blog.csdn.net/hailangdeyingzi/article/details/76696941
https://blog.csdn.net/benpaodelulu_guajian/article/details/83822059

错误

Job for postfix.service failed

Job for postfix.service failed. See ‘systemctl status postfix.service’ and ‘journalctl -xn’ for details.

1
2
3
4
5
6
7
8
9
# 打开postfix配置
vim /etc/postfix/main.cf
# 发现116行左右的配置为:
inet_interfaces = localhost
inet_protocols = all

# 改成以下内容并保存:
inet_interfaces = localhost # 只能接受来自本机的邮件
inet_protocols = ipv4 # 拒绝ipv6的本机地址

FirewallD is not running

1
2
3
4
5
6
7
8
9
10
11
12
13
# 修改配置文件 /etc/sysctl.conf, 在最后添加以下内容后保存:
net.ipv4.ip_forward=1
# 为了使配置生效, 输入:
sysctl -p
# 再次启动firewalld服务, 输入:
systemctl start firewalld
# 检查firewalld服务状态, 输入:
systemctl status firewalld -l
# 提示以下信息, 注意到Active: active (running)则成功:参考1图

# 再次输入
sudo firewall-cmd --permanent --add-service=http
# 提示以下信息, 则成功: 参考2图

Request Entity Too Large

使用 client_max_body_size 设置允许最大请求的数据的大小:

1
2
3
4
5
6
7
8
9
10
11
12
server {
listen 80;
server_name git.example.com;

# 设置最大为 50 M
client_max_body_size 50m;

location / {
# 3000 是 Gogs 运行的端口
proxy_pass http://localhost:3000;
}
}

防火墙设置命令图

防火墙设置命令图

防火墙设置成功图

防火墙设置成功图

命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 查看所有命令
gitlab-ctl

# 启动
gitlab-ctl start

# 重启
gitlab-ctl restart

# 关闭
gitlab-ctl stop

# 查看状态
gitlab-ctl status

# 重编译配置
gitlab-ctl reconfigure

# 查看日志
gitlab-ctl tail

设置

修改nginx端口号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1.编辑gitlab.rb
vim /etc/gitlab/gitlab.rb

nginx['listen_port'] = 8081

# 2.编辑gitlab-http.conf
vim /var/opt/gitlab/nginx/conf/gitlab-http.conf

server {
listen *:8081;

# 3.重启
gitlab-ctl reconfigure
gitlab-ctl restart

修改unicorn端口

1
2
3
4
5
6
7
8
# 1.编辑gitlab.rb
vim /etc/gitlab/gitlab.rb

unicorn['port'] = 8092

# 2.重启
gitlab-ctl reconfigure
gitlab-ctl restart

修改clone地址

1
2
3
4
5
6
7
8
9
10
# 1.编辑gitlab.yml配置,修改对应的host
vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml

gitlab:
host: localhost >> 这里改为主机的IP即可
port: 80
https: false

# 2.重启gitlab
gitlab-ctl restart

内存使用过高

v12

修改 /etc/gitlab/gitlab.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 修改内存
# unicorn['worker_memory_limit_min'] = "400 * 1 << 20"
# unicorn['worker_memory_limit_max'] = "650 * 1 << 20"
# 改成
unicorn['worker_memory_limit_min'] = "200 * 1 << 20"
unicorn['worker_memory_limit_max'] = "300 * 1 << 20"

# 修改并发数
# sidekiq['concurrency'] = 25
# 改成
sidekiq['concurrency'] = 16

# 数据库修改
# postgresql['shared_buffers'] = "256MB"
# postgresql['max_worker_processes'] = 8
postgresql['shared_buffers'] = "256MB"
postgresql['max_worker_processes'] = 8

# 修改cpu数据
# unicorn['worker_timeout'] = 60
# unicorn['worker_processes'] = 2
unicorn['worker_timeout'] = 60
unicorn['worker_processes'] = 2

v14

修改 /etc/gitlab/gitlab.rb

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 关闭容器仓库功能
gitlab_rails['gitlab_default_projects_features_container_registry'] = false
gitlab_rails['registry_enabled'] = false
registry['enable'] = false
registry_nginx['enable'] = false

# 包仓库、依赖管理
gitlab_rails['packages_enabled'] = false
gitlab_rails['dependency_proxy_enabled'] = false

# GitHub Pages 服务为开源项目提供了一个不错的文档、Demo 的展示方式,GitLab 虽说也是如此,但在私有化部署场景,可以使用 CI 结合其他更高效的工具,完成这个功能,比如 Hugo(golang) / MdBook(rust) 。
# 如果你翻阅资料,会发现在 GitLab 内部,Pages 是一个相对复杂的服务,可选择的配置项非常多。同样可以调整配置对其进行关闭。
# GitLab Pages
gitlab_pages['enable'] = false
pages_nginx['enable'] = false

# GitLab 14 中对于监控功能有着非常多的加强,甚至在应用初始化后,会创建一个项目专门用于监控 GitLab 本体的运行状况。
对于个人使用场景,由于没有复杂负载压力,做好备份即可保障数据安全,所以监控和性能基准相关的功能都可以考虑关闭。
# 关闭监控和性能基准相关功能
prometheus_monitoring['enable'] = false
alertmanager['enable'] = false
node_exporter['enable'] = false
redis_exporter['enable'] = false
postgres_exporter['enable'] = false
pgbouncer_exporter['enable'] = false
gitlab_exporter['enable'] = false
grafana['enable'] = false
sidekiq['metrics_enabled'] = false

# 针对应用的性能分析和上报,也可以直接关闭。
# Usage Statistics
gitlab_rails['usage_ping_enabled'] = false
gitlab_rails['sentry_enabled'] = false
grafana['reporting_enabled'] = false

# 对于个人场景,如果你没有混合云/公有云场景,或不需要使用 GitLab 进行 CD 管理,那么 KAS 和 Terraform 完全可以关闭。
# 而 Kerberos 和 Sentinel 在文档中看起来是企业版软件的功能,为了节约资源,这里一并调整配置,显式声明关闭功能。
# Mattermost 虽然是一个好用聊天的应用,在单人场景下、或者有熟悉的的IM场景下,这个功能也可以进行关闭。
# GitLab KAS
gitlab_kas['enable'] = false
gitlab_rails['gitlab_kas_enabled'] = false
# Terraform
gitlab_rails['terraform_state_enabled'] = false
# Kerberos 文档说EE only,但是默认值为 true
gitlab_rails['kerberos_enabled'] = false
# Sentinel
sentinel['enable'] = false
# Mattermost
mattermost['enable'] = false
mattermost_nginx['enable'] = false

# 默认情况下,GitLab 会创建大量 Puma 进程来提供 Web 服务能力。我们可以适当对其进行调整和设置,够用就行。此外管理调度的 sidekiq 也可以调低并发,避免不必要的资源浪费。
# 禁用 PUMA 集群模式
puma['worker_processes'] = 0
puma['min_threads'] = 1
puma['max_threads'] = 2

# 降低后台守护进程并发数
sidekiq['max_concurrency'] = 5

# 如果不需要电子邮件相关功能,也可以进行关闭。
# 关闭电子邮件相关功能
gitlab_rails['smtp_enable'] = false
gitlab_rails['gitlab_email_enabled'] = false
gitlab_rails['incoming_email_enabled'] = false

# GitLab CPU 占用波动频繁的问题吗?如果你愿意使用轻量的 Drone 对其进行替换的话,可以考虑将默认的 CI 功能关闭,可以将 CPU 资源消耗降低到非常低的数值。
gitlab_ci['gitlab_ci_all_broken_builds'] = false
gitlab_ci['gitlab_ci_add_pusher'] = false

操作

root密码忘记

进入git的bin的目录找到gitlab-rails,执行命令进入命令行修改密码

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
# 进入bin目录
cd /opt/gitlab/bin

# 进入命令行
# 进入过程有点慢
gitlab-rails console production

# 获取root用户
user = User.where(id: 1).first

# 查看旧的密码
user.password

# 修改密码
user.password='xxxxxxx'

# 查看新密码
user.password

# 保存
# 提示如下信息表示成功
# Enqueued ActionMailer::DeliveryJob (Job ID: 7dc9cc44-0296-4a62-8b9a-335d4d5da11b) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", gid://gitlab/User/1
# => true
user.save!

# 退出
# 不需要重启服务器,直接用新密码登录即可
quit

使用外部nginx1

  • 修改gitlab的配置
1
2
3
4
5
6
7
8
9
10
11
# 关闭自带的nginx
vim /etc/gitlab/gitlab.rb

# nginx['enable'] = true换成 false
nginx['enable'] = false

# 修改unicron端口
vim /var/opt/gitlab/gitlab-rails/etc/unicorn.rb

# listen "127.0.0.1:80", 改成自己的端口
listen "127.0.0.1:8082", :tcp_nopush => true
  • 配置nginx跳转git
    新增加nginx对git的配置
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# gitlab socket 文件地址
upstream gitlab {
# 不同版本不一样,可以在 /var/opt/gitlab/gitlab-rails/etc/unicorn.rb 查看
server unix:/var/opt/gitlab/gitlab-workhorse/socket;
}

server {
listen *:80;

server_name xxxx.com; # 请修改为你的域名

server_tokens off; # don't show the version number, a security best practice
root /opt/gitlab/embedded/service/gitlab-rails/public;

# Increase this if you want to upload large attachments
# Or if you want to accept large git objects over http
client_max_body_size 250m;

# individual nginx logs for this gitlab vhost
access_log /var/log/gitlab/nginx/gitlab_access.log;
error_log /var/log/gitlab/nginx/gitlab_error.log;

location / {
# serve static files from defined root folder;.
# @gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html @gitlab;
}

# if a file, which is not found in the root folder is requested,
# then the proxy pass the request to the upsteam (gitlab unicorn)
location @gitlab {
# If you use https make sure you disable gzip compression
# to be safe against BREACH attack

proxy_read_timeout 300; # Some requests take more than 30 seconds.
proxy_connect_timeout 300; # Some requests take more than 30 seconds.
proxy_redirect off;

proxy_set_header X-Forwarded-Proto $scheme;
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-Frame-Options SAMEORIGIN;

proxy_pass http://gitlab;
}

# Enable gzip compression as per rails guide: http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
# WARNING: If you are using relative urls do remove the block below
# See config/application.rb under "Relative url support" for the list of
# other files that need to be changed for relative url support
location ~ ^/(assets)/ {
root /opt/gitlab/embedded/service/gitlab-rails/public;
# gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}

error_page 502 /502.html;
}
  • 502权限处理
1
2
3
# 添加git的socket的权限允许所有访问
# 路径不同版本不一样,可以在/var/opt/gitlab/gitlab-rails/etc/unicorn.rb查看
chmod -R o+x /var/opt/gitlab/gitlab-workhorse/socket

使用外部nginx2

修改git的nginx的端口,然后自己的nginx代理git的nginx端口即可。

ip地址乱码

http://ac90e9c0d704/yfdg/client.git

1
2
3
4
5
6
7
8
9
10
11
# 1. 进入容器
docker exec -it gitlab bash

# 2. 修改 gitlab.yml
vi /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml

# http
# host:改成自己ip地址或域名
# port:改成自己端口号
# ssh
# ssh_port:改成自己端口号