2013/06/02

RHEL 6 安裝 Gitlab [Old]

Gitlab(a.k.a. Gitlabhq)

Gitlab 是一套以 gitolite 爲基礎的 Git Hosting Website,
Gitlab 是用 Ruby on Rails 框架開發的,需要合適的 Ruby 環境。

作業系統:RHEL 6.0 i386

[前置安裝]

wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm && rpm -i *.rpm

# yum remove gitosis

yum groupinstall "Development Tools"
yum install libxml2-devel libxslt-devel  libxml2 libxslt
yum install git openssl zlib subversion
yum install sqlite sqlite-devel 
yum install mysql++-devel mysql++ 
yum install readline readline-devel compat-readline5 
yum install libcurl libcurl-devel libstdc++ libstdc++-devel libstdc++-docs compat-libstdc++-33 curl
yum install gcc-c++ patch zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel sudo
yum install libicu 

# http://pkgs.org/download/libicu-devel
wget http://mirror.centos.org/centos/6/os/i386/Packages/libicu-devel-4.2.1-9.el6.i686.rpm
rpm -ivh libicu-devel-4.2.1-9.el6.i686.rpm

yum install python-pip
pip-python install pygments

yum install redis
chkconfig redis on
service redis start

[安裝 Ruby]

# using RVM to install Ruby
# install RVM in system wide

su - 

curl -L get.rvm.io | bash -s stable
source ~/.bash_profile
rvm requirements
rvm install 1.9.2
rvm use 1.9.2 --default

which ruby
which gem

[設定 Gitolite]

adduser -r -s /bin/sh -c 'git version control' -U -m git
adduser -c 'gitlab system' -U -m gitlab

usermod -a -G git gitlab
usermod -a -G rvm gitlab
usermod -a -G rvm git

chmod 755 /home/git
chmod 755 /home/gitlab

logout & login

sudo -H -u gitlab ssh-keygen -q -N '' -t rsa -f /home/gitlab/.ssh/id_rsa
sudo -H -u git git clone http://github.com/gitlabhq/gitolite /home/git/gitolite

sudo -u git sh -c 'echo -e "PATH=\$PATH:/home/git/bin\nexport PATH" > /home/git/.profile'
sudo -u git -i -H /home/git/gitolite/src/gl-system-install
cp /home/gitlab/.ssh/id_rsa.pub /home/git/gitlab.pub
chmod 777 /home/git/gitlab.pub

sudo -u git -H sed -i 's/0077/0007/g' /home/git/share/gitolite/conf/example.gitolite.rc
sudo -u git -H sh -c "PATH=/home/git/bin:$PATH; gl-setup -q /home/git/gitlab.pub"

chmod -R g+rwX /home/git/repositories/
chown -R git:git /home/git/repositories/

# check if gitolite works
# If you CAN NOT clone gitolite-admin repository - DONT PROCEED INSTALLATION
su - gitlab
git clone git@localhost:gitolite-admin.git /tmp/gitolite-admin
rm -rf /tmp/gitolite-admin 
exit

http://sitaramc.github.com/gitolite/
http://sitaramc.github.com/gitolite/rpmdeb.html
http://sitaramc.github.com/gitolite/add.html

[設定 Gitlab]

gem install charlock_holmes
gem install bundler

su - gitlab

cd /home/gitlab
git clone http://github.com/gitlabhq/gitlabhq.git gitlab

cd gitlab
cp config/gitlab.yml.example config/gitlab.yml # using default website setting
cp config/database.yml.sqlite config/database.yml # using SQLite as database

bundle install --without development test --deployment

bundle exec rake gitlab:app:setup RAILS_ENV=production
bundle exec rake gitlab:app:status RAILS_ENV=production

bundle exec rake environment resque:work QUEUE=* RAILS_ENV=production BACKGROUND=yes
bash /home/gitlab/gitlab/resque.sh

bundle exec rails s -e production # test running
bundle exec rails s -e production -d # test running as a daemon

測試網址
http://localhost:3000

預設的帳號密碼
admin@local.host / 5iveL!fe

https://github.com/gitlabhq/gitlabhq/blob/stable/doc/installation.md
http://dl.dropbox.com/u/936096/debian_ubuntu.sh

[設定 Nginx]

yum install nginx

su - gitlab
cd ~/gitlab
cp config/unicorn.rb.orig config/unicorn.rb
bundle exec unicorn_rails -c config/unicorn.rb -E production -D

exit

編輯 /etc/nginx/nginx.conf,在 http { 後加入以下內容:

# 注意修改 YOUR_SERVER_IP 與 YOUR_SUBDOMAIN

upstream gitlab {
    server unix:/home/gitlab/gitlab/tmp/sockets/gitlab.socket;
}

server {
    listen YOUR_SERVER_IP:80;
    server_name YOUR_DOMAIN;
    root /home/gitlab/gitlab/public;

    # individual nginx logs for this gitlab vhost
    access_log  /var/log/nginx/gitlab_access.log;
    error_log   /var/log/nginx/gitlab_error.log;

    location / {
    # serve static files from defined root folder;.
    # @gitlab is a named location for the upstream fallback, see below
    try_files $uri $uri/index.html $uri.html @gitlab;
    }

    # if a file, which is not found in the root folder is requested, 
    # then the proxy pass the request to the upsteam (gitlab unicorn)
    location @gitlab {
      proxy_redirect     off;
      # you need to change this to "https", if you set "ssl" directive to "on"
      proxy_set_header   X-FORWARDED_PROTO http;
      proxy_set_header   Host              YOUR_SUBDOMAIN:80;
      proxy_set_header   X-Real-IP         $remote_addr;

      proxy_pass http://gitlab;
    }

}

編輯 /etc/init.d/gitlab

#! /bin/bash
### BEGIN INIT INFO
# Provides:          gitlab
# Required-Start:    $local_fs $remote_fs $network $syslog redis-server
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: GitLab git repository management
# Description:       GitLab git repository management
### END INIT INFO

DAEMON_OPTS="-c /home/gitlab/gitlab/config/unicorn.rb -E production -D"
NAME=unicorn
DESC="Gitlab service"
PID=/home/gitlab/gitlab/tmp/pids/unicorn.pid
RESQUE_PID=/home/gitlab/gitlab/tmp/pids/resque_worker.pid

case "$1" in
  start)
        CD_TO_APP_DIR="cd /home/gitlab/gitlab"
        START_DAEMON_PROCESS="bundle exec unicorn_rails $DAEMON_OPTS"
        START_RESQUE_PROCESS="./resque.sh"

        echo -n "Starting $DESC: "
        if [ `whoami` = root ]; then
          sudo -u gitlab sh -l -c "$CD_TO_APP_DIR > /dev/null 2>&1 && $START_DAEMON_PROCESS && $START_RESQUE_PROCESS"
        else
          $CD_TO_APP_DIR > /dev/null 2>&1 && $START_DAEMON_PROCESS && $START_RESQUE_PROCESS
        fi
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        kill -QUIT `cat $PID`
        kill -QUIT `cat $RESQUE_PID`
        echo "$NAME."
        ;;
  restart)
        echo -n "Restarting $DESC: "
        kill -USR2 `cat $PID`
        kill -USR2 `cat $RESQUE_PID`
        echo "$NAME."
        ;;
  reload)
        echo -n "Reloading $DESC configuration: "
        kill -HUP `cat $PID`
        kill -HUP `cat $RESQUE_PID`
        echo "$NAME."
        ;;
  *)
        echo "Usage: $NAME {start|stop|restart|reload}" >&2
        exit 1
        ;;
esac

exit 0

避免 SSH 主機信任問題

su - gitlab
ssh git@YOUR_DOMAIN

The authenticity of host 'YOUR_DOMAIN (YOUR_IP)' can't be established.  
RSA key fingerprint is 98:2e:d7:e0:de:9f:ac:67:28:c2:42:2d:37:16:58:4d.
Are you sure you want to continue connecting (yes/no)? 

# 回答 yes 表示信任該主機

注意!目前 Gitlab 有一個 README 顯示 utf8 的 bug
https://github.com/gitlabhq/gitlabhq/issues/725#issuecomment-5669823

測試啓動

chmod +x /etc/init.d/gitlab
/etc/init.d/nginx restart
/etc/init.d/gitlab restart

設定開機啓動

yum install postfix
chkconfig postfix on
chkconfig nginx on
echo "/etc/init.d/gitlab start" >> /etc/rc.local

Reference:
http://zx-1986.blogspot.com/2011/11/gitlabhq.html
http://zx-1986.blogspot.com/2011/11/gitlabhq-in-redhat.html

沒有留言:

張貼留言