目录

  • 前言
  • 一、搭建安装
  • (一)搭建环境要求
  • (二)搭建过程
  • 1、直接下载安装,自动安装
  • 2、下载安装包,手动安装
  • 二、配置邮箱
  • 三、常用操作命令
  • (一)基操
  • (二)检查服务的日志信息
  • (三)检查服务状态
  • 四、遇到的问题及解决方案
  • 附注


前言

由于目前使用的版本控制工具SourceTree满足不了现在项目组的项目管理需求,为了方便项目组提交代码及合并分支,所以在我们本地的服务器(18.04)上搭建了自己的Gitlab网站。

一、搭建安装

(一)搭建环境要求

gitlab-ce安装要求:

https://docs.gitlab.com/ce/install/requirements.html gitlab-ee安装要求:

https://docs.gitlab.com/ee/install/requirements.html

云服务器安装gitlab内存占满 gitlab本地服务器搭建_git


存储器要求7200转的机械盘,或者固态SSD。

云服务器安装gitlab内存占满 gitlab本地服务器搭建_ubuntu_02


这里推荐四核心处理器,至少4g内存,可以支持五百人以上。

去年我看的是至少两核心处理器,嗯~

注意下哈,这里内存一定要留足4g的空间,不然安好了也用不了,会报502错误。就像这样子

云服务器安装gitlab内存占满 gitlab本地服务器搭建_git_03

(二)搭建过程

这里注意一下啊,gitlab-ce(社区版),有些功能没有gitlab-ee(企业版)全。当然,选择gitlab-ee可以试用一些付费功能,在试用期结束后,还是要掏钱的。所以,作为一个理财小能手(qiongB)。我选择gitlab-ce,而且社区版也更容易在遇到问题时,在gitlab社区内找到合适的解决方案。

1、直接下载安装,自动安装

这里大家可以参考


gitlab-ee 举例

For Ubuntu 20.04, arm64 packages are also available.

1.Install and configure the necessary dependencies
初始化配置必要的依赖项

sudo apt-get update
sudo apt-get install -y curl openssh-server ca-certificates tzdata

Next, install Postfix to send notification emails. 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.
接下来去配置Postfix邮件服务。如果你想要其他的邮件服务发送,可以跳过这一步,在完成Gitlab安装后配置外部的SMTP邮件服务。(可选配置,我这里没用到)

sudo apt-get install -y postfix

During Postfix installation a configuration screen may appear. Select ‘Internet Site’ and press enter. Use your server’s external DNS for ‘mail name’ and press enter. If additional screens appear, continue to press enter to accept the defaults.

2.Add the GitLab package repository and install the package
Add the GitLab package repository.

下载安装Gitlab

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/ | sudo bash

Next, install the GitLab package. Make sure you have correctly set up your DNS, and change https:// 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://.

这一步是配置你的gitlab服务地址的,也可以安装后在/etc/gitlab/gitlab.rb文件里修改

sudo EXTERNAL_URL="https://" apt-get install gitlab-ee

3.Browse to the hostname and login
On your first visit, you’ll be redirected to a password reset screen. Provide the password for the initial administrator account and you will be redirected back to the login screen. Use the default account’s username root to login.

See our documentation for detailed instructions on installing and configuration.

4.Set up your communication preferences
Visit our email subscription preference center to let us know when to communicate with you. We have an explicit email opt-in policy so you have complete control over what and how often we send you emails.

Twice a month, we send out the GitLab news you need to know, including new features, integrations, docs, and behind the scenes stories from our dev teams. For critical security updates related to bugs and system performance, sign up for our dedicated security newsletter.

Important note: If you do not opt-in to the security newsletter, you will not receive security alerts.

5. Recommended next steps
After completing your installation, consider the recommended next steps, including authentication options and sign-up restrictions.


2、下载安装包,手动安装

上面的官方搭建步骤,是搭建的gitlab-ee版本的。咱们这里用自己的方式来搭建gitlab-ce版本。

(1)下载安装包

官方地址:https://packages.gitlab.com/gitlab 清华大学镜像:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/bionic/main/g/gitlab-ce/

我这里用的是去年下载的 gitlab-ce_11.10.8-ce.0_amd64

(2)通过工具上传服务器

cd /usr/local
mkdir gitlab
cd /usr/local/gitlab

云服务器安装gitlab内存占满 gitlab本地服务器搭建_ubuntu_04


(3)更新源

sudo apt-get update

云服务器安装gitlab内存占满 gitlab本地服务器搭建_云服务器安装gitlab内存占满_05

(4)安装依赖

apt-get install -y curl openssh-server ca-certificates

云服务器安装gitlab内存占满 gitlab本地服务器搭建_git_06

(5)安装SMTP发送邮件软件(可选)

sudo apt-get install -y postfix

(6)进入压缩包文件夹,并且解压

dpkg -i gitlab-ce_11.10.8-ce.0_amd64.deb

云服务器安装gitlab内存占满 gitlab本地服务器搭建_postgresql_07


解压成功后,会出现小狐狸

云服务器安装gitlab内存占满 gitlab本地服务器搭建_gitlab_08


到了这一步,成功一半了。

(7)修改服务器地址,vim /etc/gitlab/gitlab.rb

云服务器安装gitlab内存占满 gitlab本地服务器搭建_gitlab_09

用这种方式也可以配置访问端口:external_url 'http://192.168.0.110:60000'

同时也可以配置域名,gitlab设置external_url 默认不添加端口是占用80端口的,如果80端口被占用,一定要修改启动端口哦。

(8)保存后,使用gitlab-ctl reconfigure重加载配置,时间可能稍久。

启动好后,访问我们配置的端口,就可以看到下面初始化管理员(root)密码的页面了。

云服务器安装gitlab内存占满 gitlab本地服务器搭建_云服务器安装gitlab内存占满_10


上面是正常的配置了,不正常的像下面

云服务器安装gitlab内存占满 gitlab本地服务器搭建_ubuntu_11


这个出现的问题可能是:

  1. 硬件原因,服务器内存不够,达不到软件安装的最低配置。
  2. 相关的端口被占用了,比如80,8080热门端口被占用,这些可以通过把占用的端口kill掉,也可以修改配置文件gitlab.rb来修改端口号。这个具体参考本文:

四、遇到的问题及解决方案

二、配置邮箱

由于我们前面没有配置poxfix,所以我们接下来开始配置外部邮件服务SMTP,我这里是参考官网配置的腾讯邮箱服务。

https://docs.gitlab.com/omnibus/settings/smtp.html#qq-exmail 其他邮箱参考https://docs.gitlab.com/omnibus/settings/smtp.html

(自己找自己需要的邮箱服务)

云服务器安装gitlab内存占满 gitlab本地服务器搭建_git_12

云服务器安装gitlab内存占满 gitlab本地服务器搭建_postgresql_13


1、配置邮箱

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = ""
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "aaa@"
gitlab_rails['smtp_password'] = "password"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = 'aaa@'
gitlab_rails['smtp_domain'] = "exmail."

add the following configuration information to /etc/gitlab/gitlab.rb and run gitlab-ctl reconfigure.
添加配置到gitlab.rb中,并且重加载配置。

2、测试邮箱服务

云服务器安装gitlab内存占满 gitlab本地服务器搭建_git_14

  1. 打开控制台
gitlab-rails console
  1. 输入测试内容
Notify.test_email('xxxxxxxxx@', 'Message Subject', 'Message Body').deliver_now

云服务器安装gitlab内存占满 gitlab本地服务器搭建_git_15

你的邮箱能收到gitlab发的邮件就OK了。

三、常用操作命令

(一)基操

配置服务

sudo gitlab-ctl reconfigure

启动服务

sudo gitlab-ctl start

停止服务

sudo gitlab-ctl stop

重启服务

sudo gitlab-ctl restart

(二)检查服务的日志信息

检查redis的日志

sudo gitlab-ctl tail redis

检查postgresql的日志

sudo gitlab-ctl tail postgresql

检查gitlab-workhorse的日志

sudo gitlab-ctl tail gitlab-workhorse

检查logrotate的日志

sudo gitlab-ctl tail logrotate

检查nginx的日志

sudo gitlab-ctl tail nginx

检查sidekiq的日志

sudo gitlab-ctl tail sidekiq

检查unicorn的日志

sudo gitlab-ctl tail unicorn

(三)检查服务状态

sudo gitlab-ctl status

一般服务状态显示信息
显示格式:

状态 : 进程名称:(进程ID)运行时间(秒);进程的日志服务进程和运行时间 run: gitlab-workhorse: (pid
 4752) 10759s; run: log: (pid 1077) 13185s run: logrotate: (pid 12616)
 3557s; run: log: (pid 1079) 13185s run: nginx: (pid 4764) 10758s; run:
 log: (pid 1076) 13185s run: postgresql: (pid 4770) 10757s; run: log:
 (pid 1073) 13185s run: redis: (pid 4778) 10757s; run: log: (pid 1072)
 13185s run: sidekiq: (pid 4782) 10756s; run: log: (pid 1075) 13185s
 run: unicorn: (pid 4786) 10756s; run: log: (pid 1074) 13185s

状态 说明
run 运行状态
down 服务停止

四、遇到的问题及解决方案

这里把本人遇到的问题总结下,给大家参考:

Q1:最常见的问题,访问报502,gitlab访问错误。Whoops, GitLab is taking too much time to respond

云服务器安装gitlab内存占满 gitlab本地服务器搭建_ubuntu_16

问题定位:我的80,8080端口被其他进程占用。

解决方案:

  1. vim 打开gitlab.rb
  2. 解决80端口问题:
    将external_url添加一个未被使用的端口
    external_url ‘http://192.168.0.110’
    修改为没有使用的端口即可:
    external_url ‘http://192.168.0.110:60000’
  3. 解决8080端口问题:
    将下面gitlab.rb这3行打开注释并修改为未被占用端口
    默认注释:
# unicorn['port'] = 8080  
# postgresql['shared_buffers'] = "256MB"
# postgresql['max_connections'] = 200

修改为:
unicorn[‘port’] = 8089
postgresql[‘shared_buffers’] = “256MB”
postgresql[‘max_connections’] = 200

  1. 重新加载配置sudo gitlab-ctl reconfigure后即可访问。

Q2:root密码忘记怎么办?
解决方案:

cd /opt/gitlab/bin/

gitlab-rails console production

u=User.where(id:1).first

u.password=12345678

u.password_confirmation=12345678

u.save!

exit

如下图:

云服务器安装gitlab内存占满 gitlab本地服务器搭建_ubuntu_17


参考下图:

云服务器安装gitlab内存占满 gitlab本地服务器搭建_云服务器安装gitlab内存占满_18


Q3:Gitlab启动失败解决

问题描述:使用sudo gitlab-ctl start启动服务失败

云服务器安装gitlab内存占满 gitlab本地服务器搭建_ubuntu_19


解决方案:

  1. 可以先尝试用
systemctl start gitlab-runsvdir.service
  1. 如果冻结不动的话,就需要查看系统服务项了
systemctl -t target

如果系统正常的话,所有任务应该是loaded active active,如果存在inactive dead选型,意味着有些服务出错了。

UNIT                   LOAD   ACTIVE SUB    DESCRIPTION
basic.target           loaded active active Basic System
cloud-config.target    loaded active active Cloud-config availability
cloud-init.target      loaded active active Cloud-init target
cryptsetup.target      loaded active active Encrypted Volumes
getty.target           loaded active active Login Prompts
graphical.target       loaded active active Graphical Interface
local-fs-pre.target    loaded active active Local File Systems (Pre)
local-fs.target        loaded active active Local File Systems
multi-user.target      loaded active active Multi-User System
network-online.target  loaded active active Network is Online
network-pre.target     loaded active active Network (Pre)
network.target         loaded active active Network
nss-user-lookup.target loaded active active User and Group Name Lookups
paths.target           loaded active active Paths
remote-fs-pre.target   loaded active active Remote File Systems (Pre)
remote-fs.target       loaded active active Remote File Systems
slices.target          loaded active active Slices
sockets.target         loaded active active Sockets
swap.target            loaded active active Swap
sysinit.target         loaded active active System Initialization
time-sync.target       loaded active active System Time Synchronized
timers.target          loaded active active Timers

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

22 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
multi-user.target      loaded inactive dead   start Multi-User System
  1. 使用systemctl list-jobs 如果存在某个任务是running,则是该任务堵塞了其他所有服务。
    比如 == plymouth-quit-wait.service ==
1 graphical.target                      start waiting
107 plymouth-quit-wait.service           start running
  2 multi-user.target                    start waiting
169 ureadahead-stop.timer                start waiting
121 gitlab-runsvdir.service              start waiting
151 system-getty.slice                   start waiting
 31 setvtrgb.service                     start waiting
122 systemd-update-utmp-runlevel.service start waiting
  1. 利用 systemctl stop plymouth-quit-wait.service结束该任务
  2. 然后重启服务gitlab-ctl reconfigure即可。
  3. 详细参考:https://docs.gitlab.com/omnibus/common_installation_problems/#gitlab-runsvdir-not-starting