/auto-deploy-base-app

A Ruby on Rails starter app that can be cloned and configured for automatic deployment using AWS, Passenger, Nginx, and Capistrano.

Primary LanguageRubyMIT LicenseMIT

Ruby on Rails Automatic Deployment Starter App

Step 1 - Configuring Server

Create a AWS Ubuntu EC2 instance

Use default settings for everything except when configuring the Security Group. Add a new role with the following settings:
Type: HTTP, Protocol: TCP, Port Range: 80, Source: Anywhere 0.0.0.0/0

Login To Server As Root

$ ssh -i "your-pem-file.pem" ubuntu@your-ec2-instance.compute-1.amazonaws.com  
$ sudo -s  

Setting Up The Environment

$ apt-get update  
$ apt-get install build-essential  
$ wget -O ruby-install-0.5.0.tar.gz https://github.com/postmodern/ruby-install/archive/v0.5.0.tar.gz  
$ tar -xzvf ruby-install-0.5.0.tar.gz  
$ cd ruby-install-0.5.0/  
$ make install  
$ ruby-install --system ruby 2.2.2 -- --disable-install-rdoc  
$ rm -r ~/ruby-install-*  
$ gem install bundler  

Step 2 - Configuring The User

Create A New User

$ useradd -d /home/your-new-user -m -s /bin/bash your-new-user  

Login As User

On your personal computer copy your public key (~/your/ssh/path/id_rsa.pub).
Then on the server paste the id_rsa.pub with the following:

$ mkdir -p /home/your-new-user/.ssh
$ touch /home/your-new-user/.ssh/authorized_keys 

Open the new file and paste your public key:

$ nano /home/your-new-user/.ssh/authorized_keys

Then run the following:

$ chown -R your-new-user /home/your-new-user/.ssh  
$ chmod 600 /home/your-new-user/.ssh/authorized_keys

Now login as the new user:

$ ssh your-new-user@your-ec2-instance.compute-1.amazonaws.com

If it fails try:

$ SSH_AUTH_SOCK=0 ssh your-new-user@your-ec2-insance.compute-1.amazonaws.com

Then you can run it with out SSH_AUTH_SOCK=0.

Check your ruby version:

$ ruby -v

Generate your Github deploy key:

$ ssh-keygen -t rsa

Copy the key (cat ~/.ssh/id_rsa.pub) to your Github Repos Deploy Keys under Settings.

Step 3 - Further Server Configuration

Install Git

Login as root from your personal computer:
$ ssh -i "your-pem-file.pem" ubuntu@your-ec2-instance.compute-1.amazonaws.com
$ sudo -s

Then install Git:
$ apt-get install git-core

Capistrano Configuration

In config/deploy.rb set your application name and Github Repo:

set :application, 'your-app-name'  
set :repo_url, 'git@github.com:your-github-user/your-github-repo.git'  

Then set where to deploy to:

set :deploy_to, '/home/your-new-user/app'  

In config/deploy/production.rb insert:

server 'your-ec2-instance.compute-1.amazonaws.com', user: 'your-new-user', roles: %w{app db web}

Database Configuration

Login as root again and install SQLite3's development headers:
$ apt-get install libsqlite3-dev

JavaScript Runtime Configuration

Still logged in as root run:
$ apt-get install nodejs

Deploying the first version

On your personal computer run:
$ bundle exec cap production deploy

This will create a starter structure on your Server.

Again on your personal computer generate the Production Key Secret:
$ rake secret

Copy the secret and as Root on the Server run:
$ touch /home/your-new-user/app/shared/config/secrets.yml
$ nano /home/your-new-user/app/shared/config/secrets.yml

In your secrets.yml file insert:

production:    
  secret_key_base: "your-production-secret-key"  

On your personal computer rerun the deploy command:
$ bundle exec cap production deploy

This finishes the file structure on the server (ls /home/your-new-user/app).

Step 4 - Install Nginx and Passenger

Install Curl Development Headers With SSL Support

As root run: $ apt-get install libcurl4-openssl-dev

Passenger

Then install passenger:
$ gem install passenger

Nginx

After installing Passenger run:
$ passenger-install-nginx-module

Configuring Nginx

In your nginx config file (/opt/nginx/conf/nginx.conf) put:

user  your-new-user;

at the top of the file. And then put:

server { 
  listen 80; 
  server_name your-ec2-instance.compute-1.amazonaws.com;  
  root /home/your-new-user/app/current/public; 
  passenger_enabled on; 
}  

Then start Nginx with:
$ /opt/nginx/sbin/nginx

Now check http://your-ec2-instance.compute-1.amazonaws.com/

Step 5 - Configuring The Shared Directory

Setting Up The Linked Files

In config/deploy.rb set the following:

set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')  

Then create the linked database directory:
$ touch /home/your-new-user/app/shared/config/database.yml

Edit the new database file:
$ nano /home/your-new-user/app/shared/config/database.yml

These settings can be found on your personal computer in the config/database.yml file

production:   
  adapter: sqlite3  
  pool: 5  
  timeout: 5000  
  database: db/production.sqlite3

Your Done!

When you make changes remember to commit and push your changes, and then rerun:
bundle exec cap production deploy.
To test your changes locally run:
bundle exec passenger start