为了满足自己一些业余项目的版本管理需要,搭建了自己的gitlab服务器。本文记录了搭建过程以及其中遇到的一些问题和解决方法。中间参考了官方文档和很多前辈的blog,感谢。也感谢腾讯云提供的免费服务器^ - ^。
云服务器配置要求
https://docs.gitlab.com/ee/install/requirements.html
参考gitlab的官方文档,需要关注Memory一条,实际使用中发现需要2GB RAM + 2G swap才能正常安装、运行gitlab。
免费服务器申请、配置
目前我使用的是腾讯云提供的免费云服务器申请链接,1核/1GB/1Mbps/50GB,操作系统使用的是ubuntu16.04.1,目前腾讯有提供活动,个人用户只需要1元钱即可试用1个月(比较适合用于学习,后面长期使用还需要再挑选合适的服务商,哪位朋友有合适的欢迎推荐)。
服务器设置
swap设置
需要注意的是默认系统配置中没有使用swap,之前没有接触过服务器端不清楚这是不是云服务器的普遍配置方式。
安装、配置gitlab时内存开销会大于1G,在目前的服务器上出现out of memory的问题,选择配置swapfile添加swap空间的方式来解决内存不足的问题。swap空间一般设置为物理内存的一倍(或两倍)大小。
Step 1. 创建2GB大小的空文件
sudo dd if='/dev/zero of=/root/swapfile bs=1024 count=2048
Step 2. 制作swap文件并生效
sudo mkswap /root/swapfile
sudo swapon /root/swapfile
Step 3. 设置开机自动挂载
sudo vim /etc/fstab
将下面信息添加为最后一行
/root/swapfile swap swap defaults 0 0
使用top命令或者free -m来查看swap设置是否生效
内存大小调整
为了解决超时问题,需要将服务器的内存大小改为2GB,在腾讯云服务器实例的操作列表中选调整配置,可以免费将内存大小调整为2GB。顺利解决^ - ^
超时问题
添加swap后,gitlab-ctl reconfigure不再出现out of memory问题,但在后续操作和使用中会出现超时问题,个人猜测是由于大量使用swap空间(物理内存剩余只有60MB左右),导致服务运行速度过慢导致。
出现超时的几个地方:
- 在ruby_block action run一步会出现超时
Running handlers: There was an error running gitlab-ctl reconfigure:ruby_block[authorize Grafana with GitLab] (monitoring::grafana line 95) had an error: Mixlib::ShellOut::CommandTimeout: Command timed out after 600s: Command exceeded allowed execution time, process terminated
---- Begin output of /opt/gitlab/bin/gitlab-rails runner -e production ‘app = Doorkeeper::Application.where(redirect_uri: “http://xx.xx.xx.xx/-/grafana/login/gitlab”, name: “GitLab Grafana”).first_or_create;puts app.uid.concat(" ").concat(app.secret);’ ---- STDOUT:
STDERR:
---- End output of /opt/gitlab/bin/gitlab-rails runner -e production ‘app = Doorkeeper::Application.where(redirect_uri: “http://xx.xx.xx.xx/-/grafana/login/gitlab”, name: “GitLab Grafana”).first_or_create;puts app.uid.concat(" ").concat(app.secret);’
---- Ran /opt/gitlab/bin/gitlab-rails runner
-e production ‘app = Doorkeeper::Application.where(redirect_uri: “http://xx.xx.xx.xx/-/grafana/login/gitlab”, name: “GitLab Grafana”).first_or_create;puts app.uid.concat(" ").concat(app.secret);’ returned
- 虽然上面一步出现超时,测试发现已经可以通过浏览器访问Gitlab服务器,并且可以进入到注册页面,但提交注册时出现返回超时而用户可以成功注册的情况。sudo gitlab-rake gitlab:env:info 正常返回配置信息,页面返回虽然超时但操作可以正常完成,这也是我判断可能是服务运行过慢导致超时的原因。
- 在登录时反复超时,无法正常使用。
将服务器内存配置改为2GB后解决超时问题。
Gitlab install
安装过程参考官方文档
-
安装、配置必要依赖
sudo apt-get update sudo apt-get install -y curl openssh-server ca-certificates
-
安装邮件服务(这步先跳过,后面再补)
sudo apt-get install -y postfix
-
添加gitlab源并安装
添加源curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash
安装
sudo EXTERNAL_URL="host_url_or_ip:port" apt-get install gitlab-ee
在没有url的情况下,直接使用IP来取代上述命令中的 host_url_or_ip,用 “ ” 将IP包起来(不包没试过),端口默认使用8080,也可以设置自己需要的端口。
-
上述命令会依次执行安装、配置两步。目前碰到的问题都是出现在配置一步(如上面提到的内存大小导致的问题),如果出现类似以下问题:
Running handlers: There was an error running gitlab-ctl reconfigure:
bash[migrate gitlab-rails database] (gitlab::database_migrations line
51) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected
process to exit with [0], but received ‘1’可以尝试以下解决方法:
sudo gitlab-ctl stop sudo chmod 755 /var/opt/gitlab/postgresql sudo systemctl restart gitlab-runsvdir sudo gitlab-ctl reconfigure sudo gitlab-ctl restart
-
访问服务并注册
首次访问服务时,需要设置管理员账号的密码,默认管理员ID为 root。 -
限制用户注册
Gitlab默认是没有注册限制的,也就是说任何人只要登录了xx.xx.xx.xx/users/sign_in页面,就可以使用注册功能进行注册,因此我们需要对用户注册进行限制。Gitlab提供了如白名单、黑名单等比较丰富的注册限制功能。需要使用管理员账号(root)登录,进入Admin Area。
详细信息请参考官方文档。
由于我搭建的服务仅限自己和两三个好友使用(目前我理解的Gitlab白名单功能需要有私有域名的邮箱才能使用),因此我关闭了注册功能,直接在管理面板中添加用户。
TODO
- email通知
- ssh
来源:CSDN
作者:iyouju
链接:https://blog.csdn.net/iyouju/article/details/104114951