Linux:Gitlab服務器配置

Linux:Gitlab服務器配置

用於搭建自己的Git Server,可應用於樹莓派等設備,博主的搭建平臺爲x86架構主機,樹莓派使用Gitlab性能略有不足,推薦使用Gogs。

1、安裝Gitlab

#step 1: 安裝依賴項
sudo apt-get install -y curl openssh-server ca-certificates

#step 2: 接下來,爲了發送提醒郵件,需要安裝Postfix。如果你想使用其他方案發送郵件請跳過此步驟,在gitlab安裝完成之後配置額外的smtp服務
sudo apt-get install -y postfix

#安裝postfix的時候,會出現配置界面,如下所示:
Package configuration
┌───────────────────────────┤ Postfix Configuration ├───────────────────────────┐
│ Please select the mail server configuration type that best meets your needs.  │
│                                                                               │
│  No configuration:                                                            │
│   Should be chosen to leave the current configuration unchanged.              │
│  Internet site:                                                               │
│   Mail is sent and received directly using SMTP.                              │
│  Internet with smarthost:                                                     │
│   Mail is received directly using SMTP or by running a utility such           │
│   as fetchmail. Outgoing mail is sent using a smarthost.                      │
│  Satellite system:                                                            │
│   All mail is sent to another machine, called a 'smarthost', for delivery.    │
│  Local only:                                                                  │
│   The only delivered mail is the mail for local users. There is no network.   │
│                                                                               │
│ General type of mail configuration:                                           │
│                                                                               │
│                            No configuration                                   │
│                            Internet Site                                      │
│                            Internet with smarthost                            │
│                            Satellite system                                   │
│                            Local only                                         │
│                                                                               │
│                                                                               │
│                     <Ok>                         <Cancel>                     │
│                                                                               │
└───────────────────────────────────────────────────────────────────────────────┘

#選擇【Internet Site】選項完成本頁配置。下一步配置mail_name,輸入對應用戶名即可。實際使用中不需要用到郵件通知,所以這裏使用默認的地址,回車完成postfix的安裝配置。

#step 3: 配置軟件源 由於官網的資源安裝會很慢,推薦使用國內的鏡像去安裝,清華軟件源如下:
https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/

#step 4: 信任 GitLab 的 GPG 公鑰:
curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/null

#step 5: 添加清華大學的鏡像到/etc/apt/sources.list ,xenial爲系統版本號,根據ubuntu版本選擇
echo 'deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu xenial main' | sudo tee /etc/apt/sources.list.d/gitlab.list

#step 6: 更新及安裝gitlab-ce
sudo apt-get update
sudo apt-get install gitlab-ce

2、配置Gitlab

#配置gitlab實例訪問路徑
sudo nano /etc/gitlab/gitlab.rb
#修改 external_url爲自己主機的ip或者域名(如果是綁定了域名)
external_url 'http://Your IP or Domain name'

#如果需要Nginx端口轉發則進行一下配置,否則可以直接跳過
#在gitlab裏配置nginx的監聽端口,打開gitlab的配置文件
sudo nano /etc/gitlab/gitlab.rb
#大概在文件中間部分,948行,有如下配置,去掉#註釋,修改nil爲8888
nginx['listen_port'] = 8888
#配置Nginx代理
#保存文件退出。接下來配置nginx,打開nginx配置文件
sudo nano /etc/nginx/conf.d/default.conf
#在文件中添加如下配置:
upstream git {
    server  localhost:8888;
}
server {
    listen 80;
    server_name (Your IP or Domain name);

    location /gitlab {
        # 設置最大允許上傳單個的文件大小
        client_max_body_size 1024m;
        proxy_redirect off;
        #以下確保 gitlab中項目的 url 是域名而不是 http://git,不可缺少
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 反向代理到 gitlab 內置的 nginx
        proxy_pass http://git/gitlab;
        index index.html index.htm;
    }
}
#保存退出,重新加載nginx配置
sudo nginx -s reload

3、Gitlab啓動及其他指令

重新加載gitlab配置並重啓

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

打開必要的服務

sudo service sshd start
sudo service postfix start

其他指令

#使配置生效。
sudo gitlab-ctl reconfigure

#查看Gitlab狀態
sudo gitlab-ctl status

#清除緩存頁面:
sudo gitlab-rake cache:clear RAILS_ENV=production

#GitLab各組件啓動:
sudo gitlab-ctl start

#GitLab各組件停止:
sudo gitlab-ctl stop

#GitLab各組件重啓:
sudo gitlab-ctl restart

#禁用GitLab開機啓動:
sudo systemctl disable gitlab-runsvdir

#啓用GitLab開機自啓:
sudo systemctl enable gitlab-runsvdir
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章