User Tools

Site Tools


ruby:rails:rbenvandthin

rbenv, bundler, thin and nginx

Install rbenv

# Install rbenv
git clone git://github.com/sstephenson/rbenv.git /opt/rbenv
 
# Add rbenv to the path:
echo '# rbenv setup' > /etc/profile.d/rbenv.sh
echo 'export RBENV_ROOT=/opt/rbenv' >> /etc/profile.d/rbenv.sh
echo 'export PATH="$RBENV_ROOT/bin:$PATH"' >> /etc/profile.d/rbenv.sh
echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh
 
chmod +x /etc/profile.d/rbenv.sh
source /etc/profile.d/rbenv.sh
 
# Install ruby-build:
pushd /tmp
  git clone git://github.com/sstephenson/ruby-build.git
  cd ruby-build
  ./install.sh
popd
 
# Install Ruby 1.9.2-p290:
rbenv install 1.9.2-p290
rbenv global 1.9.2-p290
 
# Rehash:
rbenv rehash

Prepare your rails app

bundle install --binstubs --deployment

Thin init script

Init script for thin which manages thin within a shared rbenv environment:

#!/bin/sh
 
# thin  This shell script takes care of starting and stopping
#       thin daemon from rbenv shared install
#
# chkconfig:    2345 85 15
# description:  thin is an Ruby web server
# config:       /etc/sysconfig/thin
 
### BEGIN INIT INFO
# Provides:          thin
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# X-Start-Before:    nginx
# X-Stop-After:      nginx
# Short-Description: thin initscript
# Description:       This shell script takes care of starting and
#                    stopping thin daemon from rbenv shared install
### END INIT INFO
 
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
RBENV_DIR="/opt/rbenv"
THIN_BIN="$RBENV_DIR/shims/thin"
BUNDLE_BIN="$RBENV_DIR/shims/bundle"
CONFIG_DIR="/etc/thin"
CONFIG_FILE_EXT=".yml"
 
[ -f /etc/sysconfig/thin ] && . /etc/sysconfig/thin
 
if [ ! -x "$THIN_BIN" ]; then
    echo "error: thin executable does not exist or not executable: $THIN_BIN" && exit 1
fi
 
if [ -d "$RBENV_DIR" ]; then
    export RBENV_ROOT="$RBENV_DIR"
    export PATH="$RBENV_ROOT/bin:$PATH"
    eval "$(rbenv init -)"
else
    echo "error: rbenv directory does not exist: $RBENV_DIR" && exit 1
fi
 
thin_ctl() {
    local command=$1; shift
 
    [ ! -d $CONFIG_DIR ] && echo "error: directory does not exist: $CONFIG_DIR" && exit 1
 
    if [ $# -eq 0 ]; then
        CONFIG_FILES=`cd ${CONFIG_DIR} && ls *${CONFIG_FILE_EXT}`
    else
        for app_name in "$@"; do
            CONFIG_FILES="$CONFIG_FILES ${app_name}${CONFIG_FILE_EXT}"
        done
    fi
 
    for config_file in ${CONFIG_FILES[@]}; do
        echo "thin $command ${config_file//$CONFIG_FILE_EXT}"
        [ ! -e "${CONFIG_DIR}/${config_file}" ] && echo "error: file does not exist: ${CONFIG_DIR}/${config_file}" && exit 1
 
        case $command in
        start|stop|restart)
            chdir=`egrep "^chdir:" "${CONFIG_DIR}/${config_file}" | awk '{ print $2 }'`
            [ ! -d "$chdir" ] && echo "error: chdir directory does not exist: $chdir" && exit 1
            cd "$chdir"
            "$BUNDLE_BIN" exec "$THIN_BIN" -C "${CONFIG_DIR}/${config_file}" $command
            ;;
        *)
            echo "Unknown command ${command}"
            ;;
        esac
    done
 
    return 0
}
 
enable_application() {
    old="$1$CONFIG_FILE_EXT.disabled"
    new="$1$CONFIG_FILE_EXT"
    [ ! -e "$CONFIG_DIR/$old" ] && echo "error: file does not exist: $CONFIG_DIR/$old" && exit 1
    mv "$CONFIG_DIR/$old" "$CONFIG_DIR/$new"
}
 
disable_application() {
    old="$1$CONFIG_FILE_EXT"
    new="$1$CONFIG_FILE_EXT.disabled"
    [ ! -e "$CONFIG_DIR/$old" ] && echo "error: file does not exist: $CONFIG_DIR/$old" && exit 1
    mv "$CONFIG_DIR/$old" "$CONFIG_DIR/$new"
}
 
config() {
  echo "Creating config directory: $CONFIG_DIR"
  [ ! -d "$CONFIG_DIR" ] && mkdir "$CONFIG_DIR"
 
  echo "Creating config example: $CONFIG_DIR/example${CONFIG_FILE_EXT}.disabled"
 
  "$THIN_BIN" config -C "${CONFIG_DIR}/example${CONFIG_FILE_EXT}.disabled" \
    -u nobody -g nobody --tag example \
    -s 4 --max-conns 1024 --max-persistent-conns 512 \
    -S /tmp/example.sock -c /var/www/example \
    -l /var/log/thin/example.log -P /var/run/example.pid \
    -A autodetect -e production
}
 
if [ $# -gt 2 ]; then
    exit 2
fi
 
case "$1" in
    start|stop|restart)
        thin_ctl "$@"
        ;;
    enable|disable)
        [ -z "$2" ] && echo "error: missing app_name" && exit 1
        $1_application "$2"
        ;;
    config)
        config
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|enable|disable|config} [app_name]"
        exit 2
        ;;
esac
 
exit 0

Example nginx rails config

Example file under /etc/nginx/conf.d/example.com.conf

upstream thin {
  server unix:/var/www/example/current/tmp/thin.0.socket fail_timeout=0;
}
 
server {
  server_name example.com;
  root /srv/www//example/current/public;
 
  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }
 
  try_files $uri/index.html $uri @thin;
  location @thin {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://thin;
  }
 
  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}
/srv/wiki.niwos.com/data/pages/ruby/rails/rbenvandthin.txt · Last modified: 2012/11/09 17:52 by niwo