ROR 3.2/ruby 1.9.3 Linux+Passenger/Nginx+Mysql+RVM

by minalecs
363 deployments · 101 still active · last rev. 2 years ago

# stackscript: RoR with Linux+Nnginx+Mysql+Passenger+RVM+Chef Solo
# Installs Multiuser RVM + Ruby 1.9.3 + Nginx + Passenger + MySQL + Git + Bundler + Deploy User

# updated for ubuntu 12.04 lts 09/21/12
#Warning: having issues with download of init script may have to download manually and copy #into /etc/init.d/
#http://pastebin.com/bh7xJ328

# Things to remember after install or to automate later:
# - adjust server timezone if required
# - put SSL certificate files at /usr/local/share/ca-certificates/
# - set up nginx to point to deployment app and eventual static site
# - (installs logrotate) create logrotate file to the deployed app logs
# - (generates keys)generate github ssh deployment keys
# - setup reverse DNS on Linode control panel
# - run cap production deploy:setup to configure initial files
# - install imagemagick

Compatible with: No distros currently supported
						#!/bin/bash
# <UDF name="db_password" Label="MySQL root Password" />
# <UDF name="r_env" Label="Rails/Rack environment to run" default="production" />
# <UDF name="ruby_release" Label="Ruby 1.9.3 Release" default="p194" example="p194" />
# <UDF name="deploy_user" Label="Name of deployment user" default="deploy" />
# <UDF name="deploy_password" Label="Password for deployment user" />
# <UDF name="deploy_sshkey" Label="Deployment user public ssh key" />
# <UDF name="new_hostname" Label="Server's hostname" default="appserver" />

exec &> /root/stackscript.log

source <ssinclude StackScriptID=1>  # Common bash functions
source <ssinclude StackScriptID=123>  # Awesome ubuntu utils script

function log {
  echo "### $1 -- `date '+%D %T'`"
}

function system_install_logrotate {
  apt-get -y install logrotate
}

function set_default_environment {
  cat >> /etc/environment << EOF
  RAILS_ENV=$R_ENV
  RACK_ENV=$R_ENV
EOF
}

function create_deployment_user {
  system_add_user $DEPLOY_USER $DEPLOY_PASSWORD "users,sudo"
  system_user_add_ssh_key $DEPLOY_USER "$DEPLOY_SSHKEY"
  system_update_locale_en_US_UTF_8
}

function install_essentials {
  aptitude -y install build-essential libpcre3-dev libssl-dev libcurl4-openssl-dev libreadline5-dev libxml2-dev libxslt1-dev libmysqlclient-dev openssh-server git-core
  good_stuff
}

function set_nginx_boot_up {
  wget http://pastebin.com/download.php?i=bh7xJ328 -O nginx
  chmod 744 /etc/init.d/nginx
  /usr/sbin/update-rc.d -f nginx defaults
  cat > /etc/logrotate.d/nginx << EOF
/usr/local/nginx/logs/* {
  daily
  missingok
  rotate 52
  compress
  delaycompress
  notifempty
  create 640 nobody root
  sharedscripts
  postrotate
  [ ! -f /user/local/nginx/logs/nginx.pid ] || kill -USR1 `cat /user/local/logs/nginx.pid`
  endscript
}
EOF
}

function set_production_gemrc {
  cat > ~/.gemrc << EOF
verbose: true
bulk_treshold: 1000
install: --no-ri --no-rdoc
benchmark: false
backtrace: false
update: --no-ri --no-rdoc
update_sources: true
EOF
  cp ~/.gemrc $USER_HOME
  chown $USER_NAME:$USER_NAME $USER_HOME/.gemrc
}


log "Updating System..."
system_update

log "Installing essentials...includes goodstuff"
install_essentials

log "Setting hostname to $NEW_HOSTNAME"
system_update_hostname $NEW_HOSTNAME

log "Creating deployment user $DEPLOY_USER"
create_deployment_user

cat >> /etc/sudoers <<EOF
Defaults !secure_path
$DEPLOY_USER ALL=(ALL) NOPASSWD: ALL
EOF


log "Setting basic security settings"
system_security_fail2ban
system_security_ufw_install
system_security_ufw_configure_basic
system_sshd_permitrootlogin No
system_sshd_passwordauthentication No
system_sshd_pubkeyauthentication Yes
/etc/init.d/ssh restart

log "installing log_rotate"
system_install_logrotate

log "Installing and tunning MySQL"
mysql_install "$DB_PASSWORD" && mysql_tune 40

log "Installing RVM and Ruby dependencies" >> $logfile
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 "Installing RVM system-wide"
curl -L get.rvm.io | sudo bash -s stable
usermod -a -G rvm "$DEPLOY_USER"

source /etc/profile.d/rvm.sh
source /etc/profile

log "Installing Ruby 1.9.3-$RUBY_RELEASE"
rvm install 1.9.3-$RUBY_RELEASE
rvm use 1.9.3-$RUBY_RELEASE --default

log "Updating Ruby gems"
set_production_gemrc
gem update --system


log "Instaling Phusion Passenger and Nginx"
gem install passenger
rvmsudo passenger-install-nginx-module --auto --auto-download --prefix="/usr/local/nginx"

log "Setting up Nginx to start on boot and rotate logs"
set_nginx_boot_up

log "Setting Rails/Rack defaults"
set_default_environment

log "Install Bundler"
gem install bundler

log "Restarting Services"
restartServices