Skip to content

Instantly share code, notes, and snippets.

@jamesfwz
Last active February 20, 2020 15:00
Show Gist options
  • Save jamesfwz/8492029e44cbe3fc549a to your computer and use it in GitHub Desktop.
Save jamesfwz/8492029e44cbe3fc549a to your computer and use it in GitHub Desktop.
Auto Deploy AWS

This guide setup Apache2/Passenger/Ruby/Rails/Nginx on CentOS 6.6 64bit

Pre-deployment

# Use ec2-user mode
rvm gemset create <gemset>
rvm gemset use <gemset>
Next, we need to setup the SSH for Capistrano to access the Git:
```ruby
test -e ~/.ssh/id_dsa.pub || ssh-keygen -t dsa # Enter 3 times
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

# Need to do this for CentOS
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys 
restorecon -Rv ~/.ssh

# Accepting host authenticity for user
# Test it (you need to accept the host authenticity):
ssh localhost
# if you did not see any prompt for password, it is done!

# Test: accept host authenticity for git
git ls-remote ssh://ec2-user@[domain]/~/git/[app].git master

Access SSH via authorized_keys

# Your localhost iTerm:
vi ~/.ssh/id_rsa.pub # Copy local ssh key (Command + C)
# ec2-user mode:
vi ~/.ssh/authorized_keys # Paste local ssh key to AWS (Command + V) 

install MINA

# GemFile
gem 'mina'
bundle install
mina init
# Created config/deploy.rb.

config/deploy.rb

require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
# require 'mina/rbenv'  # for rbenv support. (http://rbenv.org)
require 'mina/rvm'    # for rvm support. (http://rvm.io)

# Basic settings:
#   domain       - The hostname to SSH to.
#   deploy_to    - Path to deploy into.
#   repository   - Git repo to clone from. (needed by mina/git)
#   branch       - Branch name to deploy. (needed by mina/git)

# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
app           = ''
domain        = ''
user          = 'ec2-user'
ruby_version  = 'ruby-2.2.2'
ruby_gemset   = ''

set :term_mode, nil
set :shared_paths, ['config/database.yml', 'log']
set :app, app
set :domain, domain
set :repository, "ssh://#{user}@#{domain}/~/git/#{app}.git"
set :user, user
set :rvm_path, "/usr/local/rvm/bin/rvm"

# Optional settings:
#   set :user, 'foobar'    # Username in the server to SSH to.
#   set :port, '30000'     # SSH port number.

# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.

task :environment do
  # If you're using rbenv, use this to load the rbenv environment.
  # Be sure to commit your .rbenv-version to your repository.
  # invoke :'rbenv:load'
  stage = ENV['to'] || 'production'
  case stage
  when 'staging'
    set :branch, 'staging'
  when 'development'
    set :branch, 'development'
  when 'production'
    set :branch, 'master'
  else
    print_error "Please specify a stage. eg. mina deploy to=production"
    exit
  end

  set :rails_env, stage
  set :deploy_to, "/home/#{user}/#{stage}"

  invoke :"rvm:use[#{ruby_version}@#{ruby_gemset}]"
  # For those using RVM, use this to load an RVM version@gemset.
  # invoke :'rvm:use[ruby-1.9.3-p125@default]'
end



# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do
  queue! %[mkdir -p "#{deploy_to}/shared/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]

  queue! %[mkdir -p "#{deploy_to}/shared/config"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]

  queue! %[touch "#{deploy_to}/shared/config/database.yml"]
  queue  %[echo "-----> Be sure to edit 'shared/config/database.yml'."]

  queue %[bundle config build.pg --with-pg-config=/usr/lib64/pgsql93/bin/pg_config]
end

desc "Deploys the current version to the server."
task :deploy => :environment do
  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.

    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'

    to :launch do
      queue "mkdir -p #{deploy_to}/#{current_path}/tmp"
      queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt"
    end
  end
end

desc "Seed data to the database"
task :seed => :environment do
  queue "cd #{deploy_to}/#{current_path}/"
  queue "bundle exec rake db:seed RAILS_ENV=#{rails_env}"
  queue  %[echo "-----> Rake Seeding Completed."]
end

# For help in making your deploy script, see the Mina documentation:
#
#  - http://nadarei.co/mina
#  - http://nadarei.co/mina/tasks
#  - http://nadarei.co/mina/settings
#  - http://nadarei.co/mina/helpers
# Your localhost iTerm
mina setup
# your localhost iTerm:
git remote add aws ssh://ec2-user@[domain]/~/git/[app].git
git push aws master

# deploy
mina deploy

Set Environment

# logout and login again:
ssh ec2-user@[domain]

# Set environment
vim ~/.bash_profile

~/.bash_profile

PATH=$PATH:$HOME/.local/bin:$HOME/bin
SECRET_KEY_BASE="1b6f401fe7511d30017117b4a74111baa6d0d4a9fcb43de86c901ae487975577a32b29d29fa26fcace631b9121ec93d83809ef7edc77169b8a11d6de4f723d4c"
HOST="in2ideas.com"
...
ADMIN_EMAIL="[email protected]"

export PATH SECRET_KEY_BASE HOST ADMIN_EMAIL

Restart nginx

service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment