自己搭建Gitlab服务器

更换yum源

为了加快速度,首先可以更换yum的源

1
[root@iZ94tq694y3Z ghost]#touch /etc/yum.repos.d/gitlab_gitlab-ce.repo

替换内容为:

1
2
3
4
5
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

安装

用root用户安装:

1
2
3
4
5
6
7
yum install curl openssh-server openssh-clients postfix cronie
service postfix start
chkconfig postfix on

yum makecache
yum install gitlab-ce
gitlab-ctl reconfigure

由于自己的阿里云服务器太渣渣了,所以在这里一直卡主了。看了一下阿里云的服务监控。内存已经爆满了,不得不去花钱升级了一下配置。现在是1CPU and 2GB内存。

继续执行,还是报错了。

1
2
3
4
Running handlers:
There was an error running gitlab-ctl reconfigure:

execute[semodule -i /opt/gitlab/embedded/selinux/rhel/7/gitlab-7.2.0-ssh-keygen.pp] (gitlab::selinux line 20) had an error: Errno::ENOMEM: execute[Guard resource] (dynamically defined) had an error: Errno::ENOMEM: Cannot allocate memory - fork(2)

查看内存

1
2
3
4
5
[root@iZ94tq694y3Z ghost]# free  -m
total used free shared buffers cached
Mem: 1841 1760 80 52 5 76
-/+ buffers/cache: 1678 162
Swap: 0 0 0

解决Cannot allocate memory - fork问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@iZ94tq694y3Z swapfile]# mkdir /swapfile
[root@iZ94tq694y3Z swapfile]# cd /swapfile
[root@iZ94tq694y3Z swapfile]# dd if=/dev/zero of=swap bs=1024 count=2000000
2000000+0 records in
2000000+0 records out
2048000000 bytes (2.0 GB) copied, 12.8547 s, 159 MB/s
[root@iZ94tq694y3Z swapfile]# mkswap -f swap
Setting up swapspace version 1, size = 1999996 KiB
no label, UUID=da70ea74-4bac-484a-9c14-2c20e265c267
[root@iZ94tq694y3Z swapfile]# swapon swap
swapon: /swapfile/swap: insecure permissions 0644, 0600 suggested.
[root@iZ94tq694y3Z swapfile]# free -h
total used free shared buffers cached
Mem: 1.8G 1.7G 68M 52M 1.2M 85M
-/+ buffers/cache: 1.6G 155M
Swap: 1.9G 0B 1.9G

再重新执行gitlab-ctl reconfigure成功

1
2
3
4
Running handlers:
Running handlers complete
Chef Client finished, 35/743 resources updated in 01 minutes 31 seconds
gitlab Reconfigured!

继续配置:gitlab-rails console production

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@iZ94tq694y3Z swapfile]# gitlab-rails console production
DEPRECATION WARNING: Passing the environment's name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -e option instead. (called from require at bin/rails:4)
--------------------------------------------------------------------------------
GitLab: 12.3.5 (2417d5becc7)
GitLab Shell: 10.0.0
PostgreSQL: 10.9
--------------------------------------------------------------------------------
Loading production environment (Rails 5.2.3)
irb(main):001:0>
irb(main):002:0> user = User.where(id:1).first
=> #<User id:1 @root>
irb(main):003:0> 'chg''xxxxxx'
=> "xxxxxx"
irb(main):004:0> user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: c9f8831f-25c1-429c-bc3a-073a2a1e5fb8) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", #<GlobalID:0x00007fa89b887368 @uri=#<URI::GID gid://gitlab/User/1>>
=> true
irb(main):005:0>

配置域名

所有gitlab的配置都在/etc/gitlab/gitlab.rb了里面。我这里只修改了其中的几项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
web_server['external_users'] = ['root'] #支持的用户

nginx['enable'] = false # 不用自带的nginx,用自己安装的

external_url 'https://gitlab.cyblogs.com' # 配置域名

unicorn['port'] = 8081 # 服务端口号

# 配置邮箱
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.sina.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "chengcheng222e@sina.com"
gitlab_rails['smtp_password'] = "xxxxxx"
gitlab_rails['smtp_domain'] = "sina.com"
gitlab_rails['smtp_authentication'] = "plain"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false

配置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
[root@iZ94tq694y3Z conf.d]# cat gitlab.cyblogs.com.conf 

# 这里需要通过域名解析到这里来,大家自己去搜索相关的教程
upstream gitlab.cyblogs.com {
server 127.0.0.1:8081;
}

server {
listen 80;
server_name gitlab.cyblogs.com ;

location / {
root html;
index index.html index.htm;
proxy_pass http://gitlab.cyblogs.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect HOST default;
}

location /assets {
root /opt/gitlab/embedded/service/gitlab-rails/public;
}

error_page 404 /404.html;
error_page 500 /500.html;
error_page 502 /502.html;

location ~ ^/(404|500|502)(-custom)?\.html$ {
root /opt/gitlab/embedded/service/gitlab-rails/public;
internal;
}
}

GitLab备份和恢复

1
2
# 可以将此命令写入crontab,以实现定时备份
/usr/bin/gitlab-rake gitlab:backup:create

Gitlab完全卸载

1
2
3
4
5
6
# 停止gitlab
gitlab-ctl stop
# 删除
rpm -e gitlab-ee
# 彻底删除
find / -name gitlab | xargs rm -rf

GitLab常用命令

1
2
3
4
5
6
7
8
9
gitlab-ctl start    # 启动所有 gitlab 组件;
gitlab-ctl stop # 停止所有 gitlab 组件;
gitlab-ctl restart # 重启所有 gitlab 组件;
gitlab-ctl status # 查看服务状态;
vim /etc/gitlab/gitlab.rb # 修改gitlab配置文件;
gitlab-ctl reconfigure # 重新编译gitlab的配置;
gitlab-rake gitlab:check SANITIZE=true --trace # 检查gitlab;
gitlab-ctl tail # 查看日志;
gitlab-ctl tail nginx/gitlab_access.log

验证

浏览器输入:http://gitlab.cyblogs.com/users/sign_in

http://static.cyblogs.com/WX20191014-222324@2x.png

还有很多的配置还未配置,有待慢慢研究~

参考地址:

如果大家喜欢我的文章,可以关注个人订阅号。欢迎随时留言、交流。

简栈文化服务订阅号