Skip to content

Instantly share code, notes, and snippets.

@lin
Last active April 11, 2016 08:16
Show Gist options
  • Save lin/0e6fbeb6766697d47fb9d4b8ea0565eb to your computer and use it in GitHub Desktop.
Save lin/0e6fbeb6766697d47fb9d4b8ea0565eb to your computer and use it in GitHub Desktop.
Install Ruby (2.3.0p0) + Rails (4.2.6) + Postgres (9.3.11) on Ubuntu (ubuntu-trusty-14.04-amd64) with AWS

Last update, 04-08-2016

sudo apt-get update

RVM

sudo apt-get install curl
# visit https://rvm.io/ for detais, currently it works by following commands.
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm requirements
rvm -v

Ruby

rvm install ruby
rvm use ruby --default
rvm rubygems current
ruby -v

Rails

gem install rails
rails -v
sudo apt-get install libsqlite3-dev
gem install sqlite3
sudo apt-get install nodejs npm
gem install railties

===========================

Additional Information

Test your rails app.

mkdir ~/test
cd ~/test
rails new app
cd app
rails g scaffold User name:string
rake db:migrate
bundle exec rails server -b 0.0.0.0

Visit yourwebsite.com:3000 to see result (as shown in the following image). Remeber you have to open the port 3000 for outbound in your Security Groups to see the result.

rails success

Also, you can check yourwebsite.com:3000/users to test the database.

control + c to terminate the WEBrick server.

git

sudo apt-get install git
git config --global user.name [your name]
git config --global user.email [your email]

Postgresql Database

Official Guilines

$ sudo apt-get install postgresql postgresql-contrib
$ sudo apt-get install libpq-dev
$ psql --version

===========================

Additional Information

you can start/stop/restart the postgres by

sudo /etc/init.d/postgresql start/stop/restart

Create user (role) postgres and assign a password to it.

sudo -u postgres psql postgres
\password postgres
\q # to quit

Since it's connected locally only, we can use peer authentication.

# create user (role) ubuntu
sudo -u postgres createuser --superuser $USER

# set password for user ubuntu
sudo -u postgres psql
\password ubuntu
\q

# create a database named ubuntu
sudo -u postgres createdb $USER

===========================

Additional Information

If you want to change it to md5 authentication, you need to change pg_hba.conf file.

sudo vi /etc/postgresql/9.3/main/pg_hba.conf # local all [username] md5
sudo /etc/init.d/postgresql restart

install

sudo apt-get install nginx

===========================

Additional Information

start / restart / stop server

$ sudo service nginx start/restart/stop

Local computer (i.e. Mac) setup for unicorn

gem 'unicorn' # add it to Gemfile
bundle install

make pids folder at [root]/pids make unicorn.pid file under [root]/pids/ directory

create a file as config/unicorn.rb, as in file 08 on this page.

create a file as config/unicorn_init.sh, as in file 09 on this page.

# config/unicorn.rb
# Set the working application directory
# working_directory '/path/to/your/app'
working_directory '/var/www/yourapp'
# Unicorn PID file location
# pid '/path/to/pids/unicorn.pid'
pid '/var/www/yourapp/pids/unicorn.pid'
# Path to logs
# stderr_path '/path/to/log/unicorn.log'
# stdout_path '/path/to/log/unicorn.log'
stderr_path '/var/www/yourapp/log/unicorn.log'
stdout_path '/var/www/yourapp/log/unicorn.log'
# Unicorn socket
# listen '/tmp/unicorn.[application name].sock'
listen '/tmp/unicorn.yourapp.sock'
# Number of processes
# worker_processes 4
worker_processes 2
# Time-out
timeout 30
#!/bin/sh
set -e
# Example init script, this can be used with nginx, too,
# since nginx and unicorn accept the same signals
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/var/www/yourapp
PID=/var/www/yourapp/pids/unicorn.pid
CMD="unicorn_rails -D -c $APP_ROOT/config/unicorn.rb -E production"
action="$1"
set -u
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 $action in
start)
sig 0 && echo >&2 "Already running" && exit 0
su -c "$CMD" - ubuntu
;;
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 -c "$CMD" - ubuntu
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $old_pid && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $old_pid
then
echo >&2 "$old_pid still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su -c "$CMD" - ubuntu
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 "
exit 1
;;
esac

Create your app folder.

sudo mkdir -p /var/www
sudo chown -R ubuntu:ubuntu /var/www
sudo chmod -R g+rw /var/www
cd /var/www

Install your app

git clone https://github.com/[username]/[app].git
cd [app]
bundle install
sudo vi /etc/nginx/sites-available/default

Edit as in file 12 on this page.

Then start the nginx to test result.

# make sure you are at /var/www/[app]
mkdir log
rake db:create
rake db:migrate
rake db:seed
unicorn_rails -c config/unicorn.rb -D
sudo service nginx restart
# /etc/nginx/sites-available/default
upstream app_server {
server unix:/tmp/unicorn.yourapp.sock fail_timeout=0;
}
server {
listen 80;
server_name yourapp.com;
root /var/www/yourapp/public;
try_files $uri/index.html $uri.html $uri @appname;
location @appname {
proxy_pass http://app_server; # match the name of upstream directive which is defined above
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^/assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
break;
}
}

Make unicorn script executable.

sudo chmod +x config/unicorn_init.sh

Make it a service.

sudo cp config/unicorn_init.sh /etc/init.d/unicorn

===========================

Additional Information

You can start / restart / stop server by

$ sudo service unicorn start/restart/stop

Add secret key for devise and production environment

rake secret
sudo vi ~/.bash_profile

Add this line

export SECRET_KEY_BASE='secret'
. ~/.bash_profile
echo $SECRET_KEY_BASE

Remember to migrate database again for production database.

RAILS_ENV=production rake db:create
RAILS_ENV=production rake db:migrate
RAILS_ENV=production rake db:seed

Reboot the instance to load the new env variables. Then start the unicorn and nginx.

sudo service unicorn start
sudo service nginx start

===========================

Additional Information

Multiple sites

sudo ln -s /etc/nginx/sites-available/cataluv.conf /etc/nginx/sites-enabled/cataluv.conf

This step will help you with github settings, so that you don't need to enter your github account and password everytime you make changes.

Please reference this page for details.

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_rsa
$ pbcopy < ~/.ssh/id_rsa.pub
# copy the content to clipboard
ssh -T [email protected]
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment