Ruby/Rails rbenv nginx mysql

by minalecs
9 deployments · 1 still active · last rev. 4 years ago

Ruby/Rails rbenv nginx mysql

Compatible with: No distros currently supported
						#!/bin/bash
# stackscript: RoR with Linux+Nnginx+Mysql+Passenger+RVM+Chef Solo
# Installs System RVM + Ruby 1.9.3 + Nginx + Passenger + MySQL + Git + Bundler + Deploy User
# Alot of this was copied from StackScript 2253.
# For another similar script I looked at script 1635.
# I found this script to be the most complete when creating a new instance.

# 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

#!/bin/bash
# <UDF name="r_env" Label="Rails/Rack environment to run" default="production" />
# <UDF name="ruby_ver" Label="Ruby 1.9.3 Release" default="1.9.3-p392" example="1.9.3-p392" />
# <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" />

# <UDF name="db_password" Label="MySQL root Password" />


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 {

  #/usr/sbin/update-rc.d -f nginx defaults
  cat > /etc/logrotate.d/nginx << EOF
/var/log/nginx/* {
  daily
  missingok
  rotate 52
  compress
  delaycompress
  notifempty
  create 640 nobody root
  sharedscripts
  postrotate
  [ ! -f /var/log/nginx/nginx.pid ] || kill -USR1 `cat /var/log/nginx/nginx.pid`
  endscript
}
EOF
}

function as_user {
  su -l -c "$1" $deploy_user
}



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

# Installing mysql and nginx
log "Installing mysql and nginx"

apt-get install -y python-software-properties

case "`lsb_release -si`" in
  "Ubuntu")
    add-apt-repository -y ppa:nginx/stable
  ;;
  "Debian")
    # PostgreSQL
    echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -sc`-pgdg main" > /etc/apt/sources.list.d/pgdg.list
    wget -q -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
    # nginx
    wget -q -O - http://nginx.org/keys/nginx_signing.key | apt-key add -
    cat <<EOT > /etc/apt/sources.list.d/nginx.list
deb http://nginx.org/packages/debian/ `lsb_release -sc` nginx
deb-src http://nginx.org/packages/debian/ `lsb_release -sc` nginx
EOT
  ;;
esac

aptitude update
aptitude -y install nginx
log "Installing and tunning MySQL"
mysql_install "$DB_PASSWORD" && mysql_tune 40

log "$(rm -v /etc/nginx/sites-enabled/*)"
log "$(service nginx start)"


aptitude -y install libxml2-dev proj libjson0-dev xsltproc docbook-xsl docbook-mathml

# Installing rbenv
log "setting up rbenv"
su -l -c "git clone git://github.com/sstephenson/rbenv.git ~/.rbenv" $DEPLOY_USER
su -l -c "echo 'export PATH=\"\$HOME/.rbenv/bin:\$PATH\"' >> ~/.profile" $DEPLOY_USER
su -l -c "echo 'eval \"\$(rbenv init -)\"' >> ~/.profile" $DEPLOY_USER
su -l -c "git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build" $DEPLOY_USER

log "Installing Ruby ${RUBY_VER}"
su -l -c "rbenv install ${RUBY_VER} && rbenv rehash && rbenv global ${RUBY_VER}" $DEPLOY_USER
su -l -c "echo 'gem: --no-document' > ~/.gemrc && gem i bundler" $DEPLOY_USER

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"
su -l -c "git clone git://github.com/carsomyr/rbenv-bundler.git ~/.rbenv/plugins/bundler" $DEPLOY_USER
su -l -c "gem install bundler" $DEPLOY_USER
su -l -c "rbenv rehash" $DEPLOY_USER

log "installing node js"
aptitude -y install nodejs

log "Restarting Services"
reboot