Linux 環境下安裝 GitLab 與配置

Linux 環境下安裝 GitLab 與配置

什麼是 GitLab?
GitLab 是一個用於倉庫管理系統的開源項目,使用 Git 做爲代碼管理工具,並在此基礎上搭建起來的 web 服務。node

GitLab的功能特色
提供了管理,計劃,建立,驗證,打包,發佈,配置,監視和保護應用程序的功能 是一個在線代碼倉庫託管軟件,是提供代碼託管、提交審覈和問題跟蹤的代碼管理平臺python

對服務器的要求:配置建議CPU2核心,內存4G以上
Git、GitLab、GitHub三者的區別
Git 基於命令的版本操做,全命令操做,無可視化界面;ios

Gitlab 基於 Git 實現的在線代碼倉庫軟件,提供了 web 的管理界面,用於企業內部;nginx

Github 基於 Git 實現的在線代碼倉庫軟件,提供了可視化管理界面,提供開放和私有的倉庫。大部分開源項目選擇 Github 做爲代碼託管倉庫;git

服務組成部分
nginx:靜態web服務器:端口號爲80;web

gitlab-shell:用於處理 git 命令和修改 authorized key 列表;redis

gitlab-workhorse:輕量級反向代理服務器(go語言);sql

gitlab-workhorse 是一個敏捷的反向代理,它會處理一些大的 HTTP 請求,好比文件上傳、下載,gitshell

push/pull 和 git 包下載,其餘請求會反向代理到 gitlab rails 應用,即反向按代理給後端的unicorn;數據庫

logrotate:日誌文件管理工具;

postgresql:數據庫;

reids:緩存數據庫;

sidekiq:用於在後臺執行隊列任務(異步執行) (ruby);

unicorn:用ruby編寫的一個HTTP服務器:端口號爲8080;

gitlab rails應用:是託管在這個服務器上

功能介紹
基於web方式的管理平臺;

擁有完善的權限的控制和身份認證模塊;

問題追蹤和代碼質量管理;

代碼review(複查)功能;

文檔管理、知識庫管理;

安裝 GitLab 的步驟

[root@localhost ~]# ls /var/cache/yum/x86_64/\$releasever/gitlab_gitlab-ce/packages/     //從網絡上yum下載的包的位置
[root@localhost ~]# yum install -y curl openssh-server openssh-clients cronie policycoreutils-python postfix     //安裝依賴包
[root@localhost ~]# curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash   //下載gitlab的yum(倉庫)源,sudo用於提權(適用於普通用戶)
[root@localhost ~]# yum list | grep gitlab-ce           //查看是否有gitlab-ce
gitlab-ce.x86_64                        11.7.5-ce.0.el7            gitlabgitlab-ce
[root@localhost ~]# yum -y install gitlab-ce        //安裝gitlab-ce 
[root@localhost ~]# ls /var/cache/yum/x86_64/\$releasever/gitlab_gitlab-ce/packages/     //從網絡上yum下載的包的位置

###設置URL
[root@localhost ~]# vim /etc/gitlab/gitlab.rb  //修改一個URL地址
  external_url 'http://gitlab.bdqn.cn'
[root@localhost ~]# vim /etc/hosts  //編輯一個域名解析
  192.168.1.21    gitlab.bdqn.cn
[root@localhost ~]# gitlab-ctl reconfigure   //從新配置gitlab的配置文件

[root@localhost ~]# gitlab-ctl start   //啓動gitlab服務
ok: run: alertmanager: (pid 5151) 363s
ok: run: gitaly: (pid 4934) 368s
ok: run: gitlab-monitor: (pid 5070) 366s
ok: run: gitlab-workhorse: (pid 4897) 370s
ok: run: logrotate: (pid 4120) 416s
ok: run: nginx: (pid 4071) 418s
ok: run: node-exporter: (pid 4974) 367s
ok: run: postgres-exporter: (pid 5178) 362s
ok: run: postgresql: (pid 3564) 458s
ok: run: prometheus: (pid 5111) 364s
ok: run: redis: (pid 3420) 464s
ok: run: redis-exporter: (pid 5090) 365s
ok: run: sidekiq: (pid 3934) 430s
ok: run: unicorn: (pid 3862) 431s
[root@localhost ~]# gitlab-ctl restart          //從新啓動gitlab服務
[root@localhost ~]# free -m    //查看運行內存
[root@localhost ~]# firefox gitlab.bdqn.cn   //查看gitlab頁面,或者使用ip地址訪問
[root@localhost ~]# firefox 192.168.1.21

gitlab頁面的出現的問題; 檢測端口號80和8080是否被佔用;
訪問主頁502的時候,看一下端口號8080是否被佔用

gitlab相關命令
gitlab-ctl start       //啓動gitlab服務
gitlab-ctl stop        //中止gitlab服務
gitlab-ctl start unicorn(添加組件名稱)   //單獨開啓某個組件
gitlab-ctl status        //查看gitlab的運行狀態
gitlab-ctl reconfigure    //修改完配置文件從新編譯
gitlab-ctl tail       //查看gitlab的日誌

配置 GitLab 郵件服務
sendmail /postfix /smtp
古老郵箱 郵件服務服務器 在線郵箱

配置smtp郵箱步驟

###修改配置文件
[root@localhost ~]# vim /etc/gitlab/gitlab.rb
 gitlab_rails['gitlab_email_from'] = 'bm73333@163.com'   //說明郵箱從哪裏發,設置本身的郵箱地址
 #ctrl+v 進入可視塊 +x
 gitlab_rails['smtp_enable'] = true
 gitlab_rails['smtp_address'] = "smtp.163.com" //在線郵件服務器地址
 gitlab_rails['smtp_port'] = 25   //端口號,開啓本地發郵件的端口
 gitlab_rails['smtp_user_name'] = "bm73333@163.com"  //郵箱地址
 gitlab_rails['smtp_password'] = "qwerty567890"  //郵箱受權碼,調用郵箱
 ###郵箱受權碼的獲取
 設置 
 POP3/SMTP/IMAP
 客戶端受權密碼,在裏面就能夠設置受權碼

 gitlab_rails['smtp_domain'] = "163.com"
 gitlab_rails['smtp_authentication'] = "login"   //在登陸時,查看是否有受權碼
 gitlab_rails['smtp_enable_starttls_auto'] = true   //加密方式
 gitlab_rails['smtp_tls'] = false    //加密方式

  user['git_user_email'] = "bm73333@163.com"
###修改完成

從新編譯配置文件
[root@localhost ~]# gitlab-ctl reconfigure
重啓gitlab服務
[root@localhost ~]# gitlab-ctl restart
進入控制檯
[root@localhost ~]# gitlab-rails console
-------------------------------------------------------------------------------------
 GitLab:       11.7.5 (c5b5b18)
 GitLab Shell: 8.4.4
 postgresql:   9.6.11
-------------------------------------------------------------------------------------
Loading production environment (Rails 5.0.7.1)
irb(main):001:0> Notify.test_email('18256001006@163.com','yangxiaofeiNB','hello,world!').deliver_now  //發送郵件,deliver_now表明馬上發送
(格式:收件人的郵箱地址,郵件標題,郵件內容)
irb(main):003:0> exit   //退出控制檯

GitLab 的平常操做

gitlab的備份
[root@localhost ~]# gitlab-rake gitlab:backup:create    //gitlab的備份
備份的數據的目錄
[root@localhost ~]# ls /var/opt/gitlab/backups/
1550541218_2019_02_19_11.7.5_gitlab_backup.tar   //備份完成後會造成tar包,文件名格式:時間戳,當前日期,當前gitlab版本,backup備份包
恢復數據的步驟
首先中止一下兩個組件
[root@localhost ~]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
[root@localhost ~]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up

切換到備份目錄
[root@localhost ~]# cd /var/opt/gitlab/backups/
[root@localhost backups]# ls
1550541218_2019_02_19_11.7.5_gitlab_backup.tar
[root@localhost backups]# gitlab-rake gitlab:backup:restore BACKUP=1550541218_2019_02_19_11.7.5   //進行數據的恢復,名字爲上面的時間戳,日期,版本號組成
出現如下提示
Do you want to continue (yes/no)? yes
Do you want to continue (yes/no)? yes
[root@localhost backups]# gitlab-ctl start sidekiq  //啓動組件服務
ok: run: sidekiq: (pid 19474) 0s
[root@localhost backups]# gitlab-ctl start unicorn  //啓動組件服務
ok: run: unicorn: (pid 19536) 1s
數據恢復完成

訪問gitlab的web頁面
http://gitlab.bdqn.com 或者 http://localhost 的 ip 地址

進入控制檯的兩種方法

一、[root@localhost ~]# gitlab-rails console   //進入控制檯
-------------------------------------------------------------------------------------
 GitLab:       11.7.5 (c5b5b18)
 GitLab Shell: 8.4.4
 postgresql:   9.6.11
-------------------------------------------------------------------------------------
Loading production environment (Rails 5.0.7.1)  //加載生產環境
irb(main):001:0>
#二、[root@localhost ~]# gitlab-rails console production
-------------------------------------------------------------------------------------
 GitLab:       11.7.5 (c5b5b18)
 GitLab Shell: 8.4.4
 postgresql:   9.6.11
-------------------------------------------------------------------------------------
Loading production environment (Rails 5.0.7.1)
irb(main):001:0>

忘記 root 密碼,進行修改

[root@localhost ~]# gitlab-rails console production
-------------------------------------------------------------------------------------
 GitLab:       11.7.5 (c5b5b18)
 GitLab Shell: 8.4.4
 postgresql:   9.6.11
-------------------------------------------------------------------------------------
Loading production environment (Rails 5.0.7.1)
irb(main):001:0> user = User.where(id:1).first   //切換到root用戶
=> #<User id:1 @root>    
irb(main):002:0> user.password='1234.com'   //''裏面修改密碼,密碼爲8位
=> "1234.com"
irb(main):003:0> user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 7661b452-fcb4-48a0-b9f0-35995ba21258) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", #<GlobalID:0x00007f4360748a10 @uri=#<URI::GID gid://gitlab/User/1>>
=> true
irb(main):004:0> exit

驗證是都修改爲功,使用web進行登陸測試
##至此修改完成

查看 GitLab 的日誌

[root@localhost ~]# gitlab-ctl tail   //查看總體的日誌
查看組件的相關日誌
[root@localhost ~]# gitlab-ctl tail redis   //查看redis的日誌
[root@localhost ~]# gitlab-ctl tail logrotate

經過 ssh 遠程鏈接方式往 GitLab 上傳文件

查看密鑰
[root@localhost ~]# ssh-keygen -t rsa -C "zhangsan@bdqn.cn"  //爲建立用戶時輸入的郵箱地址
[root@localhost .ssh]# pwd 
/root/.ssh
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub
[root@localhost .ssh]# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDG+woCe7UsBcXhU/I5hRZ0B7fzOOzzGsYzm1+qSvxn1SRaWQ53TNfWJJKpmamodHDI0IWHld6MI+2IB5bSXobNjrV1W147m34fE3KZZ4Kx9wwQ89UnUzqREIPDHnTLozHOE36AxGh6pREFawMjxJJjzd6d3xC9PM9bQ3KaY80agGY44N1vO6c+Turfkpyre3fyITudEqPlrCibJF9eikXgFhHNtrrWVAII6LUKU89LNfXKKYfOmXvB+T+ZYODkXqPR5GPCz4IIuGd5fxpfAmnTvm+Sz3Dp5APVSmveEx9ivtRVsb5lqlYoXJT1vL+D9dQKFY7HI8/FttOSduQNLfrv 45354066@qq.com
複製密鑰,到web頁面進行添加密鑰

驗證遠程登陸是否成功
[root@localhost .ssh]# ssh -T git@gitlab.bdqn.cn
The authenticity of host 'gitlab.bdqn.cn (192.168.1.21)' can't be established.
ECDSA key fingerprint is SHA256:88Ixrb6xHdt9SVsQ3n78nQbfzPplW8kZNKQLuldud6w.
ECDSA key fingerprint is MD5:3e:34:68:db:3a:f6:5c:a5:de:18:9b:1c:cc:ef:f9:ff.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.bdqn.cn,192.168.1.21' (ECDSA) to the list of known hosts.
Welcome to GitLab, @root!

克隆項目,進行上傳

[root@localhost ~]# yum install -y git   //下載git
聲明誰操做,全局配置,爲用戶和用戶郵箱(用戶必須有才行)
[root@localhost ~]# git config --global user.name "zhangsan"
[root@localhost ~]# git config --global user.email "zahngsan@bdqn.cn"
[root@localhost ~]# git clone git@gitlab.bdqn.cn:ios/ios.git   //克隆項目庫
正克隆到 'ios'...
warning: 您彷佛克隆了一個空版本庫。
[root@localhost ~]# ls                  //生成一個目錄,就是克隆的代碼
anaconda-ks.cfg       ios   模板  圖片  下載  桌面
initial-setup-ks.cfg  公共  視頻  文檔  音樂
[root@localhost ~]# cd ios/             //首先進入到克隆的庫中
[root@localhost ~]# touch README.md          //建立一個文件
[root@localhost ~]# echo 111 > README.md        //輸入內容
[root@localhost ios]# git add README.md       //git中添加內容
[root@localhost ios]# git commit -m "test111"   //進行提交,打一個標籤
[master(根提交) ad3c913] test111
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
[root@localhost ios]# git push origin master  //上傳,原來的,向主幹添加
Counting objects: 3, done.
Writing objects: 100% (3/3), 209 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@gitlab.bdqn.cn:ios/ios.git
 * [new branch]      master -> master
進入web頁面就能夠看到項目了
克隆項目
[root@localhost ~]# mkdir test
[root@localhost ~]# cd test/
[root@localhost test]# git clone git@gitlab.bdqn.cn:ios/ios.git
正克隆到 'ios'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收對象中: 100% (3/3), done.
[root@localhost test]# ls
ios
[root@localhost test]# cd ios/
[root@localhost ios]# ls
README.md
相關文章
相關標籤/搜索