Skip to content

Instantly share code, notes, and snippets.

@jpbalarini
Last active February 15, 2017 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpbalarini/a9a87c9c17ada32908de to your computer and use it in GitHub Desktop.
Save jpbalarini/a9a87c9c17ada32908de to your computer and use it in GitHub Desktop.
How to create a RoR Bitnami Amazon EC2 instance

RoR Bitnami Amazon EC2 instance

This guide will provide you some basic steps to create a Bitnami RoR Amazon EC2 instance, using Nginx with Passenger.

  1. Create an Amazon EC2 instance using a Community AMI. At the time of writing, the latest Bitnami AMI is: bitnami-rubystack-2.2.4-4-linux-ubuntu-14.04.1-x86_64-hvm-ebs Be careful to open SSH, HTTP and HTTPS ports on the "Step 6: Configure Security Group". You will need to save also the certificate to later connect to the instance.

  2. Log in by SSH to the instance:

    ssh -i cert.pem bitnami@public-ip
    

    Where cert.pem is the certificate you created when launching the instance and public-ip is the instance public IP (can be found on the EC2 instances tab).

  3. Once logged in, start the ssh agent:

    eval `ssh-agent -s`
    

    This will be used to clone the repository where the code is.

  4. Genereate a new ssh key without passphrase:

    ssh-keygen -t rsa -b 4096 -C "name@email.com”
    

    And press a lot of enters.

  5. Copy the generated public key:

    cat ~/.ssh/id_rsa.pub 
    
  6. Paste the public key where the code is hosted (SSH Keys)

  7. Go to your root folder:

    cd ~
    
  8. Clone your code:

    git clone git@your-repo.git
    
  9. Go to your project root and install all your gems.

    bundle install
    
  10. Create a database for your app

    1. Connect to postgres (password is bitnami)

      psql -U postgres
      
    2. Create the db

      CREATE DATABASE YOUR_DATABASE_NAME;
      
    3. Exit

      \q
      
  11. Configure your config/database.yml file

    production:
      adapter: postgresql
      database: YOUR_DATABASE_NAME
      host: localhost
      user: postgres
      password: bitnami
    
  12. Setup up your db

    export RAILS_ENV=production
    rake db:migrate
    
  13. Precompile your assets

    rake assets:precompile
    
  14. Create a secret key for your application

    rake secret
    

    Ideally, the output from this command should be saved as an environment variable named $SECRET_KEY_BASE, which will then be read by Rails as needed. If this doesn’t work for you, you can also hardcode the key into the config/secrets.yml file, although this is less secure.

    export SECRET_KEY_BASE=previous_output
    

    Then, on config/secrets.yml

    production:
      # secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
      secret_key_base: output_from_rake_secret
    
  15. Make the directories myapp/, myapp/config, myapp/public and their parent directories readable and executable by the Nginx Web server. This server runs as the user 'daemon', so change the group and permissions accordingly.

    sudo chgrp daemon .
    sudo chgrp daemon -R config public
    chmod 755 .
    chmod 750 -R config public
    
  16. Configure Nginx

    1. Open the ngix config file:

      sudo nano ~/stack/nginx/conf/nginx.conf
      
    2. Leave everything as it is, except for the server part. It should be something like this:

      ...stuff...
      
      server {
          # port to listen on. Can also be set to an IP:PORT
          listen 80;
          server_name coolschoolservice.com;
          root /home/bitnami/coolschoolservice/public;
          passenger_enabled on;
          rails_env production;
      
          # PageSpeed
          #pagespeed on;
          #pagespeed FileCachePath /opt/bitnami/nginx/var/ngx_pagespeed_cache;
          #  Ensure requests for pagespeed optimized resources go to the pagespeed
          #  handler and no extraneous headers get set.
          #location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
          #location ~ "^/ngx_pagespeed_static/" { }
          #location ~ "^/ngx_pagespeed_beacon$" { }
          #location /ngx_pagespeed_statistics { allow 127.0.0.1; deny all; }
          #location /ngx_pagespeed_message { allow 127.0.0.1; deny all; }
      }
      ...stuff...
      
    3. Save and exit

    4. Stop apache

      sudo /opt/bitnami/ctlscript.sh stop apache
      
    5. Stop apache from starting at boot up

      sudo mv /opt/bitnami/apache2/scripts/ctl.sh /opt/bitnami/apache2/scripts/ctl.sh.disabled
      
      sudo mv /opt/bitnami/config/monit/conf.d/apache.conf /opt/bitnami/config/monit/apache.conf.disabled
      
    6. Restart nginx

      sudo /opt/bitnami/ctlscript.sh restart nginx
      
  17. Congratulations, you should have your environment up and running!

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