GitLab 是一個用于倉庫管理系統的開源項目,使用Git作為代碼管理工具,并在此基礎上搭建起來的web服務。
系統環境準備
建議:記憶體4G以上不然帶不動
[root@V1 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@V1 ~]# uname -r
3.10.0-693.el7.x86_64
#關閉firewalld和NetWorkManager
[root@V1 ~]# systemctl stop firewalld
[root@V1 ~]# systemctl stop NetWorkManager
#永久關閉
[root@V1 ~]# systemctl disable firewalld
[root@V1 ~]# systemctl disable NetWorkManager
#關閉selinux
[root@V1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@V1 ~]# setenforce 0
#更換阿裡源和下載下傳epel源
[root@V1 ~]#mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@V1 ~]#wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@V1 ~]#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#yum源更新,重新開機系統
[root@V1s ~]# yum update && reboot
#安裝相應工具
[root@V1 ~]yum install curl policycoreutils openssh-server openssh-clients postfix -y
[root@V1 ~]systemctl start postfix
#建立GitLab源
[root@V1 ~]# vim /etc/yum.repos.d/gitlab-ce.repo
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
[root@V1 ~]# yum makecache
[root@V1 ~]# yum install -y gitlab-ce
#安裝git工具
[root@V1 ~]# yum install git -y
通常我們在部署的時候會發現80和8080端口均被占用,那麼我們就需要修改gitlab的預設端口。
#gitlab配置檔案路徑,用于gitlab如何調用80和8080的服務等。
/etc/gitlab/gitlab.rb
編輯配置檔案并找到 unicorn 配置,将端口修改成想要的端口
修改/etc/gitlab/gitlab.rb
vim /etc/gitlab/gitlab.rb
#unicorn['port'] = 8080 修改 8070 預設是注釋的去掉前面的#
unicorn['port'] = 8070
#nginx['listen_port'] = nil 修改 8090 預設是注釋的去掉前面的#
nginx['listen_port'] = 8090
修改/var/opt/gitlab/gitlab-rails/etc/unicorn.rb
vim /var/opt/gitlab/gitlab-rails/etc/unicorn.rb
#listen "127.0.0.1:8080", :tcp_nopush => true
listen "127.0.0.1:8070", :tcp_nopush => true
修改預設的gitlab nginx的web服務80端 /var/opt/gitlab/nginx/conf/gitlab-http.conf
vim /var/opt/gitlab/nginx/conf/gitlab-http.conf
#listen *:80;
listen *:8090;
#加載配置檔案并啟動,時間稍長
[root@V1 ~]# gitlab-ctl reconfigure
#gitlab-ctl其他指令
啟動:gitlib-ctl start
關閉:gitlab-ctl stop
重新開機:gitlab-ctl restart
重載配置:gitlab-ctl reconfigure
檢視狀态:gitlab-ctl status
#檢視端口保證80端口不被占用
[root@V1 ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 700 root 7u IPv4 16903 0t0 TCP *:http (LISTEN)
nginx 804 gitlab-www 7u IPv4 16903 0t0 TCP *:http (LISTEN)
浏覽器登入gitlab,輸入
http://伺服器ip:配置的nginx端口,進入下圖:

設定一個密碼後,使用 root 使用者進行登入。
建立一個項目
那麼我們怎樣為 GitLab 的賬号添加 SSH keys 呢
1).首先打開 linux 伺服器,輸入指令:ls -al ~/.ssh,檢查是否顯示有 id_rsa.pub 或者 id_dsa.pub 存在,如果存在請直接跳至第3步。
2).在 bash 中輸入 ssh-keygen -t rsa -C ”[email protected]” ,注意這個地方的郵箱位址位址替換成你自己的郵箱位址即可,在顯示如下的輸出後一直按回車即可:
[root@V1 ~]# useradd web-gitlab
[root@V1 ~]# su !$
su web-gitlab
[web-gitlab@V1 root]$
[web-gitlab@V1 root]$
[web-gitlab@V1 root]$ ssh-keygen -t rsa -C ”[email protected]”
Generating public/private rsa key pair.
Enter file in which to save the key (/home/web-gitlab/.ssh/id_rsa):
Created directory '/home/web-gitlab/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/web-gitlab/.ssh/id_rsa.
Your public key has been saved in /home/web-gitlab/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:hwumGaUbZy7pD/R1LmaRC4NV42NToJZIuqsv1CMyhuo ”[email protected]”
The key's randomart image is:
+---[RSA 2048]----+
| . +.. |
| o . = o |
| . . * = |
| . * . = |
|. o * B S o |
|++ = & = B |
|=.o O o * . |
|o. . o o . |
|oE. ... |
+----[SHA256]-----+
[web-gitlab@V1 root]$
在這裡可以看到 id_rsa 和 id_rsa.pub 已經生成,并且生成的路徑為 /home/web-gitlab/.ssh/。
[web-gitlab@V1 root]$ cd /home/web-gitlab/
[web-gitlab@V1 ~]$
[web-gitlab@V1 ~]$
[web-gitlab@V1 ~]$ ll
total 0
[web-gitlab@V1 ~]$
[web-gitlab@V1 ~]$ ll .ssh/
total 8
-rw------- 1 web-gitlab web-gitlab 1679 Apr 28 10:42 id_rsa
-rw-r--r-- 1 web-gitlab web-gitlab 402 Apr 28 10:42 id_rsa.pub
3).打開 id_rsa.pub 檔案,并且複制全部内容。
[web-gitlab@PaulV1 ~]$ cat .ssh/id_rsa.pub
4).打開 GitLab 賬戶,添加 SSH Keys:
這樣就添加了一個SSH key。
5).完成上面的步驟之後就可以使用ssh來連接配接GitLab,并進行相應的操作了。
我們可以通過 ssh 來 clone 項目
[root@V1 ~]# su web-gitlab
[web-gitlab@V1 root]$
[web-gitlab@V1 root]$ cd /home/web-gitlab/
[web-gitlab@V1 ~]$ git clone git@伺服器IP:root/test-demo.git
Cloning into 'test-demo'...
The authenticity of host '伺服器IP (伺服器IP)' can't be established.
ECDSA key fingerprint is SHA256:+CWe+3ROJVtuClFaH425BsTSYf+c9vTuhHF/6jTr4TI.
ECDSA key fingerprint is MD5:63:c6:8a:1a:59:fa:b8:ec:22:cb:29:dd:68:57:d4:3d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '伺服器IP' (ECDSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.
[web-gitlab@V1 ~]$
[web-gitlab@V1 ~]$ ll
total 4
drwxrwxr-x 3 web-gitlab web-gitlab 4096 Apr 28 11:03 test-demo
這就是我們通過 ssh 克隆下來的項目。
參考資料: