artimys
7/9/2017 - 3:49 PM

Amazon EC2: How to setup Rails server

How to setup a Ruby on Rails web server on Amazon EC2 Ubuntu Server.

1) Change Timezone
  -Terminal: http://manpages.ubuntu.com/manpages/yakkety/en/man1/timedatectl.1.html
    sudo timedatectl set-timezone <timeszone>
    sudo timedatectl set-timezone EST
    - View other timezones
    timedatectl list-timezones
    
  -GUI:
    sudo dpkg-reconfigure tzdata


2) Update Hostname
hostname    # View current hostname
sudo nano /etc/hostname # change hostname
sudo nano /etc/hosts # change hostname where it references old hostname
sudo reboot # reboot sever to see change

3) View mounted drives
df

4) List to ports
sudo netstat -an | grep 80

#Amazon EC2 Rails App Guide Ubuntu v 16.04, rbenv, nginx, phusion passenger, sqlite

1) Ubuntu Packages

# Update current packages
sudo apt-get update && sudo apt-get -y upgrade

# Bare Essential Packages for Rails app:
sudo apt-get install
  autoconf 
  bison
  build-essential
  libssl-dev
  libreadline-dev
  libreadline6 
  libreadline6-dev 
  libyaml-dev
  libffi-dev 
  zlib1g 
  zlib1g-dev
  nodejs
    
    
    
# - SQLite Install
sudo apt-get install libsqlite3-dev sqlite3 

Update Hostntame / Timezone

# Select timezone from GUI
sudo dpkg-reconfigure tzdata

# Change hostname if public DNS
sudo nano /etc/hostname   # ex: webserver.example.com
sudo reboot

# or

# Change hostname if no plubic DNS
sudo nano /etc/hostname   # ex: webserver.localdomain.com
sudo nano /etc/hosts      # change 127.0.0.1 webserver.localdomain webserver localhost4 localhost4.localdomain4
sudo reboot

Create new user to deploy Rails app

# Create a new user called deploy
sudo adduser deploy --disabled-password   # --disabled-password only fur Ubuntu servers
sudo su - deploy                          # switch to deploy user

Give new user sudo power

sudo visudo                             # Open editor
deploy ALL=(ALL:ALL) NOPASSWD:ALL       # username ALL=(ALL:ALL) NOPASSWD:ALL
                                        # NOPASSWD:ALL option prevents password prompt when using sudo command

Create SSH key for deploy user

# On Local machine
ssh-keygen                            # press enter at password prompt for no password
                                      # Generates id_rsa and id_rsa.pub, copy id_rsa.pub

# On Server, while logged on as deploy user:
mkdir ~/.ssh && chmod 700 ~/.ssh
mkdir ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
sudo vi ~/.ssh/authorized_keys        # Paste key into authorized_keys file
                                      # to edit: press 'i' then type/paste
                                      # to save/exit: press 'ESC' then ':x' then press enter

# SSH to server
# Plot in the values: ssh -i 'path-to-your-private-key' username@amazon-server-url
ssh -i 'id_rsa' deploy@ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com

Install rbenv

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

# To test type:
type rbenv

Install ruby-build

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Install ruby

rbenv install -l      # View list of ruby versions
rbenv install 2.3.1   # Run 'rbenv versions' to see installed ruby versions
rbenv global 2.3.1

rbenv rehash          # Run every time a new version of ruby is installed

Setup rubygems

echo "gem: --no-document" > ~/.gemrc    # Does not install any gem docs for future gem installations
                                        # echo "gem: --no-rdoc --no-ri" > ~/.gemrc (RVM)
gem install bundler
rbenv rehash                            # Run every time a gem is installed

Install nginx

sudo apt-get install nginx