Install redmine with rvm + unicorn + nginx in ubuntu 11.10


## Install packages

* git
* curl

```
sudo aptitude install git curl build-essential libssl-dev libmysqlclient-dev
```

## Create user

Create ```redmine``` user:

```
sudo adduser \
  --system \
  --shell /bin/sh \
  --gecos 'redmine' \
  --group \
  --disabled-password \
  --home /home/redmine \
  redmine
```

## Create database

```
mysql -uroot -p
```

```
mysql> create database redmine;
mysql> grant all on redmine.* to redmine@localhost identified by "poli1kew";
mysql> quit
```

## Login with redmine

Be ```redmine``` user:

```
sudo su - redmine -s /bin/bash
```

Install ```rvm```:

```
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
```

Enable ```.bashrc``` config:

```
bash .bashrc
```

Now you'll need to ask RVM if it needs any more programs and if so you'll need to install them to do this type:


```
rvm requirements
```

output example:

```
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
  ruby: /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion
```

Exit redmine and install requirements:

```
exit
sudo aptitude install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion
```

Be redemine again:

```
sudo su - redmine -s /bin/bash
```

Confirm available ruby versions:

```
rvm list known
```

Install ruby 1.8.7:

```
rvm install 1.8.7
```

Switch to ruby 1.8.7:

```
rvm 1.8.7
```

Create gemset 'redmine':

```
rvm gemset create redmine
```

Install gems:

```
gem install mysql   --no-ri --no-rdoc
gem install rack    --no-ri --no-rdoc -v=1.1.0
gem install unicorn --no-ri --no-rdoc
gem install i18n    --no-ri --no-rdoc -v=0.4.2
```

## Install redmine 

Fetch redmine package via wget:

```
cd
wget http://rubyforge.org/frs/download.php/75910/redmine-1.3.2.tar.gz
tar xzvf redmine-1.3.2.tar.gz
```

Create symbolic link:

```
ln -s redmine-1.3.2 redmine
cd redmine
```

Setup database config:

```
cp config/database.yml.example config/database.yml
```

```yaml:database.yml
production:
  adapter: mysql
  database: redmine
  host: localhost
  username: redmine
  password: poli1kew
  encoding: utf8
```

セッションストア秘密鍵を生成してください:

```
rake config/initializers/session_store.rb
```

データベース上にテーブルを作成してください。Redmineのインストールディレクトリで下記コマンドを実行します:

```
RAILS_ENV=production rake db:migrate
```

下記コマンドを実行し、デフォルトデータをデータベースに登録してください:

```
RAILS_ENV=production rake redmine:load_default_data
```

Logout redmine:

```
exit
```

## unicorn で redmine を起動するため頑張る

unicorn のコンフィグを書く

* ワーカプロセスは２つ
* fork 後に実行ユーザを redmine へ変更する

```
sudo vim /home/redmine/redmine/config/unicorn.conf
```

```unicorn.conf
$unicorn_user = "redmine"
$unicorn_group = "redmine"

worker_processes 2
working_directory '/home/redmine/redmine'

listen      "127.0.0.1:50008", :tcp_nopush => true
timeout     30

pid         "tmp/pids/unicorn.pid"
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"

preload_app  true

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection

  begin
    uid, gid = Process.euid, Process.egid
    user, group = $unicorn_user, $unicorn_group
    target_uid = Etc.getpwnam(user).uid
    target_gid = Etc.getgrnam(group).gid
    worker.tmp.chown(target_uid, target_gid)
    if uid != target_uid or gid != target_gid
      Process.initgroups(user, target_gid)
      Process::GID.change_privilege(target_gid)
      Process::UID.change_privilege(target_uid)
    end
  rescue
    if RAILS_ENV = "development"
      STDERR.puts "could not change user, oh well"
    else
      raise e
    end
  end
end
```

起動スクリプトを書く:

```
sudo vim /etc/init.d/redmine
```

```sh:redmine
#!/bin/bash
#
# unicorn_rails_redmine Startup script for unicorn.
#
# chkconfig: - 85 15
# description: redmine on unicorn start/stop script.
#

#
# set rvm environment valiables.
#
export PATH=/home/redmine/.rvm/gems/ruby-1.8.7-p358@redmine/bin:/home/redmine/.rvm/gems/ruby-1.8.7-p358@global/bin:/home/redmine/.rvm/gems/ruby-1.8.7-p358/bin:/home/redmine/.rvm/rubies/ruby-1.8.7-p358/bin:/home/redmine/.rvm/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/redmine/bin
export GEM_HOME=/home/redmine/.rvm/gems/ruby-1.8.7-p358@redmine
export GEM_PATH=/home/redmine/.rvm/gems/ruby-1.8.7-p358@redmine:/home/redmine/.rvm/gems/ruby-1.8.7-p358@global:/home/redmine/.rvm/gems/ruby-1.8.7-p358
export BUNDLE_PATH=
export MY_RUBY_HOME=/home/redmine/.rvm/rubies/ruby-1.8.7-p358
export IRBRC=/home/redmine/.rvm/rubies/ruby-1.8.7-p358/.irbrc

set -u
set -e

APP_NAME=redmine
APP_ROOT="/home/redmine/$APP_NAME"
CNF="$APP_ROOT/config/unicorn.conf"
PID="$APP_ROOT/tmp/pids/unicorn.pid"
ENV=production

UNICORN_OPTS="-D -E $ENV -c $CNF"

old_pid="$PID.oldbin"

cd $APP_ROOT || exit 1

sig () {
        test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
        test -s $old_pid && kill -$1 `cat $old_pid`
}

case ${1-help} in
start)
        sig 0 && echo >&2 "Already running" && exit 0
        cd $APP_ROOT ; unicorn_rails $UNICORN_OPTS
        ;;
stop)
        sig QUIT && exit 0
        echo >&2 "Not running"
        ;;
force-stop)
        sig TERM && exit 0
        echo >&2 "Not running"
        ;;
restart|reload)
        sig HUP && echo reloaded OK && exit 0
        echo >&2 "Couldn't reload, starting instead"
        unicorn_rails $UNICORN_OPTS
        ;;
upgrade)
        sig USR2 && exit 0
        echo >&2 "Couldn't upgrade, starting instead"
        unicorn_rails $UNICORN_OPTS
        ;;
rotate)
        sig USR1 && echo rotated logs OK && exit 0
        echo >&2 "Couldn't rotate logs" && exit 1
        ;;
*)
        echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
        exit 1
        ;;
esac
```

Enable boot script:

```
sudo chmod +x /etc/init.d/redmine
sudo rcconf 
```

Start redmine:

```
sudo service redmine start
```

## Integrate unicorn with nginx

Stop apache if running:

```
sudo service apache2 stop
```

Install nginx:

```
sudo aptitude install nginx
```

Create config file for nginx:

```
sudo vim /etc/nginx/sites-available/redmine.suin.org
```

```:/etc/nginx/sites-available/redmine.suin.org
upstream redmine {
    server 127.0.0.1:50008;
}

server {
    listen 80;
    server_name redmine.suin.org;

    location / {

        root /home/redmine/redmine/public;

        if (!-f $request_filename) {
            proxy_pass http://redmine; 
            break;
        }
    }

}
```

```
sudo ln -s /etc/nginx/sites-available/redmine.suin.org /etc/nginx/sites-enabled/redmine.suin.org
```

Start nginx:

```
sudo service nginx start
```