GitLab
這是來自 Gitlab的主頁:
- GitLab 提供 git 倉儲管理、代碼查閱、問題追蹤、動態監控和維基. 企業在本地安裝Gitlab並用LDAP和活動目錄伺服器連接以進行安全的身份驗證和授權。單個GitLab伺服器可以處理超過25,000個用戶,但也可以使用多個活動伺服器創建高可用性設置。
你可以在 GitLab.com找到實時版本的例子.
安裝[編輯 | 編輯原始碼]
Gitlab需要 Redis 和資料庫後端. 如果你計劃在同一個機器運行的話, 首先得安裝 PostgreSQL (簡體中文).
為了接受郵件通知, 一個郵件伺服器必須被安裝和配置. 查閱 Category:Mail server 獲取更多細節.
配置[編輯 | 編輯原始碼]
初步說明[編輯 | 編輯原始碼]
Gitlab包括多種組件, 可查閱架構概覽頁.
gitlab包 包會以更接近標準Linux公約的方式安裝Gitlab:
描述 | GitLab's Official | gitlab包 |
---|---|---|
配置文件 GitShell | /home/git/gitlab-shell/config.yml
|
/etc/webapps/gitlab-shell/config.yml
|
配置文件 GitLab | /home/git/gitlab/config/gitlab.yml
|
/etc/webapps/gitlab/gitlab.yml
|
用戶 (家目錄) | git (/home/git )
|
gitlab (/var/lib/gitlab )
|
GitLab[編輯 | 編輯原始碼]
編輯 /etc/webapps/gitlab/gitlab.yml
並至少設置下面的參數:
Hostname: 在 gitlab:
區裡設置 host:
- 把 localhost
換成 yourdomain.com
(注意: 不要加 'http://' 或者後面的斜線) - 到你的完全合格的域名.
Port: port:
可能會讓你很困擾. 這不是Gitlab伺服器運行的埠(unicorn) ; 這是用戶通過瀏覽器初次連接的埠. 基本上, 如果你有意讓用戶通過他們的瀏覽器訪問 'yourdomain.com' , 而且不用附加埠號, 把 port:
設成 80
. 如果你想讓用戶輸入比如 'yourdomain.com:3425' 這樣的東西到瀏覽器, 那麼你就得設置 port:
成 3425
. 你也會得設置你的網絡伺服器來監聽那個埠.
Timezone (時區,可選): time_zone:
參數是可選的, 但是它可能對強制Gitlab應用的區域有用.
最後設置正確的 permissions 到 uploads(上傳) 目錄:
# chmod 700 /var/lib/gitlab/uploads
為Puma自定義埠[編輯 | 編輯原始碼]
GitLab Puma 是處理大多數用戶請求的主要組件. 默認的, Unix套接字 /run/gitlab/gitlab.socket,可以通過 /etc/webapps/gitlab/puma.rb
文件來改變:
/etc/webapps/gitlab/puma.rb
bind 'unix:///run/gitlab/gitlab.socket' bind 'tcp://127.0.0.1:8080'
如果 Puma 地址被改變, 其它與Puma交流的組件的配置也需要被更新:
- 對於 GitLab Shell, 在
/etc/webapps/gitlab-shell/config.yml
更新gitlab_url
變量.
- 提示:根據注釋裡的配置文件, UNIX 套接字能通過 URL轉義斜槓(/) (即.
http+unix://%2Frun%2Fgitlab%2Fgitlab.socket
對應默認的/run/gitlab/gitlab.socket
). 額外的, 未轉義斜槓能用來指定 相關URL根路徑 (例如./gitlab
).
- 對於 GitLab Workhorse, edit
gitlab-workhorse.service
並更新-authBackend
選項.
機密字符串[編輯 | 編輯原始碼]
確定 /etc/webapps/gitlab/secret
和 /etc/webapps/gitlab-shell/secret
文件包含了些什麼. 它們的內容應該被保密因為它們會被用作生成認證令牌等.
比如, 能夠通過下面的命令生成隨機字符串:
# hexdump -v -n 64 -e '1/1 "%02x"' /dev/urandom > /etc/webapps/gitlab/secret # chmod 640 /etc/webapps/gitlab/secret
# hexdump -v -n 64 -e '1/1 "%02x"' /dev/urandom > /etc/webapps/gitlab-shell/secret # chmod 640 /etc/webapps/gitlab-shell/secret
同時需要填寫以下新的機密字符串
/etc/webapps/gitlab/secrets.yml
production: secret_key_base: secret db_key_base: secret otp_key_base: secret openid_connect_signing_key: secret
Redis[編輯 | 編輯原始碼]
為了提供足夠的性能,你可能需要一個緩存資料庫. 安裝並配置一個redis實例, 注意專用於通過套接字監聽的部分.
- 添加
gitlab
用戶到redis
user group.
- 更新配置文件:
/etc/webapps/gitlab/resque.yml
development: url: unix:/run/redis/redis.sock test: url: unix:/run/redis/redis.sock production: url: unix:/run/redis/redis.sock
/etc/webapps/gitlab-shell/config.yml
# Redis settings used for pushing commit notices to gitlab redis: bin: /usr/bin/redis-cli host: 127.0.0.1 port: 6379 # pass: redispass # Allows you to specify the password for Redis database: 5 # Use different database, default up to 16 socket: /run/redis/redis.sock # uncomment this line namespace: resque:gitlab
資料庫後端[編輯 | 編輯原始碼]
在Gitlab運行之前需要一個[PostgreSQL]資料庫.
PostgreSQL[編輯 | 編輯原始碼]
登錄 PostgreSQL 並創造 gitlabhq_production
資料庫和它的用戶一起. 記得改變 your_username_here
和 your_password_here
到你的真正的值:
# psql -d template1
template1=# CREATE USER your_username_here WITH PASSWORD 'your_password_here'; template1=# ALTER USER your_username_here SUPERUSER; template1=# CREATE DATABASE gitlabhq_production OWNER your_username_here; template1=# \q
用新用戶連接到新資料庫來確定它有用:
# psql -d gitlabhq_production
打開新的 /etc/webapps/gitlab/database.yml
並設置 username:
和 password:
的值. 比如:
/etc/webapps/gitlab/database.yml
# # PRODUCTION # production: main: adapter: postgresql encoding: unicode database: gitlabhq_production username: your_username_here password: "your_password_here" # host: localhost # port: 5432 socket: /run/postgresql/.s.PGSQL.5432 ci: adapter: postgresql encoding: unicode database: gitlabhq_production database_tasks: false username: your_username_here password: "your_password_here" # host: localhost # port: 5432 socket: /run/postgresql/.s.PGSQL.5432 ...
對於我們的目的 (除非你知道你在幹什麼),你不必擔心配置列在 /etc/webapps/gitlab/database.yml
的其它資料庫. 我們只需要設置生產的資料庫來讓Gitlab工作.
防火牆[編輯 | 編輯原始碼]
如果你想通過Iptables (簡體中文) 防火牆給予Gitlab安裝過程直接權限, 你可能需要調整埠和網絡地址:
# iptables -A tcp_inbound -p TCP -s 192.168.1.0/24 --destination-port 80 -j ACCEPT
啟用 API-access:
# iptables -A tcp_inbound -p TCP -s 192.168.1.0/24 --destination-port 8080 -j ACCEPT
如果你在一個路由器後面, 別忘了轉發這個埠到Gitlab伺服器埠, 如果你想運行 WAN-access的話.
初始化Gitlab資料庫[編輯 | 編輯原始碼]
在初始化資料庫之前開啟 Redis 伺服器和 gitlab-gitaly.service
.
初始化資料庫並激活更多特性:
$ cd /usr/share/webapps/gitlab $ sudo -u gitlab $(cat environment | xargs) bundle exec rake gitlab:setup GITLAB_ROOT_PASSWORD=yourpassword GITLAB_ROOT_EMAIL=youremail GITLAB_LICENSE_FILE=/path/to/license
最後運行下面的命令來檢查你的安裝:
$ sudo -u gitlab $(cat environment | xargs) bundle exec rake gitlab:env:info $ sudo -u gitlab $(cat environment | xargs) bundle exec rake gitlab:check
- gitlab:env:info 和 gitlab:check 命令會顯示一個和Git相關的嚴重的錯誤. 這是正常的.
- gitlab:check 會顯示缺少初始化腳本. 這也沒什麼好擔心的, 因為 systemd 服務文件會被相應使用 (而這Gitlab無法識別).
調整修改位[編輯 | 編輯原始碼]
(如果用戶和組所有權沒有正確配置的話Gitlab檢查不會通過)
# chmod -R ug+rwX,o-rwx /var/lib/gitlab/repositories/ # chmod -R ug-s /var/lib/gitlab/repositories # find /var/lib/gitlab/repositories/ -type d -print0 | xargs -0 chmod g+s
開始並測試Gitlab[編輯 | 編輯原始碼]
確定 PostgreSQL (簡體中文) 和 Redis 運行和設置正確.
然後 start/enable gitlab.target
.
現在你可以通過訪問 http://localhost:8080 或者 http://yourdomain.com來測試你的Gitlab[失效連結 2020-08-02 ⓘ], 你可能會被提示創建密碼:
username: root password: You'll be prompted to create one on your first visit.
查閱#故障排除和在 /usr/share/webapps/gitlab/log/
目錄下的日誌文件來 排除故障.
每次更新時升級資料庫[編輯 | 編輯原始碼]
手動操作[編輯 | 編輯原始碼]
在更新 gitlab包 包後, 需要升級資料庫:
$ cd /usr/share/webapps/gitlab $ sudo -u gitlab $(cat environment | xargs) bundle exec rake db:migrate
之後, 之後重載並重啟Gitlab相關服務: gitlab-sidekiq.service
, gitlab-puma.service
, gitlab-workhorse.service
和 gitlab-gitaly.service
自動操作[編輯 | 編輯原始碼]
你可以創建 pacman 鉤子來自動升級 GitLab 軟體包更新的資料庫。創建以下三個文件,不要忘記使 shell 腳本可執行:
/etc/pacman.d/hooks/05-gitlab-pre.hook
[Trigger] Operation = Upgrade Type = Package Target = gitlab [Action] Description = Stopping gitlab services When = PreTransaction Exec = /usr/bin/systemctl stop gitlab-gitaly.service gitlab-mailroom.service gitlab-puma.service gitlab-sidekiq.service gitlab-workhorse.service
/etc/pacman.d/hooks/99-gitlab-post.hook
[Trigger] Operation = Upgrade Type = Package Target = gitlab [Action] Description = Migrating GitLab database and starting services When = PostTransaction Exec = /etc/pacman.d/scripts/gitlab-migrate-database.sh
/etc/pacman.d/scripts/gitlab-migrate-database.sh
#!/bin/sh cd "/usr/share/webapps/gitlab" sudo -u gitlab $(cat environment | xargs) bundle exec rake db:migrate # The hook runs after 30-systemd-daemon-reload.hook so another systemctl daemon-reload is not necessary. systemctl start gitlab.target
更多配置[編輯 | 編輯原始碼]
基本的 SSH[編輯 | 編輯原始碼]
在完成了基本安裝後, 為用戶設置ssh權限. Secure Shell (簡體中文)的配置會在下面描述. 其它SSH客戶端和伺服器需要不同的調整.
獲取添加用戶SSH秘鑰的建議,過程在GitLab 網站裡描述的很好了. 你可以在 /var/lib/gitlab/log/gitlab-shell.log
檢查管理員日誌來確認用戶SSH秘鑰被正確提交了. 在這些事件之後, GitLab 添加這些秘鑰到authorized_keys 文件裡,它在 /var/lib/gitlab/.ssh/authorized_keys
.
測試秘鑰的常見方法 (比如: $ ssh -T git@YOUR_SERVER
) 需要一點額外配置才能正常工作. 在 /etc/webapps/gitlab/gitlab.yml
(默認用戶: gitlab
)裡配置的用戶必須添加到伺服器的sshd配置文件,除此之外還有幾個其它改變 :
/etc/ssh/sshd_config
PubkeyAuthentication yes AuthorizedKeysFile %h/.ssh/authorized_keys
更新配置文件之後, 重啟ssh守護進程:
# systemctl restart sshd
測試用戶SSH米喲啊 (可選添加 -v 來查看額外信息):
$ ssh -T gitlab@YOUR_SERVER
自定義SSH連接[編輯 | 編輯原始碼]
如果你在一個非標準埠運行SSH,你必須改變Gitlab用戶的SSH配置:
/var/lib/gitlab/.ssh/config
host localhost # Give your setup a name (here: override localhost) user gitlab # Your remote git user port 2222 # Your port number hostname 127.0.0.1; # Your server name or IP
你還必須在 /etc/webapps/gitlab/gitlab.yml
文件裡改變相應的選項 (比如. ssh_user, ssh_host, admin_uri) .
HTTPS/SSL[編輯 | 編輯原始碼]
改變Gitlab配置[編輯 | 編輯原始碼]
修改 /etc/webapps/gitlab/shell.yml
那樣到你的Gitlab站點的URL就會以 https://
開頭.
修改 /etc/webapps/gitlab/gitlab.yml
那樣 https:
設置就會被設為 true
.
查閱 Apache HTTP Server#TLS 和 Let’s Encrypt.
Let's Encrypt(讓我們加密吧)[編輯 | 編輯原始碼]
驗證你的URL, Let's Encrypt的過程會試圖用像 https://gitlab.YOUR_SERVER_FQDN/.well-known/acme-challenge/A_LONG_ID
的東西連接你的Gitlab伺服器. 但是, 因為Gitlab配置, 每個到 gitlab.YOUR_SERVER_FQDN
的請求會被重定向到一個代理 (gitlab-workhorse) 而它無法處理這個URL.
為了繞過這個問題, 你可以使用 Let's Encrypt的webroot配置, 在 /srv/http/letsencrypt/
設置webroot.
除此之外, 強迫到Gitlab的Let's Encrypt請求重定向到這個webroot可通過添加下面的:
/etc/http/conf/extra/gitlab.conf
Alias "/.well-known" "/srv/http/letsencrypt/.well-known" RewriteCond %{REQUEST_URI} !/\.well-known/.*
Web伺服器配置[編輯 | 編輯原始碼]
如果你想把Gitlab集成進一個運行的伺服器而不是用它的內置http伺服器Unicorn,那麼按照這些說明操作.
Node.js[編輯 | 編輯原始碼]
你可以輕鬆在443埠設置http代理來代理到8080埠的Gitlab程序的流量,通過為Node.js使用http-master. 在你創建你的域名的 OpenSSL 秘鑰並獲取你的CA 證書 (或自己設置的)後, 然後去 https://github.com/CodeCharmLtd/http-master 來學習使用https代理到Gitlab的請求多容易. http-master 建立在 node-http-proxy之上.
Nginx[編輯 | 編輯原始碼]
查閱 Nginx#Configuration 獲取基本的 nginx 配置信息和 Nginx#TLS 來啟用 HTTPS. 在這個部分的例子假設伺服器區塊是用 Nginx#Managing server entries管理的.
創建和編輯基於下面代碼的配置. 查閱 upstream GitLab repository獲取更多例子.
/etc/nginx/servers-available/gitlab
upstream gitlab-workhorse { server unix:/run/gitlab/gitlab-workhorse.socket fail_timeout=0; } server { listen 80; #listen 443 ssl; # uncomment to enable ssl server_name example.com #ssl_certificate ssl/example.com.crt; #ssl_certificate_key ssl/example.com.key; location / { # unlimited upload size in nginx (so the setting in GitLab applies) client_max_body_size 0; # proxy timeout should match the timeout value set in /etc/webapps/gitlab/unicorn.rb proxy_read_timeout 60; proxy_connect_timeout 60; proxy_redirect off; 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_pass http://gitlab-workhorse; } error_page 404 /404.html; error_page 422 /422.html; error_page 500 /500.html; error_page 502 /502.html; error_page 503 /503.html; location ~ ^/(404|422|500|502|503)\.html$ { root /usr/share/webapps/gitlab/public; internal; } }
Apache[編輯 | 編輯原始碼]
安裝並配置 Apache HTTP Server (簡體中文). 你可以使用這些 upstream recipes 來開始Gitlab虛擬主機的配置文件.
對於SSL配置查閱 Apache HTTP Server#TLS. 如果你不需要它, 移除它. 注意到SSL虛擬主機需要特定IP而不是通用IP. 同樣如果你為Unicorn設置了自定義埠, 不要忘了在 BalanceMember
行也設置它.
Gitlab-workhorse[編輯 | 編輯原始碼]
Gitlab8.0以後使用分開的http伺服器gitlab-workhorse包來應對大量的http請求比如 Git 推送/拉取. 如果你想使用這個而不是 SSH, 安裝 gitlab-workhorse包 包, 啟用 gitlab-workhorse.service
並為它配置web伺服器. gitlab-workhorse包 優先級應高於 gitlab-unicorn
根據 GitLab 的建議: https://gitlab.com/gitlab-org/gitlab-ce/issues/22528#note_16036216
gitlab-unicorn.service
.默認 gitlab-workhorse包 監聽 /run/gitlab/gitlab-workhorse.socket
. 你可以 edit gitlab-workhorse.service
並改變參數 -listenAddr
來讓它監聽一個地址, 例如 -listenAddr 127.0.0.1:8181
. 如果你監聽一個地址的話你也要把網絡類型設置為 -listenNetwork tcp
當你使用nginx時記得修改你的nginx配置文件. 從gitlab-unicorn 轉換到 gitlab-workhorse 相應的編輯以下兩個設置
/etc/nginx/servers-available/gitlab
upstream gitlab { server unix:/run/gitlab/gitlab-workhorse.socket fail_timeout=0; } ... proxy_pass http://unix:/run/gitlab/gitlab-workhorse.socket; } }
有用的建議[編輯 | 編輯原始碼]
隱藏選項[編輯 | 編輯原始碼]
到Gitlab的家目錄:
# cd /usr/share/webapps/gitlab
並運行:
# rake -T | grep gitlab
rake gitlab:app:check # GITLAB | Check the configuration of the GitLab Rails app rake gitlab:backup:create # GITLAB | Create a backup of the GitLab system rake gitlab:backup:restore # GITLAB | Restore a previously created backup rake gitlab:check # GITLAB | Check the configuration of GitLab and its environment rake gitlab:cleanup:block_removed_ldap_users # GITLAB | Cleanup | Block users that have been removed in LDAP rake gitlab:cleanup:dirs # GITLAB | Cleanup | Clean namespaces rake gitlab:cleanup:repos # GITLAB | Cleanup | Clean repositories rake gitlab:env:check # GITLAB | Check the configuration of the environment rake gitlab:env:info # GITLAB | Show information about GitLab and its environment rake gitlab:generate_docs # GITLAB | Generate sdocs for project rake gitlab:gitlab_shell:check # GITLAB | Check the configuration of GitLab Shell rake gitlab:import:all_users_to_all_groups # GITLAB | Add all users to all groups (admin users are added as owners) rake gitlab:import:all_users_to_all_projects # GITLAB | Add all users to all projects (admin users are added as masters) rake gitlab:import:repos # GITLAB | Import bare repositories from gitlab_shell -> repos_path into GitLab project instance rake gitlab:import:user_to_groups[email] # GITLAB | Add a specific user to all groups (as a developer) rake gitlab:import:user_to_projects[email] # GITLAB | Add a specific user to all projects (as a developer) rake gitlab:satellites:create # GITLAB | Create satellite repos rake gitlab:setup # GITLAB | Setup production application rake gitlab:shell:build_missing_projects # GITLAB | Build missing projects rake gitlab:shell:install[tag,repo] # GITLAB | Install or upgrade gitlab-shell rake gitlab:shell:setup # GITLAB | Setup gitlab-shell rake gitlab:sidekiq:check # GITLAB | Check the configuration of Sidekiq rake gitlab:test # GITLAB | Run all tests rake gitlab:web_hook:add # GITLAB | Adds a web hook to the projects rake gitlab:web_hook:list # GITLAB | List web hooks rake gitlab:web_hook:rm # GITLAB | Remove a web hook from the projects rake setup # GITLAB | Setup gitlab db
備份和恢復[編輯 | 編輯原始碼]
創建Gitlab系統的備份:
# sudo -u gitlab -H rake RAILS_ENV=production gitlab:backup:create
從之前創建的備份文件恢復 /home/gitlab/gitlab/tmp/backups/20130125_11h35_1359131740_gitlab_backup.tar
:
# sudo -u gitlab -H rake RAILS_ENV=production gitlab:backup:restore BACKUP=/home/gitlab/gitlab/tmp/backups/20130125_11h35_1359131740
config/gitlab.yml
設置. GitLab 備份和恢復記錄在 這裡.通過SMTP從Gitlab發送郵件[編輯 | 編輯原始碼]
你可能想用 gmail (或者其它郵件服務) 從你的Gitlab寄郵件. 這能避免在Gitlab伺服器上安裝郵件守護進程的需要.
根據你的郵件伺服器設置調整 smtp_settings.rb
:
/usr/share/webapps/gitlab/config/initializers/smtp_settings.rb
if Rails.env.production? Gitlab::Application.config.action_mailer.delivery_method = :smtp ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'gmail.com', user_name: 'username@gmail.com', password: 'application password', authentication: 'plain', enable_starttls_auto: true } end
Gmail 會拒絕這樣接受的郵件 (並會郵寄給你一個郵件它拒絕了). 你需要關閉安全認證 (按照拒絕郵件中的連結) 來解決這個問題. 更安全的辦法是為username@gmail.com開啟雙因素認證並為這個配置文件設置應用密碼.
故障排除[編輯 | 編輯原始碼]
HTTPS不是綠色 (gravatar不使用 https)[編輯 | 編輯原始碼]
Redis 緩存了 gravatar 圖像, 如果你用http拜訪 GitLab, 那麼啟用 https, gravatar 會加載不安全的圖像. 你可以清楚這些緩存通過執行以下命令
cd /usr/share/webapps/gitlab RAILS_ENV=production bundle-2.3 exec rake cache:clear
以Gitlab用戶的身份.
更新後的錯誤[編輯 | 編輯原始碼]
從AUR更新安裝包後, 資料庫遷移和資源更新有時候會失敗. 這些步驟可以解決這些問題, 如果簡單的開關機不行的話.
首先, 移動到Gitlab安裝目錄.
# cd /usr/share/webapps/gitlab
如果每個Gitlab頁面都給了一個500頁面, 那麼資料庫遷移和資源可能太陳舊了. 如果不是的話, 跳過這步.
# su - gitlab -s /bin/sh -c "cd '/usr/share/webapps/gitlab'; bundle-2.3 exec rake db:migrate RAILS_ENV=production"
如果Gitlab一直在等待布置完成的話, 那麼資源可能沒有被重新編譯.
# su - gitlab -s /bin/sh -c "cd '/usr/share/webapps/gitlab'; bundle-2.3 exec rake gitlab:assets:clean gitlab:assets:compile cache:clear RAILS_ENV=production"
最後, 重啟Gitlab服務再測試你的網站.
# systemctl restart gitlab-unicorn gitlab-sidekiq gitlab-workhorse
Gitlab-Unicorn 無法訪問非默認的從庫目錄[編輯 | 編輯原始碼]
如果自定義的倉庫存儲目錄被設置在 /home
, 在gitlab-unicorn.service
禁用 ProtectHome=true
參數 (查閱 Systemd#Drop-in files 和 github.com上的相關論壇帖子).
無法連接到 Gitaly[編輯 | 編輯原始碼]
有時候, Gitaly服務無法開始, 讓gitlab無法連接到gitlay. 解決辦法很簡單:
# systemctl start gitlab-gitaly