Skip to content

Instantly share code, notes, and snippets.

@atljeremy
Forked from crmaxx/0-readme.md
Last active December 21, 2015 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atljeremy/6240360 to your computer and use it in GitHub Desktop.
Save atljeremy/6240360 to your computer and use it in GitHub Desktop.

Amazon AWS EC2 production for Ruby on Rails

Prepare cloud

Speed up ssh login

echo 'UseDNS no # if slow connection' >> /etc/ssh/sshd_config
echo 'PrintMotd yes # if you need motd messages' >> /etc/ssh/sshd_config

From /etc/pam.d/login && /etc/pam.d/sshd delete all strings included

session optional pam_motd.so

Remove components of paid monitoring

sudo apt-get remove landscape-client landscape-common
sudo apt-get autoremove

Installation

Prepare ubuntu

Update fresh installed cloud

sudo apt-get update && sudo apt-get upgrade

Install utilities

sudo apt-get install mc htop curl git-core localepurge

Install devtools

sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev

Install postgresql

sudo add-apt-repository ppa:pitti/postgresql && sudo apt-get update
sudo apt-get install postgresql-9.2 postgresql-client-9.2 postgresql-contrib-9.2 postgresql-server-dev-9.2

Check pg port. It must be 5432

sudo netstat -lp | grep postgresql

Connect to pg via psql

sudo su postgres -c psql

Create a new user

create user username with password 'password';

Grant privelegies for it.

alter user username superuser;

Install alternative malloc. Ruby works on 8-10% faster.

sudo apt-get install libtcmalloc-minimal0
echo 'export LD_PRELOAD=/usr/lib/libtcmalloc_minimal.so.0.1.0' >> ~/.bash_profile
source ~/.bash_profile

Install ruby env

Install rvm & ruby & rails

bash -s < <(curl -s https://raw.github.com/gist/4598302/install-rvm.sh)

Install web-server

Install nginx

sudo apt-get install nginx

Create dir for projects

sudo mkdir -p /srv/ror
sudo chown -R ubuntu:ubuntu /srv/ror/
#!/bin/bash
echo 'Installing RVM'
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
echo '# Ruby tuning' >> ~/.bash_profile
echo 'export RUBY_HEAP_MIN_SLOTS=1000000' >> ~/.bash_profile
echo 'export RUBY_HEAP_SLOTS_INCREMENT=1000000' >> ~/.bash_profile
echo 'export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1' >> ~/.bash_profile
echo 'export RUBY_GC_MALLOC_LIMIT=1000000000' >> ~/.bash_profile
echo 'export RUBY_HEAP_FREE_MIN=500000' >> ~/.bash_profile
source ~/.bash_profile
echo 'Installing ruby-2.0.0-p247'
rvm get head
rvm install ruby-2.0.0-p247
rvm use ruby-2.0.0-p247 --default
rvm use ruby-2.0.0-p247@global
echo 'Installing rails'
gem install rails --no-rdoc --no-ri
echo 'Installing bundler'
gem install bundler --no-rdoc --no-ri
echo 'Creating ~/.gemrc'
echo "install: --no-rdoc --no-ri" >> ~/.gemrc
echo "update: --no-rdoc --no-ri" >> ~/.gemrc
#!/bin/bash
### BEGIN INIT INFO
# Provides: APPLICATION
# Required-Start: $all
# Required-Stop: $network $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the APPLICATION unicorns at boot
# Description: Enable APPLICATION at boot time.
### END INIT INFO
#
# Use this as a basis for your own Unicorn init script.
# Change APPLICATION to match your app.
# Make sure that all paths are correct.
set -u
set -e
# Change these to match your app:
APP_NAME="TrackerScorer"
APP_ROOT="/srv/ror/$APP_NAME/current"
PID="/srv/ror/$APP_NAME/shared/pids/unicorn.pid"
ENV=production
GEM_HOME="/usr/local/rvm/gems/ruby-2.0.0-p247"
UNICORN_OPTS="-D -E $ENV -c $APP_ROOT/config/unicorn.rb"
SET_PATH="cd $APP_ROOT; rvm use ruby-2.0.0-p247@global; export GEM_HOME=$GEM_HOME"
CMD="$SET_PATH; bundle exec unicorn $UNICORN_OPTS"
old_pid="$PID.oldbin"
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $old_pid && kill -$1 `cat $old_pid`
}
case ${1-help} in
start)
sig 0 && echo >&2 "Already running" && exit 0
su - unicorn -c "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
su - unicorn -c "$CMD"
;;
upgrade)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su - unicorn -c "$CMD"
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment