Skip to content

Instantly share code, notes, and snippets.

@acushlakoncept
Last active May 13, 2023 06:15
Show Gist options
  • Save acushlakoncept/bd1442defbef92e60d0b40828069acce to your computer and use it in GitHub Desktop.
Save acushlakoncept/bd1442defbef92e60d0b40828069acce to your computer and use it in GitHub Desktop.

Source deployment_aws_capistrano

Setup EC2 instance with Ruby and RVM

Step by step instructions to setup your EC2 server for deploying a Rails application. Reference here

  • Login to your EC2 instance
 ubuntu@ip-172-30-0-195:~$ pwd
 /home/ubuntu
  • Switch to root user privileges to update system and install Ubuntu packages
 # from ubuntu@ip-172-30-0-195:~$
  sudo -s

  # from root@ip-172-30-0-195:~#
  apt-get update
  apt-get -y upgrade
  apt-get -y install build-essential libmagickcore-dev imagemagick libmagickwand-dev libxml2-dev libxslt1-dev git-core nginx redis-server curl nodejs htop

  # Set Rails environment
  echo "RAILS_ENV=production" >> /etc/environment

  # Add a gemrc file to ubuntu user
  echo -e "verbose: true\nbulk_threshold: 1000\ninstall: --no-ri --no-rdoc --env-shebang\nupdate: --no-ri --no-rdoc --env-shebang" > /home/ubuntu/.gemrc
  chmod 644 /home/ubuntu/.gemrc
  chown ubuntu:ubuntu /home/ubuntu/.gemrc
  exit
  ubuntu@ip-172-30-0-195:~$ 
  • Install RVM and Ruby - reference here
 \curl -sSL https://get.rvm.io | bash -s
  source ~/.rvm/scripts/rvm

  rvm install 2.2.2
  rvm use 2.2.2 --default
  # Using /home/ubuntu/.rvm/gems/ruby-2.2.2

  ruby -v
  # ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]

  gem install bundler
  # Fetching: bundler-1.11.2.gem (100%)
  # Successfully installed bundler-1.11.2
  # 1 gem installed

Install and configure Nginx, Passenger and PostGreSQL

  • Switch to root user privileges
   # from ubuntu@ip-172-30-0-195:~$
  sudo -s
  • Install Git + Passenger + Nginx
  apt-get install -y git

  apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
  apt-get install -y apt-transport-https ca-certificates
  sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main > /etc/apt/sources.list.d/passenger.list'

  # update ubuntu sources
  apt-get update
  apt-get install -y nginx-extras passenger

If you are running Ubuntu 20.04 source here

  echo deb [arch=amd64] https://oss-binaries.phusionpassenger.com/apt/passenger focal main > /etc/apt/sources.list.d/passenger.list
  apt update
  apt-get install -y libnginx-mod-http-passenger
  • Enable the Passenger Nginx module
   # vi or nano, your choice
  vi /etc/nginx/nginx.conf
   # uncomment include /etc/nginx/passenger.conf; if it is commented
  • Add the following Nginx server configuration
  server {
    listen 80;
    server_name 52.77.234.2; #replace with your server ip or domain

    # specify the application's 'public' directory 
    root /home/ubuntu/apps/merchant/current/public;

    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /home/ubuntu/.rvm/rubies/ruby-3.0.0/bin/ruby; # type `which ruby` to get this path
  }
  • Setting Up PostgreSQL
    source here
   # from ubuntu@ip-172-30-0-195:~$
   sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
   wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
   sudo apt install postgresql postgresql-contrib
   sudo apt-get install -y libpq-dev
   sudo systemctl start postgresql.service
  • Test that pg gem can install properly with native extensions
   gem install pg -v '1.1'
   
 # Building native extensions. This could take a while...
 # Successfully installed pg-1.1.0
 # Parsing documentation for pg-1.1.0
 # Installing ri documentation for pg-1.1.0
 # Done installing documentation for pg after 1 seconds
 # 1 gem installed
  • Configure PostgreSQL to accept all connections
 ls /etc/postgresql
 sudo vim /etc/postgresql/15/main/pg_hba.conf # replace the number with yours

  # change the following lines to METHOD 'trust', and save
  # =======================
  # TYPE  DATABASE        USER            ADDRESS                 METHOD

  # "local" is for Unix domain socket connections only
  # local   all             all                                     trust
  # IPv4 local connections:
  # host    all             all             127.0.0.1/32            trust
  # IPv6 local connections:
  # host    all             all             ::1/128                 trust

  sudo service postgresql reload
  sudo -u postgres createuser ubuntu -s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment