概述
gitlab 是一款基于Git的Web界面的Git代碼托管和代碼審查的開源軟件。它具有版本控制、代碼審查、協作等功能,被認為是 github 的完美替代品。本文將介紹在 CentOS7 上安裝 GitLab 的過程。
系統要求
- CentOS7 x64 系統,內存 2GB 以上;
- 安裝并啟動 nginx;
- 安裝并啟動 postgresql;
- 安裝并啟動 redis;
- 開通 TCP 端口 22,80,443。
安裝必要軟件包
為了安裝 GitLab,您需要在系統上安裝一些必要軟件包。
sudo yum -y update sudo yum -y install curl openssh-server openssh-clients postfix cronie wget
安裝 GitLab
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash sudo yum -y install gitlab-ce
GitLab 啟動
sudo gitlab-ctl reconfigure
在完成 GitLab 的安裝過程之后,可以使用以下命令啟動 GitLab:
sudo gitlab-ctl start
訪問 GitLab
默認情況下,GitLab 使用 HTTP 協議的 80 端口,因為在安裝 GitLab 的過程中已經安裝了 Nginx,所以可以通過訪問服務器的 IP 地址或域名來訪問您的 GitLab 實例。
http://<your-server-ip>
第一次訪問 Gitlab
當您第一次訪問您的 GitLab 實例的時候,需要設置一個管理員密碼,以便在下一次訪問時進行身份驗證。
在瀏覽器中訪問 GitLab 實例時,會自動定向到密碼設置頁面。輸入密碼并點擊 “設置密碼” 按鈕。密碼必須至少包含一個小寫字母、一個大寫字母、一個數字和一個非字母的字符,長度至少為 8 個字符。如下圖所示:
設置密碼后,會自動定向到登錄頁面,使用您剛剛設置的密碼登錄即可。
Nginx 反代
Nginx 反代可以加速 GitLab 運行速度。
修改 GitLab 配置文件
sudo vim /etc/gitlab/gitlab.rb
找到下面這一行:
external_url 'http://gitlab.example.com'
將其中的 http://gitlab.example.com 更改為您的域名或 IP 地址。然后將配置寫入 GitLab。
sudo gitlab-ctl reconfigure
配置 Nginx
創建一個新的 Nginx 配置文件:
sudo touch /etc/nginx/conf.d/gitlab.conf sudo vim /etc/nginx/conf.d/gitlab.conf
添加以下內容:
upstream gitlab-workhorse { server 127.0.0.1:8181 fail_timeout=0; } server { listen 80; # Replace with your domain name server_name gitlab.example.com; server_tokens off; ## Don't show the nginx version number, a security best practice location / { # Change this to the protocol you prefer/require. proxy_pass http://gitlab-workhorse; # Enable websocket support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-ssl on; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_read_timeout 180; proxy_send_timeout 180; } }
將里面的 gitlab.example.com 更改為您的 Nginx 域名或 IP。
重啟 Nginx。
sudo systemctl restart nginx.service
訪問 GitLab 界面。
http://gitlab.example.com
總結
在 CentOS7 上搭建 GitLab 不難,遵循上述步驟可以在短時間內完成基本安裝。如果您需要更高級別的配置,可以按照 GitLab 官方文檔中提供的方式進行操作。
參考文獻
- [1] GitLab.com
- [2] GitLab Documentation
- [3] How to Install Gitlab, Nginx and SSL on CentOS 7
- [4] How To Install and Configure GitLab on CentOS 7