Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails, Capistrano & AWS

alienxp03
December 23, 2016

Rails, Capistrano & AWS

alienxp03

December 23, 2016
Tweet

More Decks by alienxp03

Other Decks in Programming

Transcript

  1. EC2

  2. TEXT EC2 ▸ Ubuntu Server 16.04 LTS (HVM), SSD Volume

    Type ▸ Security Group (Important!!!11!!) ▸ Firewall thingy ▸ Make sure in the same VPC as all other RDS, ElastiCache, etc ▸ Configure Security Group ▸ SSH: 22 ▸ HTTP: 80 ▸ Download and store .pem file. You can’t download it again if you lost it
  3. TEXT RDS ▸ Make sure in the same VPC ▸

    Open port 5432 in Security Group
  4. TEXT ELASTICACHE (REDIS) ▸ Make sure in the same VPC

    ▸ Open port 6379 in Security Group
  5. TEXT CONFIGURE EC2 ▸ ssh -i "carpit.pem" [email protected] ▸ sudo

    apt-get update && sudo apt-get -y upgrade ▸ sudo useradd -d /home/deploy -m deploy ▸ sudo passwd deploy ▸ visudo ▸ deploy ALL=(ALL:ALL) ALL ▸ su - deploy ▸ Should run all instructions after this as user deploy
  6. TEXT RBENV ▸ git clone https://github.com/rbenv/rbenv.git ~/.rbenv ▸ echo 'export

    PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc ▸ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build ▸ rbenv install 2.3.1 ▸ # Time for coffee ▸ rbenv global 2.3.1 ▸ rbenv rehash
  7. TEXT NGINX ▸ sudo nano /etc/nginx/sites-available/default upstream app { #

    Path to Puma SOCK file, as defined previously server unix:///home/deploy/carpit/app/current/tmp/sockets/puma.sock fail_timeout=0; } server { listen 80; server_name localhost; root /home/deploy/carpit/app/current/public; try_files $uri/index.html $uri @app; location @app { proxy_pass http://app; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; } error_page 500 502 503 504 /500.html; client_max_body_size 4G; keepalive_timeout 10; }
  8. TEXT CLOUDWATCH LOGS ▸ Create a custom policy ▸ Create

    a new user (need to use API key, secret key) ▸ sudo apt-get update ▸ cd ~ ▸ curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/ awslogs-agent-setup.py -O ▸ sudo python ./awslogs-agent-setup.py --region ap-southeast-1 ▸ puma_log at /home/deploy/carpit/app/current/log/puma_access.log
  9. TEXT CUSTOM POLICY { "Version": "2012-10-17", "Statement": [ { "Effect":

    "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents", "logs:DescribeLogStreams" ], "Resource": [ "arn:aws:logs:*:*:*" ] } ] }
  10. TEXT GEMFILE group :development do gem 'capistrano', '~> 3.6' gem

    'capistrano-rails', '~> 1.2' gem 'capistrano-bundler' gem 'capistrano-rbenv' gem 'capistrano3-puma' gem 'capistrano-sidekiq' end
  11. TEXT CAPFILE require 'capistrano/setup' # Include default deployment tasks require

    'capistrano/deploy' # Others require 'capistrano/rbenv' require 'capistrano/bundler' require 'capistrano/rails/assets' require 'capistrano/rails/migrations' require 'capistrano/puma' require 'capistrano/sidekiq' # Load custom tasks from `lib/capistrano/tasks` if you have any defined Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
  12. TEXT PRODUCTION.RB ▸ lock '3.7.1' # config valid only for

    current version of Capistrano ▸ set :application, 'carpit' ▸ set :repo_url, '[email protected]:surialabs/carpit-backend.git' ▸ set :branch, ENV['BRANCH'] || 'master' ▸ set :deploy_to, '/home/deploy/carpit/app' ▸ set :rbenv_path, '/home/deploy/.rbenv' ▸ set :rbenv_ruby, '2.3.1' ▸ append :linked_files, 'config/database.yml' ▸ append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', ‘tmp/sockets', 'public/system', 'vendor/bundle' ▸ set :sidekiq_concurrency, 7 ▸ set :sidekiq_options_per_process, ["--queue default --queue quotes"]
  13. TEXT NOTES ▸ Make sure the EC2 instance SSH keys

    are added in Github ▸ Add your own SSH keys in ~/.ssh/authorized_keys to SSH without .pem file ▸ If you want to skip CI (so other people can build their PR), add line —skip-ci in your current branch commit log