当前位置:   article > 正文

Linux下安装GitLab仓库,史上最详细的教程来啦~_linux 安装git仓库 页面访问

linux 安装git仓库 页面访问

创作不易,如果觉得这篇文章对你有帮助,欢迎各位老铁点个赞呗,您的支持是我创作的最大动力!

前言

随着现在git使用的流行,现在项目版本控制工具,基本上从svn过渡到了git,这里,博主总结一下Git仓库的环境搭建过程。

本文主要以GitLab为例,搭建自己的Git仓库。当然啦,如果服务器配置不行,还是乖乖的使用轻量级的Gogs搭建git仓库吧,具体搭建过程,可以参考我另一篇博客:Linux下使用Gogs搭建Git仓库详细教程

好了,话不多说,直接进入主题。

1 确认GitLab需要使用的安装包类型

因为gitlab有很多安装包类型,首先需要确认你的服务器类型,然后根据你的服务器版本,来确认使用哪种安装包。
在这里插入图片描述

笔者以Linux服务器为例。

查看Linux系统版本,来确认gitlab需要使用的安装包类型
使用命令:cat /etc/redhat-release

CentOS Linux release 7.6.1810 (Core)
  • 1

我的服务器系统是CentOS 7,对应官方教程地址:https://about.gitlab.com/install/#centos-7

如果你的是Ubantu或者其他版本,请参考官网教程进行安装。

2 系统配置要求

We strongly recommend downloading the Omnibus package installation since it is quicker to install, easier to upgrade, and it contains features to enhance reliability not found in other methods.
We also strongly recommend at least 4GB of free RAM to run GitLab.

安装GitLab仓库,官方要求配置:https://docs.gitlab.com/ee/install/requirements.html#cpu
在这里插入图片描述
官方要求,至少需要2核CPU4GB RAM4GB swap,说白了,你的服务器配置至少是2核4g的,低于这个配置的,就玩不了了。

博主的服务器配置刚好是2核4g的,swap空间虽然查了点,勉强可以玩一玩。但是由于我在服务器配置的有其他东西,所以有时候GitLab服务就会挂掉,所以说,GitLab是比较吃资源的。建议2核8g以上的配置,再使用GitLab作为git仓库。

3 安装GitLab

3.1 安装 curl,policycoreutils-python,openssh-server服务

使用命令:yum install -y curl policycoreutils-python openssh-server

出现以下界面,说明安装成功:
在这里插入图片描述

3.2 将SSH服务设置成开机自启动

使用命令:systemctl enable sshd

3.3 启动SSH服务

使用命令:systemctl start sshd

以下界面说明,启动sshd服务成功。
在这里插入图片描述

3.4 添加http服务和https服务到firewalld

gitlab服务需要使用http协议和https协议,所以需要开放http和开放https,当然,前提是防火墙开着的(防火墙通常都是开启的),如果服务没有开启防火墙,不需要设置这个。

添加http服务到firewalld,允许http服务和https服务通过,使用命令:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https

参数说明:
pemmanent表示永久生效,若不加--permanent,系统下次启动后就会失效

3.5 让防火墙配置生效

使用命令:systemctl reload firewalld

3.6 安装postfix并将postfix服务设置成开机自启动

安装postfix,是用来发送通知邮件的,比如你给某个成员开放了一个项目权限,该成员就可以接收到邮件通知。

Linux Centos查看postfix已经安装,如果安装过,则无需安装。
使用命令:rpm -qa | grep postfix

安装postfix
使用命令:sudo yum install postfix,sudo可以不写

设置postfix自启动
使用命令:sudo systemctl enable postfix
  
笔者不推荐使用postfix发送邮件,postfix相对来说比较复杂一些。
官网有很丰富的邮箱服务配置,进行邮件通知,可以参考下文中第4部分内容的介绍,配置邮件通知(smtp方式)

如果就想使用postfix发送邮件,可以参考网上资料进行配置:https://www.cnblogs.com/flytor/p/11447686.html

查看postfix发送邮件记录:
使用命令:cat /var/mail/log

3.7 启动postfix服务

使用命令:sudo systemctl start postfix

可能会启动失败:

Job for postfix.service failed because the control process exited with error code. See "systemctl status postfix.service" and "journalctl -xe" for details.
  • 1

这是因为防火墙或者配置文件导致,无法启动的邮件服务,修改配置文件:
使用命令:vim /etc/postfix/main.cf
inet_interfaces = localhost注释掉,把inet_interfaces = all的注释打开,然后就可以启动了。
在这里插入图片描述
如果还不能启动postfix,可能是让防火墙配置未生效,再启动应该就可以启动了:
使用命令刷新防火墙配置:systemctl reload firewalld

查看postfix服务是否启动成功:
可以通过命令:systemctl status postfix 或者 ps -ef | grep postfix

下图所示标识启动成功:
在这里插入图片描述

3.8 下载GitLab

3.8.1 下载GitLab镜像

  • gitlab-ee和gitlab-ce两种镜像的区别
    关于gitlab-ee和gitlab-ce,二者是基于同样的核心代码进行开发,只是gitlab-ee(企业版)功能更强大,但需要付费使用,有30天试用期。但试用期过后如果不付费,它就跟gitlab-ce(社区版)功能是完全一样的,只是需要付费的功能无法再继续使用而已,所以这两个版本可以随意选择安装,但如果将来有付费的打算,直接安装gitlab-ee版本是个有远见的选择。当然,即使不付费,gitlab-ee使用上和gitlab-ce没有任何区别。

    所以接下来的操作我们就以安装gitlab-ee为例进行。

  • 下载gitlab-ee镜像
    使用命令下载镜像:curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
    这里默认下载的最新版本。

    出现下图所示,标识gitlab reposity镜像下载成功:
    在这里插入图片描述

    curl命令执行完成后,会在/etc/yum.repos.d目录下生成 gitlab_gitlab-ee.repo
    gitlab_gitlab-ce.repo文件。
    在这里插入图片描述

  • 使用国内镜像(如果你的服务器网速很好不需要翻墙,这个步骤可以省略)
    如果网速不好,可以使用国内镜像,修改文件:
    使用命令:vim /etc/yum.repos.d/gitlab_gitlab-ee.repo

    repo_gpgcheck属性设置为0,以及baseurl属性设置为清华大学的开源镜像地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ee/yum/el7

    原始文件内容:

    [gitlab_gitlab-ee]
    name=gitlab_gitlab-ee
    baseurl=https://packages.gitlab.com/gitlab/gitlab-ee/el/7/$basearch
    repo_gpgcheck=1
    gpgcheck=1
    enabled=1
    gpgkey=https://packages.gitlab.com/gitlab/gitlab-ee/gpgkey
           https://packages.gitlab.com/gitlab/gitlab-ee/gpgkey/gitlab-gitlab-ee-3D645A26AB9FBD22.pub.gpg
    sslverify=1
    sslcacert=/etc/pki/tls/certs/ca-bundle.crt
    metadata_expire=300
    
    [gitlab_gitlab-ee-source]
    name=gitlab_gitlab-ee-source
    baseurl=https://packages.gitlab.com/gitlab/gitlab-ee/el/7/SRPMS
    repo_gpgcheck=1
    gpgcheck=1
    enabled=1
    gpgkey=https://packages.gitlab.com/gitlab/gitlab-ee/gpgkey
           https://packages.gitlab.com/gitlab/gitlab-ee/gpgkey/gitlab-gitlab-ee-3D645A26AB9FBD22.pub.gpg
    sslverify=1
    sslcacert=/etc/pki/tls/certs/ca-bundle.crt
    metadata_expire=300
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    修改后的内容:

    
    [gitlab_gitlab-ee]
    name=gitlab_gitlab-ee
    #baseurl=https://packages.gitlab.com/gitlab/gitlab-ee/el/7/$basearch
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ee/yum/el7
    repo_gpgcheck=0
    gpgcheck=1
    enabled=1
    gpgkey=https://packages.gitlab.com/gitlab/gitlab-ee/gpgkey
           https://packages.gitlab.com/gitlab/gitlab-ee/gpgkey/gitlab-gitlab-ee-3D645A26AB9FBD22.pub.gpg
    sslverify=1
    sslcacert=/etc/pki/tls/certs/ca-bundle.crt
    metadata_expire=300
    
    [gitlab_gitlab-ee-source]
    name=gitlab_gitlab-ee-source
    #baseurl=https://packages.gitlab.com/gitlab/gitlab-ee/el/7/SRPMS
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ee/yum/el7
    repo_gpgcheck=0
    gpgcheck=1
    enabled=1
    gpgkey=https://packages.gitlab.com/gitlab/gitlab-ee/gpgkey
           https://packages.gitlab.com/gitlab/gitlab-ee/gpgkey/gitlab-gitlab-ee-3D645A26AB9FBD22.pub.gpg
    sslverify=1
    sslcacert=/etc/pki/tls/certs/ca-bundle.crt
    metadata_expire=300
    
    
    • 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

    修改内容部分截图:
    在这里插入图片描述

    注意事项以及配置文件说明:
    a、gitlab_gitlab-eegitlab_gitlab-ee-source对应的baseurl地址都要替换为清华大学开源镜像站
    b、name是仓库的名字
    c、baseurl字段是仓库的url,也就是仓库具体的访问路径,就是通过这个选项告知yum,仓库在哪。这里可以指明多个url,如果写了多个url,yum将遵循failovermethod字段进行使用
    d、enabled字段指明了该url是否启用,0为不启用,默认为1
    e、gpgcheck字段指明了是否检查rpm包的签名信息、完整性信息,0为不启用,1为启用
    (注意:如果启用这个字段,也就是为1时,如果没有gpgkey时,检查很可能无法通过。我们可以在命令行中使用-- nogpgcheck选项禁止检查gpg)
    f、repo_gpgcheck指明了是否检查元数据信息文件的签名信息与完整性,0为不启用,1为启用
    g、gpgkey=url指明了密钥文件的url路径,有了这个密钥才能检查各种完整信息。
    h、由于gpgkey对应的地址需要翻墙,所以gpgcheck也需要设置为0,如果你服务器不需要翻墙,可以使用默认值1。
    i、https://mirrors.tuna.tsinghua.edu.cn是国内清华大学开源镜像站,避免yum install可能会因为网络原因导致失败。

3.8.2 手动安装镜像

这个步骤和3.8.1执行一个即可。如果你选则了这里的手动安装方式,可以跳过3.9步骤。

除了上面,使用curl安装镜像之外,当然,可以手动下载rpm包:
https://packages.gitlab.com/gitlab
https://packages.gitlab.com/gitlab/gitlab-ee

  • rpm版本选择
    EL is short for Red Hat Enterprise Linux (EL).

    EL6 is the download for Red Hat 6.x, CentOS 6.x, and CloudLinux 6.x.
    EL5 is the download for Red Hat 5.x, CentOS 5.x, CloudLinux 5.x.
    EL7 is the download for Red Hat 7.x, CentOS 7.x, and CloudLinux 7.x.

    根据你的服务器版本,选择合适的rpm包即可。

  • 使用rpm包安装

    官网下载链接:https://packages.gitlab.com/gitlab/gitlab-ee/packages/el/7/gitlab-ee-13.0.0-ee.0.el7.x86_64.rpm

    可以直接使用wget命令下载:
    wget --content-disposition https://packages.gitlab.com/gitlab/gitlab-ee/packages/el/7/gitlab-ee-13.0.0-ee.0.el7.x86_64.rpm/download.rpm

    使用命令: rpm -ivh gitlab-ee-13.0.0-ee.0.el7.x86_64.rpm 直接安装rpm包的软件即可。(vh显示安装进度)

  • 也可以使用以下命令,直接安装镜像
    sudo yum install -y gitlab-ee-13.0.0-ee.0.el7.x86_64

3.9 安装GitLab

Next, install the GitLab package. Change https://gitlab.example.com to the URL at which you want to access your GitLab instance. Installation will automatically configure and start GitLab at that URL.

For https:// URLs GitLab will automatically request a certificate with Let’s Encrypt, which requires inbound HTTP access and a valid hostname. You can also use your own certificate or just use http://.

sudo EXTERNAL_URL="https://gitlab.example.com" yum install -y gitlab-ee
  • 1

上面官网说明的,可以安装的同时,配置git仓库访问的域名,要求域名是有效的。这里,由于博主还没有域名,暂时不配置域名访问。

使用以下命令安装即可:
yum install -y gitlab-ee

出现下图所示,标识安装成功:
在这里插入图片描述

3.10 修改GitLab配置文件

指定服务器ip和自定义端口

使用命令:
vim /etc/gitlab/gitlab.rb

使external_url指向自己的ip:external_url=“http://ip:8888”

3.11 重新加载GitLab配置文件

使用命令:gitlab-ctl reconfigure

这个过程有点慢,因为会启动部分核心服务,请耐心等待哈。

出现下图所示,说明reconfigure finished:
在这里插入图片描述

3.12 启动GitLab

使用命令:gitlab-ctl start

启动成功如下图所示:
在这里插入图片描述

输入请求http://ip:8888,如果端口号不是8888,修改为你的端口号,访问gitlab首页如下图所示:
在这里插入图片描述

注意:
访问时,有可能访问不到,如果是阿里云服务,需要配置安全组:
在这里插入图片描述

如果你的服务器或者虚拟机开启了防火墙,需要开放8888端口:

  • 开启8888防火墙端口
    firewall-cmd --zone=public --add-port=8888/tcp --permanent

  • 重新加载防火墙配置
    firewall-cmd --reload

好啦,童鞋们,可以愉快的玩耍了。
在这里插入图片描述

4 配置邮件通知(smtp方式)

3.6步骤和3.7步骤使用postfix方式发送邮件通知,当然啦,gilab官网给出了很丰富的邮箱服务配置,不使用postfix(可以跳过3.6步骤和3.7步骤)也是可以发送邮件通知的,具体的可以参考官网进行配置:https://docs.gitlab.com/omnibus/settings/smtp.html

If you want to use another solution to send emails please skip this step and configure an external SMTP server after GitLab has been installed.

If you would rather send application email via an SMTP server instead of via Sendmail, add the following configuration information to /etc/gitlab/gitlab.rb and run gitlab-ctl reconfigure.
  • 1
  • 2
  • 3

笔者使用的是stmp方式:

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

 gitlab_rails['smtp_enable'] = true
 gitlab_rails['smtp_address'] = "smtp.qq.com"
 gitlab_rails['smtp_port'] = 465
 gitlab_rails['smtp_user_name'] = "******@qq.com"
 gitlab_rails['smtp_password'] = "******授权码******"
 gitlab_rails['smtp_domain'] = "smtp.qq.com"
 gitlab_rails['smtp_authentication'] = "login"
 gitlab_rails['smtp_enable_starttls_auto'] = true
 gitlab_rails['smtp_tls'] = true
 gitlab_rails['gitlab_email_from'] = '******@qq.com'  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

刷新配置并重启生效:
gitlab-ctl reconfigure
gitlab-ctl restart

执行命令:gitlab-rails console进入控制台。 然后在控制台提示符后输入下面的命令,发送一封测试邮件:Notify.test_email('收件人邮箱', '邮件标题', '邮件正文').deliver_now

当你收到一封邮件时,说明邮件通知配置成功。

5 GitLab常用命令总结

  • 修改默认的配置文件
    使用命令:sudo vim /etc/gitlab/gitlab.rb

  • 检查gitlab配置
    使用命令:gitlab-rake gitlab:check SANITIZE=true --trace

  • 重新加载gitlab配置文件,启动部分核心服务
    使用命令:sudo gitlab-ctl reconfigure

  • 查看服务状态
    使用命令:sudo gitlab-ctl status

  • 启动所有gitlab组件
    使用命令:sudo gitlab-ctl start

  • 停止所有gitlab组件
    使用命令:sudo gitlab-ctl stop

  • 重启所有gitlab组件
    使用命令:sudo gitlab-ctl restart

  • 查看日志
    使用命令:sudo gitlab-ctl tail

    说明:以上命令中,sudo可以省略。
    Linux sudo命令以系统管理者的身份执行指令,也就是说,经由 sudo 所执行的指令就好像是 root 亲自执行。

6 进入GitLab控制台

输入命令:gitlab-rails console进入gitlab控制台,只有进入到控制台之后,才可以输入gitlab的查询语句,才会被解析。

  • 查询所有的用户:
    使用命令:user = User.all

  • 查找用户信息
    通过条件查询用户 常见的where条件有 usernameemailstate

    查找用户信息
    user = User.where(id:1).first
    user = User.find_by(email: 'admin@local.host')

    通过id查询用户:
    user = User.find(1)

    查询用户某个字段的值,比如,显示当前用户的email:
    user.email

  • 修改用户的信息

    重置密码:
    输入user.password='new_password',密码位置填写您新的密码,然后再输入user.save保存修改的用户信息即可。
     
    修改用户状态:
    user.state = 'active'
    user.save

7 GitLab自启动设置

  • 禁止 GitLab 开机自启动
    systemctl disable gitlab-runsvdir.service
  • 启用GitLab开机自启动
    systemctl enable gitlab-runsvdir.service

8 安装过程可能出现的问题和修复方法

安装出现问题,通常是网络不好,或者是卸载不干净导致的安装失败。

  • 1502 Whoops, GitLab is taking too much time to respond

    解决方案:
    修改默认端口:需要放开/etc/gitlab/gitlab.rb文件里面nginx['listen_port']unicorn['port']注释并设置端口,两个端口不能相同.具体原因笔者暂时也搞不懂,没用过nginx。

    nginx['listen_port'] = 1001
    unicorn['port'] = 1002
    
    • 1
    • 2

    可能是端口占用问题,unicorn

    刷新gitlab配置和重新启动

    gitlab-ctl reconfigure
    gitlab-ctl restart
    
    • 1
    • 2
  • STDERR: sysctl: cannot open "/etc/sysctl.d/90-omnibus-gitlab-net.core.somaxconn.conf": No such file or directory ---- End output of sysctl -e --system ---- Ran sysctl -e --system returned 255

    解决方案:
    运行命令:sudo systemctl restart gitlab-runsvdir
    再次执行:sudo gitlab-ctl reconfigure

  • 出现ruby_block[supervise_redis_sleep] action run停留很久
    在卸载gitlab,然后再次安装执行gitlab-ctl reconfigure的时候可能会出现:ruby_block[supervise_redis_sleep] action run,会一直卡无法往下进行!

    解决方案:
    先按住Ctrl+C强制结束,然后运行命令:sudo systemctl restart gitlab-runsvdir
    再次执行:sudo gitlab-ctl reconfigure

  • could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/opt/gitlab/postgresql/.s.PGSQL.5432"?

    解决方案:

    1、按住CTRL+C强制结束
    2、先停止 gitlab ,命令: sudo gitlab-ctl stop
    3、执行命令: sudo chmod 755 /var/opt/gitlab/postgresql
    4、执行命令:sudo systemctl restart gitlab-runsvdir
    5、再次配置:sudo gitlab-ctl reconfigure
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • gitlab reconfigure 卡住 ruby_block[wait for redis service socket] action run然后等了很久也没有看到这个控制台输出的日志信息发生变化
    解决方案:

    在另外开启一个终端启动如下命令:
    sudo /opt/gitlab/embedded/bin/runsvdir-start
    
    或者在当前的终端窗口中后台执行上述命令:
    nohup /opt/gitlab/embedded/bin/runsvdir-start &
    
    然后再执行:sudo gitlab-ctl reconfigure
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

9 完全卸载删除GitLab

有时候可能需要删除原来的版本,重装gitlab,比如说博主,为了写这篇博客,还专门把自己之前的gitlab仓库删除了

完全卸载gitlab步骤:

  • 停止gitlab
    使用命令:gitlab-ctl stop

  • 卸载gitlab(注意这里写的是gitlab-ee)
    使用命令:rpm -e gitlab-ee

  • 查看并删除gitlab进程
    使用命令:ps -ef | grep gitlab
    如果有进程,就杀掉(kill -9 pid),没有的话进行下一步

    杀掉后,在使用ps -aux | grep gitlab确认一遍,还有没有gitlab的进程

  • 删除所有包含gitlab的文件
    使用命令:find / -name gitlab | xargs rm -rf

    xargs 参数说明:
    xargs 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。
    xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。
    xargs 是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令。

辛苦两个晚上,终于创作了这篇博文,可以说是最详细的GitLab安装教程了,老铁们,如果这篇文章对你有帮助,点个赞支持下呗!

官方资料:https://about.gitlab.com/install/#centos-7

参考资料:https://www.jianshu.com/p/d03a08dd28a2

写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,希望尽自己的努力,做到更好,大家一起努力进步!

如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!

给自己的梦想添加一双翅膀,让它可以在天空中自由自在的飞翔!

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号