Ruby 1.9.2, nginx, mysql, bundler.

by reenhanced
5 deployments · 1 still active · last rev. 5 years ago

ruby 1.9.2

Compatible with: No distros currently supported
						#!/bin/bash
# 
# Installs Ruby 1.9, and Nginx with Passenger. 
#
# <UDF name="db_password" Label="MySQL root Password" />
# <UDF name="db_name" Label="Create Database" default="" example="Optionally create this database" />
# <UDF name="db_user" Label="Create MySQL User" default="" example="Optionally create this user" />
# <UDF name="db_user_password" Label="MySQL User's Password" default="" example="User's password" />
# <UDF name="rr_env" Label="Rails/Rack environment to run (list here: ftp://ftp.ruby-lang.org/pub/ruby/)" default="production" />
# <UDF name="hostname" Label="Hostname" />

source <ssinclude StackScriptID=1>  # Common bash functions

function log {
  echo "$1 `date '+%D %T'`" >> /root/log.txt
}

# Set hostname
echo $HOSTNAME > /etc/hostname
hostname -F /etc/hostname
cat >> /etc/hosts << EOF
127.0.0.1    $HOSTNAME
EOF

# Set UTC as timezone
ln -sf /usr/share/zoneinfo/UTC /etc/localtime ## for Universal Coordinated Time

# Update packages and install essentials
  cd /tmp
  system_update
  log "System updated"
  apt-get -y install build-essential zlib1g-dev libssl-dev libreadline5-dev openssh-server libyaml-dev libcurl4-openssl-dev libxslt-dev libxml2-dev
  goodstuff
  log "Essentials installed"

# Set up MySQL
  mysql_install "$DB_PASSWORD" && mysql_tune 40
  log "MySQL installed"
  mysql_create_database "$DB_PASSWORD" "$DB_NAME"
  mysql_create_user "$DB_PASSWORD" "$DB_USER" "$DB_USER_PASSWORD"
  mysql_grant_user "$DB_PASSWORD" "$DB_USER" "$DB_NAME"

# Set up Postfix
  postfix_install_loopback_only

# Installing Ruby
  export RUBY_VERSION="ruby-1.9.2-p290"
  log "Installing Ruby $RUBY_VERSION"

  log "Downloading: (from calling wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/$RUBY_VERSION.tar.gz)" 
  log `wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/$RUBY_VERSION.tar.gz`

  log "tar output:"
  log `tar xzf $RUBY_VERSION.tar.gz`
  rm "$RUBY_VERSION.tar.gz"
  cd $RUBY_VERSION

  log "current directory: `pwd`"
  log ""
  log "Ruby Configuration output: (from calling ./configure)" 
  log `./configure` 

  log ""
  log "Ruby make output: (from calling make)"
  log `make`

  log ""
  log "Ruby make install output: (from calling make install)"
  log `make install` 
  cd ..
  rm -rf $RUBY_VERSION
  log "Ruby installed!"

# Install ruby gems
cd /tmp
log "Downloading Ruby Gems with wget http://production.cf.rubygems.org/rubygems/rubygems-1.8.10.tgz"
log `wget http://production.cf.rubygems.org/rubygems/rubygems-1.8.10.tgz`

log 'tar output:'
log `tar xzvf rubygems-1.8.10.tgz`
rm rubygems-1.8.10.tgz

cd rubygems-1.8.10
log "rubygems setup"
log `ruby setup.rb`

log "Updating ruby gems"
log `gem update --system`


# Set up Nginx and Passenger
  log "Installing Nginx and Passenger" 
  gem install passenger
  passenger-install-nginx-module --auto --auto-download
  log "Passenger and Nginx installed"

# Configure nginx to start automatically
  cd /opt
  wget -O init-deb.sh http://library.linode.com/assets/601-init-deb.sh
  mv /opt/init-deb.sh /etc/init.d/nginx
  chmod +x /etc/init.d/nginx
  /usr/sbin/update-rc.d -f nginx defaults
  log "Nginx configured to start automatically"

# Install git
  apt-get -y install git-core

# Set up environment
  # Global environment variables
  if [ ! -n "$RR_ENV" ]; then
    RR_ENV="production"
  fi
  cat >> /etc/environment << EOF
RAILS_ENV="$RR_ENV"
RACK_ENV="$RR_ENV"
EOF

# Update rubygems to (=> 1.3.6 as required by rails3)
gem update --system

# Install bundler
gem install bundler

# Add deploy user
echo "deploy:deploy:1000:1000::/home/deploy:/bin/bash" | newusers
cp -a /etc/skel/.[a-z]* /home/deploy/
chown -R deploy /home/deploy
# Add to sudoers(?)
echo "deploy    ALL=(ALL) ALL" >> /etc/sudoers

log "Configuration for nginx is at: /opt/nginx/conf/nginx.conf"
log "Now, run ssh-keygen -t as deploy"
log "Then, clone repo into ~deploy"
log "And edit /opt/nginx/conf/nginx.conf"
log "http://library.linode.com/web-servers/nginx/configuration/basic"
log "more useful: http://modrails.com/documentation/Users%20guide%20Nginx.html#deploying_a_ror_app"
log "When done, do /etc/init.d/nginx reload"
log "And change to your app dir and run `bundle install --deployment`"

# Spit & polish
  restartServices
  log "StackScript Finished!"