gitlab搭建之旅


gitlab搭建之旅

前言:由於公司項目需求,需要自行維護一套git環境,調研了目前現有的git托管工具,最終確定使用gitlab這個開源平台。So,之后就嘗試搭建一套測試環境,不過此中過程並非一帆風順(雖早有心理准備),確是經歷了一番波折。為了提高后續的部署效率,避免重復錯誤,在此記個隨筆以備忘。

搭建環境:

服務器 -- RedHat 5.4

內核版本 -- linux 2.6.18 x86_64

參考文檔:https://github.com/gitlabhq/gitlabhq/blob/stable/doc/install/installation.md 基本上是照着官方這個文檔做的,不過官方文檔的標准環境是debian環境,所以會有些微區別

概要:

gitlab的安裝包括以下幾步:

  1. 安裝依賴包
  2. 安裝Ruby
  3. 創建系統賬戶
  4. 安裝Gitolite
  5. 搭建數據庫環境
  6. 搭建GitLab
  7. 搭建Nginx

1. 安裝依賴包

搭建gitlab環境需要安裝以下庫: (這些安裝包都需要事先檢查下,否則在后面會出現返工的問題,缺少這些包在編譯ruby的時候不會報錯,但是在搭建gitlab環境的時候會提示你缺少XXX庫,然后還要重新編譯ruby,很麻煩

yum groupinstall "Development Tools"  #等同於 apt-get install build-essential,如果沒有此group,則分別安裝make.gcc,g++,libc等開發包
yum install kernel-devel kernel-headers
yum install zlib-devel.x86_64
yum install libyaml.x86_64
yum install openssl-devel.x86_64
yum install gdbm-devel.x86_64
yum install readline-devel.x86_64
yum install ncurses-devel.x86_64
yum install libffi-devel.x86_64 #可能需要手動下載rpm包安裝
yum install git.x86_64
yum install curl.x86_64
yum install openssh-server.x86_64
yum install redis.x86_64
yum install postfix.x86_64
yum install libxml2-devel.x86_64
yum install libxslt-devel.x86_64
yum install curl-devel.x86_64
yum install libicu-devel.x86_64
yum install mysql-devel.x86_64

2. 安裝ruby (必須是1.9.3+版本

mkdir /tmp/ruby && cd /tmp/ruby
curl --progress http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p327.tar.gz | tar xz
cd ruby-1.9.3-p327
./configure
make
sudo make install
  Install the Bundler Gem:
sudo gem install bundler

 

3.創建系統賬戶

sudo useradd -r -m git

sudo useradd -g git gitlab

#Add x privilege to /home/git
sudo chmod g+x /home/git
# Generate the SSH key sudo -u gitlab -H ssh-keygen -q -N '' -t rsa -f /home/gitlab/.ssh/id_rsa

4. 安裝Gitolite

下載gitolite的源代碼

cd /home/git
sudo -u git -H git clone -b gl-v320 git://github.com/gitlabhq/gitolite.git /home/git/gitolite

安裝gitolite

# Add Gitolite scripts to $PATH
sudo -u git -H mkdir /home/git/bin
sudo -u git -H sh -c 'printf "%b\n%b\n" "PATH=\$PATH:/home/git/bin" "export PATH" >> /home/git/.profile'
sudo -u git -H sh -c 'gitolite/install -ln /home/git/bin'

# Copy the gitlab user's (public) SSH key ...
sudo cp /home/gitlab/.ssh/id_rsa.pub /home/git/gitlab.pub
sudo chmod 0444 /home/git/gitlab.pub

# ... and use it as the admin key for the Gitolite setup
sudo -u git -H sh -c "PATH=/home/git/bin:$PATH; gitolite setup -pk /home/git/gitlab.pub"

配置gitolite相關路徑的權限

# Make sure the Gitolite config dir is owned by git
sudo chmod 750 /home/git/.gitolite/
sudo chown -R git:git /home/git/.gitolite/

配置倉庫路徑的權限

# Make sure the repositories dir is owned by git and it stays that way
sudo chmod -R ug+rwXs,o-rwx /home/git/repositories/
sudo chown -R git:git /home/git/repositories/
sudo chmod -R ug-s /home/git/repositories/
find /home/git/repositories/ -type d -print0 | sudo xargs -0 chmod g+s

將域名加到gitlab賬戶的known_hosts列表中

sudo -u gitlab -H ssh git@localhost
sudo -u gitlab -H ssh git@YOUR_DOMAIN_NAME
sudo -u gitlab -H ssh git@YOUR_GITOLITE_DOMAIN_NAME

測試是否安裝成功

# Clone the admin repo so SSH adds localhost to known_hosts ...
# ... and to be sure your users have access to Gitolite
sudo -u gitlab -H git clone git@localhost:gitolite-admin.git /tmp/gitolite-admin

# If it succeeded without errors you can remove the cloned repo
sudo rm -rf /tmp/gitolite-admin

如果測試失敗,不要繼續往下走,請查看https://github.com/gitlabhq/gitlab-public-wiki/wiki/Trouble-Shooting-Guide幫助解決問題

5. 搭建數據庫

gitlab支持兩種數據庫:Mysql和PostgreSQL

Mysql:

# Install the database packages
sudo yum install mysql.x86_64 mysql-devel.x86_64 mysql-server.x86_64

# Login to MySQL
$ mysql -u root -p

# Create a user for GitLab. (change $password to a real password)
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY '$password';

# Create the GitLab production database
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;

# Grant the GitLab user necessary permissopns on the table.
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';

# Quit the database session
mysql> \q

# Try connecting to the new database with the new user
sudo -u gitlab -H mysql -u gitlab -p -D gitlabhq_production

PostgreSQL:

# Install the database packages
sudo yum install postgresql.x86_64 postgresql-devel.x86_64 postgresql-server.x86_64

# Login to PostgreSQL
sudo -u postgres psql -d template1

# Create a user for GitLab. (change $password to a real password)
template1=# CREATE USER gitlab WITH PASSWORD '$password';

# Create the GitLab production database & grant all privileges on database
template1=# CREATE DATABASE gitlabhq_production OWNER gitlab;

# Quit the database session
template1=# \q

# Try connecting to the new database with the new user
sudo -u gitlab -H psql -d gitlabhq_production

6. 搭建GitLab

下載代碼:

# We'll install GitLab into home directory of the user "gitlab"
cd /home/gitlab

# Clone GitLab repository
sudo -u gitlab -H git clone git://github.com/gitlabhq/gitlabhq.git gitlab

# Go to gitlab dir 
cd /home/gitlab/gitlab

# Checkout to stable release
sudo -u gitlab -H git checkout 4-1-stable

設置配置項:

cd /home/gitlab/gitlab

# Copy the example GitLab config
sudo -u gitlab -H cp config/gitlab.yml.example config/gitlab.yml

# Make sure to change "localhost" to the fully-qualified domain name of your
# host serving GitLab where necessary
sudo -u gitlab -H vim config/gitlab.yml

# Make sure GitLab can write to the log/ and tmp/ directories
sudo chown -R gitlab log/
sudo chown -R gitlab tmp/
sudo chmod -R u+rwX  log/
sudo chmod -R u+rwX  tmp/

# Make directory for satellites
sudo -u gitlab -H mkdir /home/gitlab/gitlab-satellites

# Copy the example Unicorn config
sudo -u gitlab -H cp config/unicorn.rb.example config/unicorn.rb

配置數據庫:

# Mysql
sudo -u gitlab cp config/database.yml.mysql config/database.yml

# PostgreSQL
sudo -u gitlab cp config/database.yml.postgresql config/database.yml

//待續…

安裝Gems:

cd /home/gitlab/gitlab

sudo gem install charlock_holmes --version '0.6.9'

# For MySQL (note, the option says "without")
sudo -u gitlab -H bundle install --deployment --without development test postgres

# Or for PostgreSQL
sudo -u gitlab -H bundle install --deployment --without development test mysql

配置Git:

gitlab需要能夠提交代碼到gitolite,所以我們需要設置一個全局的用戶信息: email和username (建議直接使用config/gitlab.yml配置文件中的email.from值)

sudo -u gitlab -H git config --global user.name "GitLab"
sudo -u gitlab -H git config --global user.email "gitlab@localhost"

設置Gitlab的Hooks:

sudo cp ./lib/hooks/post-receive /home/git/.gitolite/hooks/common/post-receive
sudo chown git:git /home/git/.gitolite/hooks/common/post-receive

初始化數據庫和激活高級特性:

sudo -u gitlab -H bundle exec rake gitlab:setup RAILS_ENV=production

安裝啟動腳本:

sudo curl --output /etc/init.d/gitlab https://raw.github.com/gitlabhq/gitlab-recipes/4-1-stable/init.d/gitlab
sudo chmod +x /etc/init.d/gitlab

檢查應用狀態:

檢查gitlab和其運行環境是否配置正確:

sudo -u gitlab -H bundle exec rake gitlab:env:info RAILS_ENV=production

確保沒有遺漏:

sudo -u gitlab -H bundle exec rake gitlab:check RAILS_ENV=production

如果所有的檢查結果都是綠色,那個恭喜你已經成功安裝了Gitlab,但是下面仍有一些工作要完成

啟動Mysql

sudo service mysql start
# or
sudo /etc/init.d/mysql start

啟動Redis

sudo service redis-server start
# or
sudo /etc/init.d/redis restart

啟動GitLab

sudo service gitlab start
# or
sudo /etc/init.d/gitlab start

7. 搭建Nginx

安裝:

sudo yum install nginx

設置配置文件:

#Download an example site config:
sudo curl --output /etc/nginx/conf.d/gitlab.conf https://raw.github.com/gitlabhq/gitlab-recipes/4-1-stable/nginx/gitlab

#Make sure to edit the config file to match your setup:
# Change **YOUR_SERVER_IP** and **YOUR_SERVER_FQDN**
# to the IP address and fully-qualified domain name
# of your host serving GitLab
sudo vim /etc/nginx/conf.d/gitlab.conf

啟動Nginx:

sudo service nginx start
sudo /etc/init.d/nginx start

8. 搭建完成

現在你可以使用管理員賬戶訪問你的gitlab網站了:

admin@local.host
5iveL!fe

9. FQA

1. 用http協議上傳代碼的時候會hung住,服務器也沒反應?

    這是因為客戶端上傳的代碼太大,超過了服務器和客戶端的限制

    解決方案:

    增加客戶端的buffer大小   

git config http.postBuffer 524288000

  增加服務端的限制  conf/gitlab.xml

git:
  bin_path: /usr/bin/git
  # Max size of git object like commit, in bytes
  # This value can be increased if you have a very large commits
  max_size: 524288000 # 500.megabytes
  # Git timeout to read commit, in seconds
  timeout: 10

2.  用http協議push時服務器報411錯誤

     這是因為nginx的http頭檢查為通過

     解決方案:重新編譯nginx,添加chunkin-nginx-module:  https://github.com/agentzh/chunkin-nginx-module

3.  用http協議push時服務器包413錯誤

     這是因為push文件大小超過了nginx的限制

     解決方案:

     修改nginx配置 /etc/nginx.conf

client_max_body_size  200M;

4. 在線瀏覽代碼的時候,當遇到*.md文件,報錯 DistributionNotFound: MarkupSafe>=0.9.2

    這是因為本機的python缺少MarkupSafe庫

    解決方案:

    從https://pypi.python.org/pypi/MarkupSafe上下載最新的庫安裝

5. 在線提交代碼時報錯:Gitlab::SatelliteNotExistError (Satellite doesn't exist)

    可能是post-receive中的命令執行出錯:

    vi /home/git/.gitolite/hooks/common/post-receive , 確定redis-cli的路徑正確

#!/usr/bin/env bash

# Version 4.1
# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.

while read oldrev newrev ref
do
  # For every branch or tag that was pushed, create a Resque job in redis.
  repo_path=`pwd`
  env -i /usr/local/bin/redis-cli rpush "resque:gitlab:queue:post_receive" "{\"class\":\"PostReceive\",\"args\":[\"$repo_path\",\"$oldrev\",\"$newrev\",\"$ref\",\"$GL_USER\"]}" > /dev/null 2>&1
done

  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM