วันพุธที่ 31 สิงหาคม พ.ศ. 2554

Install Gitorious บน Ubuntu 10.04 LTS

Share it Please
ก่อนอื่นต้องติดตั้ง package ที่จำเป็นก่อน
apt-get install -y build-essential apache2 apache2-dev libapache2-mod-xsendfile git-core git-doc mysql-server mysql-client libmysqlclient15-dev phpmyadmin ruby-dev rubygems libopenssl-ruby libdbd-mysql-ruby libmysql-ruby libexpat-dev libcurl4-openssl-dev postfix apg geoip-bin libgeoip1 libgeoip-dev imagemagick libmagick++-dev libpcre3 libpcre3-dev zlib1g zlib1g-dev zip unzip libyaml-dev libonig-dev memcached irb aspell libaspell-dev aspell-en
update gem ให้เป็น version ล่าสุดเพราะว่ามี default gem version ต่ำเกินไป
gem install rubygems-update
cd /var/lib/gems/1.8/bin
./update_rubygems
ติดตั้ง ruby gems ที่ต้องจำเป็น
gem install -b --no-ri --no-rdoc rmagick chronic geoip daemons hoe echoe ruby-yadis ruby-openid mime-types diff-lcs json ruby-hmac rake stompserver passenger rails raspell
gem install -b --no-ri --no-rdoc -v 1.0.1 rack
gem install -b --no-ri --no-rdoc -v 1.3.1.1 rdiscount
gem install -b --no-ri --no-rdoc -v 1.1 stomp
ทำ symlinks ให้กับ ruby
ln -s /usr/bin/ruby1.8 /usr/bin/ruby
ติดตั้ง Sphinx
mkdir src
cd src
wget http://sphinxsearch.com/downloads/sphinx-0.9.8.1.tar.gz
tar -xvzf sphinx-0.9.8.1.tar.gz
cd sphinx-0.9.8.1/
./configure --prefix=/usr && make all
sudo make install
clone Gitorious
mkdir -p /var/www/gitorious.osdev.co.th
chmod ugo+rwx /var/www/gitorious.osdev.co.th
cd /var/www/gitorious.osdev.co.th/
git clone git://gitorious.org/gitorious/mainline.git gitorious
Configure Services

สร้าง init script ไฟล์ git-daemon
vim /etc/init.d/git-daemon
#! /bin/sh
### BEGIN INIT INFO
# Provides:          Gitorious GIT-Daemon
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: GIT-Daemon server daemon
# Description:       Starts the GIT-Daemon needed by Gitorious
### END INIT INFO

# Author: Fabio Akita fabioakita@gmail.com

RETVAL=0
PROG="git-daemon"
GITORIOUS_ROOT=/var/www/gitorious.osdev.co.th/gitorious
GIT_DAEMON="/usr/bin/ruby $GITORIOUS_ROOT/script/git-daemon -d"
LOCK_FILE=/var/lock/git-daemon
PID_FILE=$GITORIOUS_ROOT/tmp/pids/git-daemon.pid

do_check_pid() {
if [ -f $PID_FILE ]; then
PID=`cat $PID_FILE`
RUNNING=`ps --pid $PID | wc -l`
else
PID=0
RUNNING=0
fi
}

runlevel=`runlevel | awk '{print $2}'`

start()
{
do_check_pid
if [ $RUNNING != 2 ] ; then
echo -n "Starting $PROG: "
/bin/su - git -c "$GIT_DAEMON"
sleep 10
if [ -f $PID_FILE ] ; then
echo "success"
RETVAL=0
else
echo "failure"
RETVAL=1
fi
else
echo -n "$PROG already running"
RETVAL=1
fi
[ "$RETVAL" = 0 ] && touch $LOCK_FILE
echo
}

stop()
{
do_check_pid
echo -n "Stopping $PROG: "
if [ $RUNNING != 2 ] ; then
echo -n "Stopping $PROG"
else
PROGPID=`cat $PID_FILE`
kill -TERM $PROGPID
fi
RETVAL=0
# if we are in halt or reboot runlevel kill all running sessions
# so the TCP connections are closed cleanly
if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
PROGPID=`cat $PID_FILE`
kill -9 $PROGPID > /dev/null
fi
[ "$RETVAL" = 0 ] && rm -f $LOCK_FILE && rm -f $PID_FILE
echo
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
condrestart)
if [ -f $LOCK_FILE ] ; then
if [ "$RETVAL" = 0 ] ; then
stop
# avoid race
sleep 10
start
fi
fi
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart}"
RETVAL=1
esac
exit $RETVAL
สร้าง init script ไฟล์ git-ultrasphinx
vim /etc/init.d/git-ultrasphinx
#! /bin/sh
### BEGIN INIT INFO
# Provides:          Gitorious Ultrasphinx
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Ultrasphinx daemon
# Description:       Starts the Ultrasphinx daemons needed by Gitorious
### END INIT INFO

# Author: Fabio Akita fabioakita@gmail.com

RETVAL=0
GITORIOUS_ROOT=/var/www/gitorious.osdev.co.th/gitorious

START_CMD="cd $GITORIOUS_ROOT && rake ultrasphinx:daemon:start RAILS_ENV=production"
STOP_CMD="cd $GITORIOUS_ROOT && rake ultrasphinx:daemon:stop RAILS_ENV=production"
RESTART_CMD="cd $GITORIOUS_ROOT && rake ultrasphinx:daemon:restart RAILS_ENV=production"
STATUS_CMD="cd $GITORIOUS_ROOT && rake ultrasphinx:daemon:status RAILS_ENV=production"
LOCK_FILE=/var/lock/git-ultrasphinx
PID_FILE=$GITORIOUS_ROOT/db/sphinx/log/searchd.pid

case "$1" in
start)
echo `date` " Starting git-ultrasphinx" >> "$GITORIOUS_ROOT/log/git-ultrasphinx.log"
/bin/su -- git -c "$START_CMD"
;;
stop)
echo `date`" Stopping git-ultrasphinx" >> "$GITORIOUS_ROOT/log/git-ultrasphinx.log"
/bin/su -- git -c "$STOP_CMD"
;;
status)
echo `date`" Status For  git-ultrasphinx" >> "$GITORIOUS_ROOT/log/git-ultrasphinx.log"
/bin/su -- git -c "$STATUS_CMD"
;;
restart)
echo `date`" ReStarting git-ultrasphinx" >> "$GITORIOUS_ROOT/log/git-ultrasphinx.log"
/bin/su -- git -c "$RESTART_CMD"
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
RETVAL=1
esac
exit $RETVAL
สร้าง init script ไฟล์ git-poller
vim /etc/init.d/git-poller
#!/bin/sh
# Start/stop the git poller
#
### BEGIN INIT INFO
# Provides: git-poller
# Required-Start: stomp
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 1
# Short-Description: Gitorious poller
# Description: Gitorious poller
### END INIT INFO

GITORIOUS_ROOT=/var/www/gitorious.osdev.co.th/gitorious
echo  `date` " Starting git-poller" >> "$GITORIOUS_ROOT/log/git-poller.log"
/bin/su -- git -c " cd $GITORIOUS_ROOT; RAILS_ENV=production script/poller $@"

สร้าง init script ไฟล์ stomp
vim /etc/init.d/stomp
#!/bin/sh
# Start/stop the stompserver
# WATCH THE LINE WRAPPING BELOW
### BEGIN INIT INFO
# Provides: stomp
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 1
# Short-Description: Stomp
# Description: Stomp
### END INIT INFO
test -f /usr/bin/stompserver || exit 0
. /lib/lsb/init-functions
case $1 in
start) log_daemon_msg Starting stompserver stompserver
start-stop-daemon --start --name stompserver --startas /usr/bin/stompserver \
--background --user git
log_end_msg $?
;;
stop) log_daemon_msg Stopping stompserver stompserver
start-stop-daemon --stop --name stompserver
log_end_msg $?
;;
restart) log_daemon_msg Restarting stompserver stompserver
start-stop-daemon --stop --retry 5 --name stompserver
start-stop-daemon --start --name stompserver --startas /usr/bin/stompserver \
--background --user git
log_end_msg $?
;;
status)
status_of_proc /usr/bin/stompserver stompserver && exit 0 || exit $?
;;
*) log_action_msg Usage: /etc/init.d/stomp {start|stop|restart|status}
exit 2
;;
esac
exit 0
copy ไฟล์จาก gitorious ultrasphinx plugin ไปไว้ที่ aspell library
cp /var/www/gitorious.osdev.co.th/gitorious/vendor/plugins/ultrasphinx/examples/ap.multi /usr/lib/aspell/
สร้าง gitorious logrotate file
vim /etc/logrotate.d/gitorious
โดยมี content
/var/www/gitorious.osdev.co.th/gitorious/log/*log {
missingok
notifempty
sharedscripts
postrotate
/etc/init.d/git-daemon restart > /dev/null 2>/dev/null || true
/etc/init.d/git-ultrasphinx restart > /dev/null 2>/dev/null || true
/bin/touch /var/www/gitorious.osdev.co.th/gitorious/tmp/restart.txt > /dev/null 2>/dev/null || true
endscript
}
สร้าง git system user
adduser --system --home /home/git/ --group --shell /bin/bash g
chown -R git:git /home/git
passwd git #ใส่ password ตามต้องการ
sudo usermod -a -G git www-data
sudo chown -R git:git /var/www/gitorious.osdev.co.th
set init script ให้รันเมื่อ start up
cd /var/www/gitorious.osdev.co.th/gitorious
chown -R git:git config/environment.rb script/poller log tmp
chmod -R g+w config/environment.rb script/poller log tmp
chmod ug+x script/poller

chown git:git /etc/init.d/git-ultrasphinx
chown git:git /etc/init.d/git-daemon
chown git:git /etc/init.d/stomp
chown git:git /etc/init.d/git-poller
chmod 755 /etc/init.d/git-ultrasphinx
chmod 755 /etc/init.d/git-daemon
chmod 755 /etc/init.d/stomp
chmod 755 /etc/init.d/git-poller
update-rc.d stomp defaults
update-rc.d git-daemon defaults
update-rc.d git-ultrasphinx defaults
update-rc.d git-poller defaults
Config Apache2

ติดตั้ง passenger module สำหรับ apache
/var/lib/gems/1.8/bin/passenger-install-apache2-module
สร้างไฟล์ passenger.load สำหรับ module ของ apache
vim /etc/apache2/mods-available/passenger.load
โดยมี content (ส่วนนี้อาจเปลี่ยนไปตาม passenger version)
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-3.0.8/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-3.0.8
PassengerRuby /usr/bin/ruby1.8
Enable module ที่จำเป็น
/usr/sbin/a2enmod passenger
/usr/sbin/a2enmod rewrite
/usr/sbin/a2enmod ssl
Enable SSL site
/usr/sbin/a2ensite default-ssl
Restart Apache
/etc/init.d/apache2 restart
สร้าง site gitorious
vim /etc/apache2/sites-available/gitorious

ServerName gitorious.osdev.co.th
DocumentRoot /var/www/gitorious.bluequartz.net/gitorious/public

# Enable X-SendFile for gitorious repo archiving to work
XSendFile on
XSendFileAllowAbove on

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

CustomLog /var/log/apache2/gitorious_access.log combined
TransferLog /var/log/apache2/gitorious_access.log
ErrorLog /var/log/apache2/gitorious_error.log

สร้าง site gitorious-ssl
vim /etc/apache2/sites-available/gitorious-ssl


SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
BrowserMatch ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
DocumentRoot /var/www/gitorious.osdev.co.th/gitorious/public

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

CustomLog /var/log/apache2/gitorious_ssl_access.log combined
TransferLog /var/log/apache2/gitorious_ssl_access.log
ErrorLog /var/log/apache2/gitorious_ssl_error.log
Disable default SSL site และ enable the Gitorious sites
/usr/sbin/a2dissite default
/usr/sbin/a2dissite default-ssl
/usr/sbin/a2ensite gitorious
/usr/sbin/a2ensite gitorious-ssl
สร้าง repository directory
su git
cd ~
mkdir .ssh
touch .ssh/authorized_keys
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
mkdir repositories
mkdir tarballs
mkdir tarballs-work
chmod ug+rwx repositories
echo "export PATH=$PATH:/var/www/gitorious.osdev.co.th/gitorious/script" > ~/.bashrc
Configure Gitorious
cd /var/www/gitorious.osdev.co.th/gitorious
mkdir tmp/pids
cp config/database.sample.yml config/database.yml
cp config/gitorious.sample.yml config/gitorious.yml
cp config/broker.yml.example config/broker.yml
แก้ไขไฟล์ config/gitorious.yml
vim /var/www/gitorious.osdev.co.th/gitorious/config/gitorious.yml
#  Remove every section but production
# Be sure if you paste a generated secret key using apg -m 64 that you clean up
# any line breaks. The secret key that is pasted should be on a SINGLE line.
production:

# ให้รันคำสั่ง apg -m 64 แล้วนำผลลัพธ์ที่ได้มา paste ที่ cookie_secret
cookie_secret: urbecpajicEvpigdiOlqueDracpevyerbEmDecetsendeehurEjShucJox8Ojit4WaWrinyienderdalyaifugNeahatdashyunnamFucwerdolfyedyeactEjEajIb5IksEytkurewsEetAiwirtEkfedtheebyatNoganyoofBabikOcyemEshvovHasojTropsyengyegidzyggEdamCasdijHegdeasfudCovmenpobdywajcycsuchDabr2NadUlHighEisyoilmojliawgOdAcyimpifCaiWumtangEvzueshjajOtFuciddEbWhoivedesjetockVemOdEvnooquoncoapCaQuavnelkoijfudcatfezUlreylbUd

# The path where git repositories are stored. The actual (bare) repositories resides
# in repository_base_path/#{project.slug}/#{repository.name}.git/:
repository_base_path: "/home/git/repositories"

# Stuff that's in the html . custom stats javascript code etc
extra_html_head_data:

# System message that will appear on all pages if present
system_message:

# Port the ./script/gitorious script should use which should be
# the same port that the web server is running on. In Some development
# environments you may see 3000 for the value. For production
# servers you should use the same port as your web server.
gitorious_client_port: 80

# Host the ./script/gitorious script should use:
gitorious_client_host: gitorious.bluequartz.net

# Host which is serving the gitorious app, eg "gitorious.org"
gitorious_host: gitorious.bluequartz.net

# System User which is running git daemon
gitorious_user: git

# Email spam on server errors to:
# You probably want this during development or when you are trying to debug
# an installation that is not working correctly.
exception_notification_emails: mike.jackson@bluequartz.net

# Mangle visible e-mail addresses (spam protection)
mangle_email_addresses: true

# Enable or Disable Public Mode (true) or Private Mode (false)
public_mode: true

# Define your locale
locale: en

# Where should we store generated tarballs?
# (should be readable by webserver, since we tell it to send the file using X-Sendfile)
archive_cache_dir: "/Users/git/tarballs"

# Which directory should we work in when we generate tarballs, before moving
# them to the above dir?
archive_work_dir: "/Users/git/tarballs-work"

# is it only site admins who can create new projects?
only_site_admins_can_create_projects: false

# Should we hide HTTP clone urls?
hide_http_clone_urls: false

# Is this gitorious.org? Read: should we have a very flashy homepage?
is_gitorious_dot_org: false

# Pick a default license
#default_license: GNU Affero General Public License (AGPLv3)
แก้ไขไฟล์ config/database.yml
vim /var/www/gitorious.osdev.co.th/gitorious/config/database.yml
# Remove every section but production
# Make sure the 'production' section is the following
production:
adapter: mysql
database: gitorious
username: git
password: [secret]
host: localhost
encoding: utf8
reconnect = true
สร้าง SQL User
su git
mysql -uroot -p
mysql> CREATE USER 'git'@'localhost' IDENTIFIED BY '******';
mysql> GRANT ALL PRIVILEGES ON * . * TO  'git'@'localhost' IDENTIFIED BY  '******' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
mysql> GRANT ALL PRIVILEGES ON `gitorious` .* TO 'git'@'localhost';
mysql> quit
Create the Gitorious Databases
ให้ใช้ user root ไปที่ cd /var/www/gitorious.osdev.co.th/gitorious แล้วรันคำสั่ง
bundle install
จากนั้นให้ใช้ user git รันคำสั่ง
su git
cd /var/www/
chmod go-w gitorious.osdev.co.th
cd gitorious.osdev.co.th/gitorious
vim Rakefile
ให้เพิ่ม require 'thread' เข้าไปด้านบนสุดของส่วน require
bundle exec rake db:create RAILS_ENV=production
bundle exec rake db:migrate RAILS_ENV=production
สร้าง Gitorious Admin User
cd /var/www/gitorious.osdev.co.th/gitorious
vim vendor/rails/activesupport/lib/active_support.rb
ให้เพิ่มส่วน require 'thread' เข้าไปด้านบนสุดของส่วน require
env RAILS_ENV=production /usr/bin/ruby1.8 script/create_admin
ให้ใส่ admin email และ password ที่ต้องการ

Configure Sphinx Search Daemon
vim /var/www/gitorious.osdev.co.th/gitorious/config/ultrasphinx/default.base
แก้ไขในส่วน
# Daemon options
searchd
{
# What interface the search daemon should listen on and where to store its logs
address = 0.0.0.0
port = 3312
.......
ให้เป็น
# Daemon options
searchd
{
# What interface the search daemon should listen on and where to store its logs
address = localhost
port = 3312
.......
load configurations เข้าไปใน ruby files
cd /var/www/gitorious.osdev.co.th/gitorious
bundle exec rake ultrasphinx:bootstrap RAILS_ENV=production
bundle exec rake ultrasphinx:spelling:build RAILS_ENV=production
Set permissions อีกครั้งเพื่อความชัวร์
cd /var/www/gitorious.osdev.co.th/gitorious
chown -R git:git config/environment.rb script/poller log tmp
chmod -R g+w config/environment.rb script/poller log tmp
chmod ug+x script/poller
Setup Cron Job
su git
crontab -e
* */1 * * * cd /var/www/gitorious.bluequartz.net/gitorious && rake ultrasphinx:index RAILS_ENV=production
reference http://www.bluequartz.net/

Blogroll

About