Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save RodCyb3Dev/929d94c5f095d6db5d6ac25626a4f548 to your computer and use it in GitHub Desktop.

Select an option

Save RodCyb3Dev/929d94c5f095d6db5d6ac25626a4f548 to your computer and use it in GitHub Desktop.
Complete guide for deploying rails application to aws ec2 instance, using capistrano as deploying tool with nginx & puma server

Deploy Rails App to AWS

Creating AWS EC2 Instance

- login to 'AWS Management Console' (https://aws.amazon.com/console/)
- from 'Services'(in navbar) choose 'EC2'
- from 'Create Instance' section, click on 'Launch Instance'
- then select 'AMI' (Amazon Machine Image), we will be using 'Ubuntu Server 16.04 LTS (HVM)' as example
- select 'Instance Type' as per your requirement
- then click 'Next:Configure Instance Details' to continue
  change 'Configure Instance Details' or used as default settings
- click 'Next: Add Storage'
  adjust size as per your need (default is 8GB)
- click 'Next: Tag Instance'
  create tag if you need any tagging for your instance
- click 'Next: Configure Security Group'
  click 'Add Rule' to add new rule(for port, ip address etc.)
  default you can set it to -
  SSH      TCP     22     Anywhere     0.0.0.0/0
  HTTP     TCP     80     Anywhere     0.0.0.0/0 
- then click 'Review and Launch'
  check all your setting, then click 'Launch'
- select existing or create new keypair to connect to your instance
  click 'Launch Instance', this will generate the key (in case of new keypair) & launches your instance
  find 'Public IP' from 'Description'

Setup Elastic IP

- you can generate an Elastic IP from 'Network & Security' tab
- click 'Allocate new address'
- then click 'Allocate', this will generate an Elastic IP
- click 'Close', this will redirect you to Elastic IP Dashboard
- select you Elastic IP & on right click select 'Associate address'
- choose you instance (whom you want to associate) & click 'Associate'
  this will replace your Public IP with Elastic IP (& now this IP is used)

Setup the server to connect

#login to your server from terminal using pem file (generated private key)
- ssh -i "<Path of your pem file>" ubuntu@<Public IP Address>
- sudo apt-get update && sudo apt-get -y upgrade #to update existing packages
- create 'deploy' user for deploying the application to server
- sudo useradd -d /home/deploy -m deploy #the application will be deployed in /home/deploy directory
- sudo passwd deploy #set the password for 'deploy' user
- run 'sudo visudo' & add 'deploy ALL=(ALL:ALL) ALL' into the file & save

Generating ssh-key

- su - deploy
- ssh-keygen #do not set a passphrase for the key
- cat .ssh/id_rsa.pub
- copy the ssh-key & set it as your deploy key on your repository(project on bitbucket or github)
- copy your system public ssh-key & paste it into the following file on your instance as deploy user
  nano .ssh/authorized_keys #save & exit
- sudo apt-get install git #install git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment