Rails Enviroments with RVM, Nginx and Passenger

by gogojimmy
35 deployments · 6 still active · last rev. 4 years ago

Compatible with: No distros currently supported
						#!/bin/bash
#Install Ruby 1.9.3-p125 with RVM and Nginx with Passenger

# <UDF name="db_password" Label="MySQL root Password" />
# <UDF name="ruby_version" Label="RVM Ruby version to install" default="1.9.3-p125" />
# <UDF name="username" Label="User account, type n if you want to add user yourself" default="deployer" />
# <UDF name="password" Label="User password, type n if you want to add user yourself" />
# <UDF name="ssh_key" Label="Your local machine SSH KEY to make you ssh to this machine without type password, type n if you don't want to add" />
# <UDF name="oh_my_zsh" Label="type n if you don't want to install oh-my-zsh" default="Y" />
source <ssinclude StackScriptID=1>  # Common bash functions

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

function log_line {
  echo "----------------------------------------------------------------------------------------------" >> /root/log.txt
}

function add_ssh {
  log_line
  log "starting add ssh-key"
  log_line

  grep -q [Nn] $SSH_KEY
  if [ $? == 0 ]; then
	log "Error: Skip add ssh-key because no given key"
	return 1;
  fi

  grep -q [Nn] $USERNAME
  if [ $? == 0 ]; then
	log "Error: Skip add ssh-key because no given user"
	return 1;
  fi
  mkdir /home/"$USERNAME"/.ssh
  echo "$SSH_KEY" >> /home/"$USERNAME"/.ssh/authorized_keys
  log "ssh-key added"
  log_line
}

function install_oh_my_zsh {
  log "starting install on-my-zsh"
  log_line
  grep -q [Nn] $OH_MY_ZSH
  if [ $? == 0 ]; then
	log "Error: Skip Install oh_my_zsh"
	return 1;
  fi
  log "Downloading: (https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh)"
  wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh
  echo "[[ -s "/user/local/rvm/scripts/rvm" ]] && . "/user/local/rvm/scripts/rvm"" >> /home/"$USERNAME"/.zshrc
  chsh -s `which zsh`
  log "oh-my-zsh installed"
  log_line
}

function add_user {
  grep -q [Nn] $USERNAME
  if [ $? == 0 ]; then
	log "Error: Skip add user"
	return 1;
  fi

  log "starting add user"
  log_line
  useradd -d /home/"$USERNAME" -m -s /bin/bash "$USERNAME"
  echo "$USERNAME:$PASSWORD" | chpasswd
  echo "$USERNAME    ALL=(ALL) ALL" >> /etc/sudoers
  log "User created"
  log_line
}

function install_essientials {
  log_line
  log "starting install essientials"
  log_line
  aptitude -y install git-core libmysqlclient15-dev curl build-essential libcurl4-openssl-dev zlib1g-dev libssl-dev libreadline6 libreadline6-dev libperl-dev gcc libjpeg62-dev libbz2-dev libtiff4-dev libwmf-dev libx11-dev libxt-dev libxext-dev libxml2-dev libfreetype6-dev liblcms1-dev
libexif-dev perl libjasper-dev libltdl3-dev graphviz gs-gpl pkg-config
log "Essentials installed"
  log_line
}

function install_rvm_ruby {
  log "starting install RVM"
  log_line
  curl -L get.rvm.io | sudo bash -s stable
  echo 'source "/usr/local/rvm/scripts/rvm"' >> /etc/profile
  source /etc/profile
  log "RVM installed"

  grep -q [Nn] $USERNAME
  if [ $? == 0 ]; then
	log "Skip add user to rvm group"
  else
	  usermod -a -G rvm "$USERNAME"
  fi
  log_line
  log "starting install Ruby $RUBY_VERSION"
  log `rvm install $RUBY_VERSION`
  rvm --default $RUBY_VERSION
  log "Ruby version $RUBY_VERSION installed"
  log_line
}

function install_rails_bundler_rake {
  log "starting install rails"
  log `gem install rails --no-ri --no-rdoc`
  log "rails gem installed"
  log "starting install rake"
  log `gem install rake --no-ri --no-rdoc`
  log "rake gem installed"
  log "starting install bundler"
  log `gem install bundler --no-ri --no-rdoc`
  log "bundler gem installed"
  log_line
}

function install_passenger_nginx {
  log "starting install passenger gem"
  log `gem install passenger --no-ri --no-rdoc`
  log "passenger gem installed"
  log "starting install nginx"
  log `rvmsudo passenger-install-nginx-module --auto --auto-download --prefix="/usr/local/nginx"`
  log "nginx installed"
  log_line
}

function nginx_configure {
  log "starting configure nginx"
  wget http://library.linode.com/web-servers/nginx/installation/reference/init-deb.sh
  cat init-deb.sh | sed 's:/opt/:/usr/local/:' > /etc/init.d/nginx
  chmod +x /etc/init.d/nginx
  /usr/sbin/update-rc.d -f nginx defaults
  log "nginx configured to start automatically"
  log_line
}

function install_imagemagick_rmagick {
  log "starting install imagemagick"
  log "Downloading: from:(ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz)"
  wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
  log "tar output:"
  log `tar zxvf ImageMagick.tar.gz`
  cd ImageMagick-*

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

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

  log "make install output: (from calling make output)"
  log `make install`
  log "imagemagick installed"
  log "starting install libmagickwand-dev package"
  aptitude -y install libmagickwand-dev
  log "libmagickwand-dev package installed"
  log "starting install rmagick"
  log `gem install rmagick --no-ri --no-rdoc`
  log "rmagick gem installed"
  log_line
}

####################################################################################################
# Deploy procedure                                                                                 #
####################################################################################################

# Update System and install essentials
log_line
log "starting deploy"
log "starting update system"
system_update
log "system updated"
install_essientials

# Set up Mysql
mysql_install "$DB_PASSWORD" && mysql_tune 40
log_line
log "MySQL installed"
log_line

# Add deploy user
add_user

# Add SSH Key to User you created
# add_ssh

# Install RVM
install_rvm_ruby

# Install oh-my-zsh
#install_oh_my_zsh

# Install gems and passenger
install_rails_bundler_rake
# install_passenger_nginx

# Configure nginx to start automatically
# nginx_configure

# Install ImageMagick and rmagick gem
install_imagemagick_rmagick

# Spit & polish
restartServices
log "StackScript Finished!"