How to set up a Ruby on Rails stack with Nginx using Faker

Created by Jordy Leffers at 06-12-2017 11:05:35 +0100

Ruby on rails is a development framework that enables faster development. The decision of its developers to prefer convention above configuration doesn't only speed up the process, but it also allows for more agile development as it becomes easier to switch between (parts of) projects. 

In this guide we'll go over how to set up your Linux server to run a Ruby on Rails application, as well as install another package called Faker that will help us generate test data so we can populate our database without having to come up with all this data ourselves.

Before we start, we need a Linux installation. We'll skip that step in this tutorial since you can easily get a default linux installation on one of the containers on the www.cloudcontainers.net website. This tutorial is based on the cloud containers created on the my.cloudcontainers.net page. This means that you are by default the root user, so all of the commands below don't make use of sudo. If however, you are not the root user on your system, you'll have to add "sudo" in front of the commands found in the guide below.

 

Essentials

In this tutorial I'll be using the text editor nano. You can use whichever you want, but if you don't have nano installed, this is the command:

apt install nano

 

Start off by updating your packages:

apt update && apt upgrade -y

 

Next, we'll install the packages Ruby will rely on, there are quite a few dependencies:

apt install build-essential dirmngr gnupg ruby ruby-dev zlib1g-dev libruby libssl-dev libpcre3-dev libcurl4-openssl-dev rake ruby-rack -y

 

We'll also need a Javascript runtime environment:

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

Check to see if NodeJS installed correctly:

node -v

Install Ruby on rails

We can now install Ruby, however, to run its installation script, we'll need its public key first:

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

 

Once we have imported the GPG key, we can run the RVM installation script, this might take a while as it installs Ruby itself aswell:

curl -sSL https://get.rvm.io | bash -s stable --ruby

At the end of the installation process, you'll be instructed to run a command that must be executed before using RVM:

source /usr/local/rvm/scripts/rvm


You can check if your system has the right requirements for RVM:

rvm requirements

Set this version as your system default

rvm --default use ruby-2.5.1 (Modify the version to what's appropriate for your project)

Now we've installed Ruby, we can install Rails:

 gem install rails -v 5.2.0 (Modify the version to what's appropriate for your project)

Install and configure Nginx

Nginx is the webserver we will use to display our application to the end user eventually, install it now, we'll configure it later.

apt install nginx -y

Now we've installed Nginx, we're ready to configure Nginx. First, we want to make a copy of its default server block:

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/rubyApp

Next, we configure this template to match our system:

nano /etc/nginx/sites-available/rubyApp

Here we'll make two changes:

  • Set your server_name to your domain or IP address.
  • Point root to your application's public folder



Apart from some additional comments, your file should now roughly look like this:

server {        
        listen 80 default_server;        
        listen [::]:80 default_server;         

        root /root/rubyApp/public;         
        
        # Add index.php to the list if you are using PHP        
        index index.html index.htm index.nginx-debian.html;         

        server_name 83.96.162.105;         
        
        location / {                
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;        
                   }         
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        
        #        
        #location ~ \.php$ {        
        #       include snippets/fastcgi-php.conf;        
        #        
        #       # With php7.0-cgi alone:        
        #       fastcgi_pass 127.0.0.1:9000;        
        #       # With php7.0-fpm:        
        #       fastcgi_pass unix:/run/php/php7.0-fpm.sock;        
        #}

        # deny access to .htaccess files, if Apache's document root        
        # concurs with nginx's one        
        #        
        #location ~ /\.ht {        
        #       deny all;        
        #} 
}

 

You can test your configuration file for any syntax errors by entering the following command:

nginx -t

If the test is successful, restart Nginx:

service nginx restart


You've now successfully installed and configured Ruby on Rails with Nginx!

Run your Ruby project by going in to you application's folder and using the following command:

rails server

 

Navigate to your server's IP address followed by your rails server's port:

Like this: 85.95.163.72:3000

How to install Faker

Faker can be very useful, as it helps populate our databases with test data. This is especially handy because it generates the data for you! Preventing you or your team from having to go through the pain of having to come up with all of this test-data yourself, you can now have a fairly populated database in minutes.

Faker can generate all kinds of data, including but not limited to: Names, e-mail addresses, passwords, phone-numbers etc.

 

Navigate to your application's folder:

cd rubyApp

Open the file named "Gemfile". In this file you'll find all libraries (gems) installed in Rails.

nano Gemfile

Here, we add " gem 'faker' " to the file like this:

Exit and save the file. Run the "bundle install" command:

bundle install

Check if faker installed correctly:

gem list | grep -i faker       (Expected output, version can change → faker (1.9.1) )

 

Now we'll create a sample model, I'll call it contact. You can name it whatever you'd want:

rails generate scaffold contacts first_name:string last_name:string email:string phone_number:string company:string

rails db:migrate

After migrating, your output should look like this:


A table should now have been created, you can find it in the schema file in the db map of your project.

nano db/schema.rb

It should look roughly like this:

If the file looks correct, we are finally ready to actually use faker.

 

How to use Faker

Now we have created a database table, we are finally ready to generate that test data. 

in the same db folder, open the seed.rb file:

nano db/seeds.rb

In this file we can create records to go into our newly created contacts database. Here we'll make use of Faker's ability to generate using the following syntax:

require 'faker' 
include Faker 
20.times do 
  Contacts.create( 
                   first_name: Faker::Name.first_name, 
                   last_name: Faker::Name.last_name, 
                   email: Faker::Internet.email, 
                   phone_number: Faker::PhoneNumber.phone_number, 
                   company: Faker::Company.name 
  ) 
end

!-Notice that the first letter of the database's name is capitalized-!

Faker has a lot of these random data generating placeholders, these are just a few. For an overview of available methodes go to Faker's Github to find more.

 

Now you've written the method to seed your database, run it:

rails db:seed

Start the server, and check out the result in your browser:

rails server

Your_IP_Address:3000/contacts


As you can see, a database full of automatically generated records!

 

Thank you for reading this tutorial!

Comments

Comments are turned off.