Skip to content

Instantly share code, notes, and snippets.

@marvinahv
Created March 12, 2019 02:07
Show Gist options
  • Save marvinahv/d76cd0cdaac4dde0331230acca4b2182 to your computer and use it in GitHub Desktop.
Save marvinahv/d76cd0cdaac4dde0331230acca4b2182 to your computer and use it in GitHub Desktop.
setup environment cloud9 rails postgresql redis

Setup INTHOUGHT App on AWS Cloud9

Create cloud9 environment

  • From the AWS Console, go to Services > Cloud9.
  • In Cloud9, click Create New Environment.
  • Give it a name, description, and click Next step.
  • Select default Environment type (Create new instance in EC2).
  • Select an Instance type (I'm using a pretty big one, but you can test it out).
  • Leave (or modify, if desired) the other defaults and clikc Next step.

###Clone repository

  • Create and link SSH keys for environment/bitbucket.
  • Clone repository.

Install and Configure Postgres

  • sudo yum install postgresql96 postgresql96-server postgresql96-devel postgresql96-contrib postgresql96-docs
  • sudo service postgresql96 initdb
  • sudo nano /var/lib/pgsql96/data/postgresql.conf
  • Uncomment #listen_addresses and #port: listen_addresses = 'localhost' and port = 5432
  • Edit pg_dba.conf with sudo nano /var/lib/pgsql96/data/pg_hba.conf, so it looks like this:
# TYPE  DATABASE        USER            ADDRESS               METHOD
# "local" is for Unix domain socket connections only

local   all             all                                   trust

# IPv4 local connections:

host    all             ec2-user        127.0.0.1/0           trust

# IPv6 local connections:

host    all             all             ::1/128               md5
  • sudo su - postgres
  • psql -U postgres
ALTER USER postgres WITH PASSWORD 'YOUR_PASSWORD';
CREATE USER "ec2-user" SUPERUSER;
ALTER USER "ec2-user" WITH PASSWORD 'YOUR_PASSWORD';
  • postgres=# \q
  • exit
  • sudo sed -i.bak -e 's/ident$/md5/' -e 's/peer$/md5/' /var/lib/pgsql96/data/pg_hba.conf
  • sudo service postgresql96 restart
  • Start PG automatically:
sudo chkconfig --add postgresql96
sudo chkconfig postgresql96 on

Install Ruby and dependencies

  • RVM is already installed on AWS c9. But we need to install the right version with rvm install 2.2.1 .
  • Install dependencies with bundle install.
  • Init database, run migrations, etc...

Setup Rails DB config, reqstore db, and Run Migrations

  • Add these variables to your /.env for your local db (with your own settings):
export INVISION_DATABASE_NAME="invision_dev
export INVISION_DATABASE_USERNAME="ec2-user"
export INVISION_DATABASE_PASSWORD="PASSWORD_YOU_SETUP_EARLIER"
  • Run bundle exec rake db:create.
  • Restore db backup with pg_restore -d invision_dev db/snapshots/inthought2-2018-01-05.dump -c -U postgres.
  • Run migrations bundle exec rake db:migrate.

Install and configure Redis

Reference: https://medium.com/@andrewcbass/install-redis-v3-2-on-aws-ec2-instance-93259d40a3ce

  • Install dependencies:
sudo yum -y update
sudo yum -y install gcc make
cd /usr/local/src
sudo wget http://download.redis.io/releases/redis-3.2.0.tar.gz
sudo tar xzf redis-3.2.0.tar.gz
sudo rm -f redis-3.2.0.tar.gz
  • Recomplile
cd redis-3.2.0
sudo make distclean
sudo make
  • Test Installation
sudo yum install -y tcl
sudo make test
  • Make directories & copy files
sudo mkdir -p /etc/redis /var/lib/redis /var/redis/6379
sudo cp src/redis-server src/redis-cli /usr/local/bin
sudo cp redis.conf /etc/redis/6379.conf
  • Edit the config file sudo nano /etc/redis/6379.conf, and make sure you have this options:
bind 127.0.0.1                                //line 61
daemonize yes                                 //line 127
logfile "/var/log/redis_6379.log"             //line 162
dir /var/redis/6379                           //line 246
  • Download and install the init script
sudo wget https://raw.githubusercontent.com/saxenap/install-redis-amazon-linux-centos/master/redis-server
sudo mv redis-server /etc/init.d
sudo chmod 755 /etc/init.d/redis-server
  • Open the Redis server init script sudo nano /etc/init.d/redis-server and make sure you have REDIS_CONF_FILE="/etc/redis/6379.conf" # line 26.
  • Auto-enable and start the Redis server
sudo chkconfig --add redis-server
sudo chkconfig --level 345 redis-server on
sudo service redis-server start
  • Create a file to fix memeory issue sudo nano /etc/systctl.conf. And add:
# ensure redis background save issue

vm.overcommit_memory = 1
systctl vm.overcommit_memory=1  
  • Test redis server with redis-cli ping. (You should get "PONG" back)

  • To get Sidekiq to work correctly, add these environment variables and restart to load them:

REDIS_PROVIDER=REDIS_URL
REDIS_URL=redis://localhost:6379

Run & Preview Project

  • bundle exec rails server -p $PORT
  • Click on menu Preview > Preview Running Application.
  • Modify the BASE_URL in your .env file to point to the ULR you get from the Preview. Example: BASE_URL='https://0b5311e8e6664e27a6646482329949b9.vfs.cloud9.us-west-2.amazonaws.com:8080'.
  • Click on open in new tab button.

That should be it!

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