以下內容為本人的著作,如需要轉載,請宣告原文鏈接 微信公眾號「englyf」https://www.cnblogs.com/englyf/p/16683282.html
最近到了新團隊,只有幾部新電腦,然后啥也沒有了,老江說需要一個管理工程代碼的 git 版本服務器,還說他以前用過 gitlab,于是我就自告奮勇接下了鍋,在本地主機里搭建上線一個 Gitlab 服務器,
使用這個 git 版本服務器不僅支持用 git 工具同步代碼,還可以像打開一個網站一樣使用倉庫管理的其它功能,非常方便,入口連接地址需要自己配置,后邊有介紹,
Gitlab 遵守 MIT 協議,所以可以放心大膽使用,
目標:
為了快速上線一個馬上用到的版本服務器,這里不打算對 gitlab 的使用做詳細說明,僅針對安裝配置和上線做初步的介紹,
后期計劃:
可能有些使用場景也需要用到一些特殊的服務,比如郵箱、固定域名訪問地址、外網訪問等,所以后期也會有這方面的介紹,敬請關注,
基礎環境:
Ubuntu 18.04.1 amd64
良好的網路
1) 準備一下安裝環境
以下會使用 apt-get 來安裝軟體,由于肉身在墻內(相信大部分讀者也一樣吧?),所以切換安裝源到墻內對于安裝程序會有幫助,起碼時間上會快一丟丟,為了以后如果有需要還原默認的安裝源配置,這里特地備份一下 apt-get 的安裝源組態檔
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
然后打開它
sudo gedit /etc/apt/sources.list
我覺得阿里云的資源訪問速度比較快,所以這里準備把安裝源切到阿里云,這里有個 Ubuntu 的資源貼出來給大家耍耍,用瀏覽器打開下邊的地址
https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.3e221b114ndGlW
這里邊列出了好幾個配置參考,對應于各個 Ubuntu LTS 版本(比如16.04、18.04、20.04等),太貼心了,以示敬意,不多說,直接拷貝對應的配置內容然后粘貼到上邊打開的 sources.list 檔案中并且保存,
接著就可以拉取最新的安裝包資訊了,開始后稍等片刻
sudo apt-get update
為了下面的操作,需要先安裝一個工具 curl
sudo apt-get install curl
安裝 gitlab 時還有很多的依賴這里沒有展開來講,不過幸好官方其實提供了一個現成腳本用來配置倉庫,用下面的指令就可以執行
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
上面執行程序有可能會失敗,提示
Installing /etc/apt/sources.list.d/gitlab_gitlab-ce.list...curl: (7) Failed to connect to packages.gitlab.com port 443: Connection refused
這是連接不穩定導致的,多執行幾次指令或許就順利通過了,下邊是我的執行結果,表示倉庫已經設定成功,萬事俱備就等著一鍵安裝了
Detected operating system as Ubuntu/bionic.
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... gpg: WARNING: unsafe ownership on homedir '/home/if/.gnupg'
done.
Running apt-get update... done.
The repository is setup! You can now install packages.
2) 在線安裝 gitlab
開始安裝
sudo apt-get install gitlab-ce
好遺憾,又提示 connect 失敗了
Could not connect to packages.gitlab.com:443
...
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
在上邊的安裝失敗提示資訊最后還建議在安裝命令引數里添加個選項 --fix-missing,然后重試
sudo apt-get install gitlab-ce --fix-missing
不幸的是,我這邊還是安裝失敗了,如果你的網路足夠好或許就成功了呢,要看安裝成功的資訊可以翻到 手動安裝 末尾,安裝成功可以直接跳到 啟動 gitlab 服務 一節,
苦啊,那就手動安裝吧,繼續看,,,
3) 手動安裝 gitlab
用瀏覽器去官方網站把安裝包 down下來
https://packages.gitlab.com/gitlab/gitlab-ce
我這里下載的是當前最新的包
gitlab-ce_14.10.2-ce.0_amd64.deb
找個目錄存一下再到目錄里執行
sudo dpkg -i gitlab-ce_14.10.2-ce.0_amd64.deb
這個安裝包大小差不多 1G,稍等了一會,終于安裝完成!欣賞一下壯舉
Selecting previously unselected package gitlab-ce.
(Reading database ... 130072 files and directories currently installed.)
Preparing to unpack gitlab-ce_14.10.2-ce.0_amd64.deb ...
Unpacking gitlab-ce (14.10.2-ce.0) ...
Setting up gitlab-ce (14.10.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-10
4) 啟動 gitlab 服務
上邊安裝成功的提示資訊里,說到因為安裝程序沒有檢測到有效的主機名,需要配置登錄地址,修改檔案 /etc/gitlab/gitlab.rb,用檔案圖形編輯工具 gedit 打開這個檔案
sudo gedit /etc/gitlab/gitlab.rb
在上面打開的檔案中,找到變數 external_url,把主機的 IP 地址填進去并保存,主機的 IP 可以用指令 ifconfig 找到,一般還會在 IP 后邊添加個埠號防止和默認的埠沖突,我這里選了 8080
external_url 'http://IP:8080/'
填在 external_url 后邊的內容就是 gitlab 的登錄地址了,找到的這個 IP 一般都是網內動態地址,所以上面的這個登錄地址只在網內有效,如果有需求在網外訪問,考慮到安全性等因素,需要 VPN 支持,這里不展開,另外,畢竟是動態地址,如果需要使用固定的域名地址訪問,需要 DNS 服務器支持,還是不展開了,
最后,載入配置并開始運行 Gitlab 服務
sudo gitlab-ctl reconfigure
重新啟動所有 gitlab 相關服務會花點時間,
另外,重啟服務器主機后,默認情況下,不需要手動重啟 gitlab 服務,gitlab 會自動啟動,如果你主動關閉了 gitlab 服務,那么打開的指令是
sudo gitlab-ctl start
5) 修改 gitlab 管理員密碼
gitlab 安裝完畢時,會生成一個檔案 /etc/gitlab/initial_root_password 保存管理員賬戶 root 的初始密碼
~$ sudo cat 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: 93fm3-ae-f3jijq2oqh3iuhUFUEIBEFAFE434J5-PP04K30=
# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
從檔案內容來看,這個密碼會在24小時后被自動洗掉,所以需要把這 root 賬戶的密碼另存或者重設一個好記點的,
修改管理員密碼有幾種方式,我這里用 gitlab-rails 指令重新設定管理員密碼,設定完畢之后不需要重啟 gitlab 服務,先進入控制臺
sudo gitlab-rails console
找到管理員賬戶,存入自定義變數 user
irb(main):001:0> user = User.find_by_username 'root'
=> #<User id:1 @root>
設定密碼,太簡單的密碼在后邊保存的時候會導致操作失敗
irb(main):002:0> user.password = 'xxx'
=> "xxx"
設定確認密碼
irb(main):003:0> user.password_confirmation = 'xxx'
=> "xxx"
保存
irb(main):004:0> user.save!
=> true
退出控制臺
irb(main):005:0> exit
好了,去試一下用瀏覽器打開網址 http://IP:8080/ 吧,祝你好運!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/506089.html
標籤:其他
