Ubuntu20.04安裝配置GitLab Server

介紹

GitLab CE或Community Edition是一個開源應用程序,主要用於託管Git存儲庫,以及其他與開發相關的功能,如問題跟蹤。它旨在使用您自己的基礎架構進行託管,併爲您的開發團隊提供部署內部存儲庫的靈活性,與用戶交互的公共方式,或者爲貢獻者提供託管自己項目的方法。

GitLab項目使用簡單的安裝機制在您自己的硬件上設置GitLab實例變得相對簡單。在本指南中,我們將介紹如何在虛擬機Ubuntu 20.04上安裝和配置GitLab。

進入gitlab.com官方主頁 菜單Product -> Intall GitLab

查看“View Install instructions” -> " For Ubuntu 20.04, arm64 packages are also available. "

第1步 - 安裝依賴項

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

安裝postfix爲了能發送郵件。

sudo apt-get install -y postfix

選擇“Internet Site” **<OK>**繼續。

"System mail name:" 輸入你服務器主機得郵箱名。

第2步 - 增加Gitlab版本並安裝版本包

cd /tmp
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash

顯示結果如下:
bspserver:/tmp$ curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5933  100  5933    0     0   7416      0 --:--:-- --:--:-- --:--:--  7416
Detected operating system as Ubuntu/focal.
Checking for curl...
Detected curl...
Checking for gpg...
Detected gpg...
Running apt-get update... done.
Installing apt-transport-https... done.
Installing /etc/apt/sources.list.d/gitlab_gitlab-ce.list...done.
Importing packagecloud gpg key... done.
Running apt-get update... done.

The repository is setup! You can now install packages.

您可以隨意檢查下載的腳本,以確保您對所需的操作感到滿意。您還可以在此處找到該腳本的託管版本:

less /tmp/script.deb.sh

一旦您對腳本的安全性感到滿意,請運行安裝程序:

sudo bash /tmp/script.deb.sh

該腳本將設置您的服務器以使用GitLab維護的存儲庫。這使您可以使用與其他系統軟件包相同的軟件包管理工具來管理GitLab。完成後,您可以使用以下apt命令安裝實際的GitLab應用程序:

sudo apt install gitlab-ce

這將在您的系統上安裝必要的組件。

安裝顯示結果如下:

bspserver:/tmp$ sudo apt install gitlab-ce
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  gitlab-ce
0 upgraded, 1 newly installed, 0 to remove and 63 not upgraded.
Need to get 972 MB of archives.
After this operation, 2,637 MB of additional disk space will be used.
Get:1 https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu focal/main amd64 gitlab-ce amd64 14.5.2-ce.0 [972 MB]
Fetched 972 MB in 2min 29s (6,538 kB/s)                                                                                                          
Selecting previously unselected package gitlab-ce.
(Reading database ... 201115 files and directories currently installed.)
Preparing to unpack .../gitlab-ce_14.5.2-ce.0_amd64.deb ...
Unpacking gitlab-ce (14.5.2-ce.0) ...
Setting up gitlab-ce (14.5.2-ce.0) ...
It looks like GitLab has not been configured yet; skipping the upgrade script.

       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.
  


     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/
  

Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:
  sudo gitlab-ctl reconfigure

For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md

Help us improve the installation experience, let us know how we did with a 1 minute survey:
https://gitlab.fra1.qualtrics.com/jfe/form/SV_6kVqZANThUQ1bZb?installation=omnibus&release=14-5

注意事項

在安裝GitLab應用程序中如果報以下錯誤,我們可以配置國內加速鏡像。

報錯信息:

# apt-get install gitlab-ce
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package gitlab-ce

修改安裝腳本

sudo vim /etc/apt/sources.list.d/gitlab_gitlab-ce.list

OLD

deb https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu/ focal main
deb-src https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu/ focal main

NEW

deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu focal main
deb-src https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu focal main

再次執行

sudo apt update
sudo apt install gitlab-ce

參考鏈接:https://gitlab.com/gitlab-org/gitlab-foss/-/issues/2370

第3步 - 編輯GitLab配置文件

在使用該應用程序之前,需要更新配置文件並運行重新配置命令。首先,打開Gitlab的配置文件:

sudo vim /etc/gitlab/gitlab.rb

頂部附近是external_url配置線。更新它以匹配您的域。更改httphttps以便GitLab會自動將用戶重定向到受Let's加密證書保護的站點:

## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
##!
##! Note: During installation/upgrades, the value of the environment variable
##! EXTERNAL_URL will be used to populate/replace this value.
##! On AWS EC2 instances, we also attempt to fetch the public hostname/IP
##! address from AWS. For more details, see:
##! https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
external_url 'http://example.com' // 此處修改爲您的域名或ip地址 external_url 'http://173.20.14.206' 

設置Gitlab郵箱服務配置:

### GitLab email server settings
###! Docs: https://docs.gitlab.com/omnibus/settings/smtp.html
###! **Use smtp instead of sendmail/postfix.**

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.aliyun.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "[email protected]"
gitlab_rails['smtp_password'] = "your aliyun email password"
gitlab_rails['smtp_domain'] = "aliyun.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitilab_email_from'] = "[email protected]"
user["git_user_email"] = "[email protected]"

檢查Gitlab郵箱服務配置是否修改成功:

grep "^[a-Z]" /etc/gitlab/gitlab.rb

bspserver@bspserver-hingetech:/tmp$ sudo grep "^[a-Z]" /etc/gitlab/gitlab.rb
external_url 'http://173.20.14.206'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.aliyun.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "[email protected]"
gitlab_rails['smtp_password'] = "your aliyun email password"
gitlab_rails['smtp_domain'] = "aliyun.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitilab_email_from'] = "[email protected]"
user["git_user_email"] = "[email protected]"

保存並關閉文件。運行以下命令重新配置Gitlab:

sudo gitlab-ctl reconfigure

這將使用它可以找到的有關您的服務器的信息初始化GitLab。這是一個完全自動化的過程,因此您不必回答任何提示。

到最後有個root初始密碼提示如下:

Notes:
Default admin account has been configured with following details:
Username: root
Password: You didn't opt-in to print initial root password to STDOUT.
Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours.

NOTE: Because these credentials might be present in your log files in plain text, it is highly recommended to reset the password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.

bspserver@bspserver-hingetech:/tmp$ sudo cat  /etc/gitlab/initial_root_password
# WARNING: This value is valid only in the following conditions
#          1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
#          2. Password hasn't been changed manually, either via UI or via command line.
#
#          If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.

Password: nWuzgOFdJkyQawfwpi842UHoW93YE51QK+FK2EkBPDc=

# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.

利用初始密碼登錄之前設置http://173.20.14.206登錄主頁IP。

成功登錄後你便可以選擇“Edit profile” -> " Password"修改root的密碼。

修改成功後自動退出,重新登錄。

附上GitLab常用命令:

常用命令 說明
sudo gitlab-ctl reconfigure 重新加載配置,每次修改/etc/gitlab/gitlab.rb文件之後執行
sudo gitlab-ctl status 查看 GitLab 狀態
sudo gitlab-ctl start 啓動 GitLab
sudo gitlab-ctl stop 停止 GitLab
sudo gitlab-ctl restart 重啓 GitLab
sudo gitlab-ctl tail 查看所有日誌
sudo gitlab-ctl tail nginx/gitlab_acces.log 查看 nginx 訪問日誌
sudo gitlab-ctl tail postgresql 查看 postgresql 日誌

第4步 - gitlab重置root用戶密碼

切換目錄:

cd /opt/gitlab/bin

執行 :sudo gitlab-rails console -e production 命令 開始初始化密碼

bspserver@bspserver-hingetech:~$ sudo gitlab-rails console -e production
[sudo] password for bspserver: 
Sorry, try again.
[sudo] password for bspserver: 
Sorry, try again.
[sudo] password for bspserver: 
--------------------------------------------------------------------------------
 Ruby:         ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]
 GitLab:       14.5.2 (76ceea558aa) FOSS
 GitLab Shell: 13.22.1
 PostgreSQL:   12.7
--------------------------------------------------------------------------------
Loading production environment (Rails 6.1.4.1)
irb(main):001:0> 

輸入 u=User.where(id:1).first 來查找與切換賬號(User.all 可以查看所有用戶)

irb(main):001:0> u=User.where(id:1).first
=> #<User id:1 @root>
irb(main):002:0> u=User.all
=> #<ActiveRecord::Relation [#<User id:1 @root>]>
irb(main):003:0> 

通過u.password='12345678'設置密碼爲12345678(這裏的密碼看自己喜歡):

=> #<User id:1 @root>
irb(main):005:0> u.password='12345678'
=> "12345678"

通過u.password_confirmation='12345678' 再次確認密碼

irb(main):006:0> u.password_confirmation='12345678'
=> "12345678"

通過 u.save!進行保存(切記切記 後面的 !)

irb(main):007:0> u.save!
=> true

如果看到上面截圖中的true ,恭喜你已經成功了,執行 exit 退出當前設置流程即可。

回到gitlab ,可以通過 root/12345678 這一超級管理員賬號登錄了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章