Deploying a Ruby on Rails app using Nginx and Passenger on Ubuntu 16.04 Xenial Xerus - OtagoPolytechnic/opp-cico GitHub Wiki

Overview

This tutorial will explain the steps you need to take to deploy a Ruby On Rails app to an AWS sever running Nginx and Passenger on Ubuntu 16.04 Xenial Xerus.

Launching a EC2 server on AWS

  1. Login to your AWS-Management-Console and choose EC2.

  2. Click on Launch Instance.

  3. Find and select Ubuntu Server 16.04 LTS. Click next.

  4. Find and select t2.micro. The micro server is provides enough resources for our needs.

  5. You can skip over Configure Instance Details and Add Storage. The defaults are fine.

  6. Next we are asked to fill out a value for the key Name. In the value section give your server a name.

  7. Add an HTTP rule. Keep the default SSH rule.

  8. Next move to the review page and click Launch.

  9. You will now be asked to add an existing key pair. Select the bit-platform-dev.

  10. Your server will now start to Launch.

Connecting to the server

Now we have the sever setup and launched the next step is to connect to it using SSH. We can now SSH with the bit-platform-dev key.

ssh -i bit-platform-dev.pem ubuntu@"YOUR SERVER IP HERE"

If the connection is unsuccessful you can try this command

chmod 400 bit-platform-dev.pem

Adding a Sudo User

It is best practice to make a new user that will manage our CICO app instead of using the root user. We can add a new user by typing this command. I recommend giving the user a password for better security.

adduser cico

Now we have the new user we will want to give the new user sudo privileges.

gpasswd -a cico sudo

You can now switch to the new user. You will be prompted to enter the password.

su cico

You will still be in root users home directory, enter cd to enter the cico home directory.

Preparing the system

Ensure that curl and gpg are installed.

sudo apt-get update
sudo apt-get install -y curl gnupg build-essential

Installing Ruby

Next we need to install some dependencies for Ruby.

sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

Next we will install Ruby using rbenv. Most people prefer using rbenv these days as it has the least issues.

Installing with rbenv is a simple two step process. First you install rbenv, and then ruby-build.

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

rbenv install 2.3.1
rbenv global 2.3.1
ruby -v

The last step is to install Bundler.

gem install bundler

rbenv users need to run the rehash command after installing bundler.

rbenv rehash

Installing Rails

The next step is to install a JavaScript runtime like NodeJS that will allow us to use Coffeescript and the Asset Pipeline in Rails.

To install NodeJS, we’re going to add it using the official repository.

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

Install Rails

gem install rails -v 4.2.6

rbenv users need to run the rehash command again to make the rails executable available.

rbenv rehash

We can easily check everything has been installed correctly by running this command.

rails -v
# Rails 4.2.6

Setting Up PostgreSQL

We are going to use a PostgreSQL database with out Rails app. We will install PostgreSQL using an apt-key.

sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.5 libpq-dev

It is good practice to make our own user for PostgreSQL.

sudo -u postgres createuser "YOUR USERNAME HERE" -s

Now we will login in with the default PostgreSQL user and set the password of the new account we just created.

sudo -u postgres psql
postgres=# \password "YOUR USERNAME HERE"

Now you will want to locate your pg_hba.conf at /etc/postgresql/9.5/main/ and use nano to edit it. In the conf file you will want to change all lines that say peer to trust. This allows the rails app to access your database.

Installing Nginx and Passenger

These commands will install Passenger + Nginx through Phusion’s APT repository.

# Install PGP key and add HTTPS support for APT
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates

# Add APT repository
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update

# Install Passenger + Nginx
sudo apt-get install -y nginx-extras passenger

Next we want to edit /etc/nginx/nginx.conf and uncomment include /etc/nginx/passenger.conf;.

For example, you may see this:

# include /etc/nginx/passenger.conf;

Remove the ‘#’ characters, like this:

include /etc/nginx/passenger.conf;

When you are finished with this step, restart Nginx:

sudo service nginx restart

Now we should check the installation of Nginx and Passenger by running:

sudo /usr/bin/passenger-config validate-install

All checks should pass.

Nginx updates, Passenger updates and system updates are delivered through the APT package manager regularly. You should run the following command regularly to keep them up to date:

sudo apt-get update
sudo apt-get upgrade

Configure Git

Now we need to configure Git so we can pull down the code. You find out how to do this here. The steps are exactly the same.

Cloning the app

You need to pick a location in which to permanently store your application’s code. A good location is /var/www/APP_NAME. Let us create that directory.

sudo mkdir -p /var/www/cicoapp
sudo chown cico: /var/www/cicoapp

Now let us pull the code from Git:

cd /var/www/cicoapp
git clone [email protected]:OtagoPolytechnic/opp-cico.git

Your app’s code now lives on the server at /var/www/opp-cico/code.

Install app dependencies

Now we need to install the app dependencies

cd /var/www/cicoapp/opp-cico/cico
bundle install --deployment --without development test

Configure database and secrets

Now we need to configure the database.yml and secrets.yml files. You will find example files in the config folder.

Just add your PostgreSQL username and password that we created earlier to the database.yml file.

nano config/database.yml

After that we need to run this command and add it to the production section of the secrets.yml

bundle exec rake secret
nano config/secrets.yml

To prevent other users on the system from reading sensitive information belonging to your app, let’s tighten the security on the configuration directory and the database directory.

chmod 700 config db
chmod 600 config/database.yml config/secrets.yml

Compile Rails assets and run migrations

Now we will want to compile assets such as scss and JavaScript files, and run database migrations to the production database.

rake db:create RAILS_ENV=production
bundle exec rake assets:precompile db:migrate RAILS_ENV=production

Configure Nginx and Passenger

Now we need to tell Passenger which ruby command it should use to run the app. Use this command to find it and copy the file path after Command:.

passenger-config about ruby-command

Edit Nginx configuration file

We need to create an Nginx config file so Nginx knows how to run the app.

cd /etc/nginx/sites-enabled/
touch cico.conf
nano cico.conf

Put this inside the file.

server {
  listen 80;
  server_name yourserver.com;

  # Tell Nginx and Passenger where your app's 'public' directory is
  root /var/www/myapp/code/public;

  # Turn on Passenger
  passenger_enabled on;
  passenger_ruby /path-to-ruby;
}
  • Replace server name with the ip address of your server.

  • Paste the path to the public directory of your rails app.

  • Paste the ruby command file path we copied before into the passenger_ruby section.

When you are done, restart Nginx

sudo service nginx restart

You should now be able to find your web app at the server’s ip address just by pasting the ip into your web browser.

The next steps would be to give your server a domain name.

⚠️ **GitHub.com Fallback** ⚠️