This is a brief note about deploying a Ruby on Rails application on an AWS EC2 server.

Assuming you have created an EC2 instance with Ubuntu 18 and connected to it with puTTy.

Install Dependencies

sudo apt install curlcurl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash — curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add — echo “deb https://dl.yarnpkg.com/debian/ stable main” | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt-get update 
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn

Install rbenv

cd git clone https://github.com/rbenv/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc exec $SHELL  git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc exec $SHELL

Install Ruby and set the default version.

rbenv install 3.1.0rbenv global 3.1.0ruby -vgem install bundler

In case of switching the ruby version, just install the new ruby version, then use “rbenv global 2.5.8” or “rbenv local 2.5.8” to switch the ruby version.

Install Rails

gem update --systemgem install rails -v 6.1rails -v

In case of switching rails version, just uninstall rails and install the new version.

gem uninstall rails -v 6.1gem uninstall railties -v 6.1

Install application

Take the geoblacklight for example. Create the application with:

DISABLE_SPRING=1 rails new gbl -m https://raw.githubusercontent.com/geoblacklight/geoblacklight/feature/1225-bug-fix-for-template-install/template.rbcd gblbundle install

Since geoblacklight depends on solr and solr depends on java, we need to install java as well:

sudo apt install default-jre

Then start the application server by:

rake geoblacklight:server

If everything goes well, the server will start listening on port 3000:

Install and Config Nginx

sudo apt update
sudo apt install nginx
sudo service nginx start

Configure nginx at /etc/nginx/sites-enabled/default as below:

Configure application

add below line in config/environments/development.rb

config.hosts << “<your Public IPv4 DNS from AWS>ec2–18–138–252–16.ap-southeast-1.compute.amazonaws.com”

Then restart the application, we can access the application from Public IPv4 DNS address now:

Run rails server in production mode

Create and Migrate DB for production mode:

RAILS_ENV=production rake db:create db:migrate db:seed

Add configuration in config/environments/production.rb

config.hosts << “<your Public IPv4 DNS from AWS>ec2–18–138–252–16.ap-southeast-1.compute.amazonaws.com”config.assets.compile = true

Generate a Rails Secret Key:

rake secret

Export the output as environment variables:

export SECRET_KEY_BASE=output-of-rake-secret

Precompile the assets:

RAILS_ENV=production rake assets:precompile

Start the application:

RAILS_ENV=production rake geoblacklight:server

Index some sample data:

rake geocombine:index\

Access the application through the Public IPv4 DNS:

Mission accomplished!

Happy coding!

Particular thanks to Eric Larson , Majew

--

--

Shawn Wong
Shawn Wong

Written by Shawn Wong

Ruby on Rails developer, AWS cloud practitioner, GeoBlacklight, Omeka practitioner.

Responses (1)