How To Deploy a Rails App With Passenger and Nginx On Digital Ocean Part 2 — Adding Users, Ruby and Rails

Previously, we created a Digital Ocean account and an SSH key to have a secure connection. If you have not done so already, go back to the first part and do so.

Create Droplet

Next we are going to create a droplet. To do so, click on Create Droplet, which will create a virtual machine on Digital Ocean for you.

Once you click on create Droplet you will find a page to: Enter the name of the Droplet Choose the size of the Droplet Select the Region where the actual server will be. The closer to you the faster your updates will apply because the distance is shorter between you and the server Any available settings for that server You should choose at the very least, IPv6 to future poof your site and enable backups to provide loss of any data in an emergency Select the type of image for your server which is the operating system and if you want to use a application. Choose Ubuntu 14.10 x64, since Postgres 9.4 can only run on this operating system Finally, select the SSH key we created in the Initial Setup

Click on Create Droplet. This will now take a minute or two to create the Droplet for you. Once done, you will be taken to the control panel for that Droplet.

Add Users

Before we can begin to install Ruby, we need to take care of some administration duties. Mainly creating a user who is not the root user because a root user has too much access to more sensitive parts of the operating system.

Step One — Create a User

Log into your Droplet that you created from the last part and enter this command

$ adduser newuser

adduser command will ask you to enter in a password and confirmation password. It will also ask for additional information. This is optional and you can just skip over it.

adduser command will now enter in the user to the operating system and create all of the folders for the new user.

Step Two — Add Grant New User Sudo Privileges

As the root user, enter this command to access the sudoers file which keeps track of which users of the Droplet are using the Sudo command.

$ visudo

Search for root ALL=(ALL:ALL) ALL in visudo

Creating a new line under root ALL=(ALL:ALL) ALL and add in this command

newuser ALL=(ALL:ALL) ALL

This will give newuser administrative access for commands when you use sudo in front of them. Use with caution though as this command is very powerful and could make your Droplet unstable if used incorrectly.

Step Three — Add SSH Keys To New User

Change user to the newuser since we need to access their home directory

$ su newuser $ password: [Enter in newuser password]

Then create a hidden folder called .ssh

$ mkdir .ssh

Exit out of ssh by typing exit twice. Once for newuser and another one for the root user.

Type in this command to copy your SSH public key to the newuser

$ cat ~/.ssh/ | ssh [name of new user name]@[your Droplet ip address] “cat >> ~/.ssh/authorized_keys”

You will need to enter in the password of the newuser in order to add in the SSH public key into that account. Once done, use the command ssh to go into the Droplet as the new user. You should not have to enter in a password.

Installing rbenv

Finally, after we have configured the access rights to the Droplet, it is time to install rbnenv which will be used to control the different version of Ruby on your Droplet. This can help if you are going to run multiple sites on this Droplet. While I will be installing rbenv you can just install Ruby directly. If the Droplet is going to only be used for one website, take a look at this article’s step 4 for the instructions.

Step One — Update apt-get

Before we can begin, we should explain what is an apt-get. apt is the package manager of Linux which Ubuntu is based on. A package is a list of files that are archived and thus make it easier to transmit over the internet. Keeping that in mind, a Package Manager is able to download those packages and install them as applications onto your system. It also can keep track of them which means they can be updated, uninstalled or you can search other packages you may want to install later.

Because apt package list is always being updated, it is best to use the command

$ sudo apt-get update

To update the list and versions of the different packages on your system.

Step Two — Install the rbenv and Ruby dependencies

This will install any Ruby dependencies that are needed, as well as Git, which is a version control for your code which we will talk about later. Git will allow you to get rbenv repo from Github, a poplar repository of open source projects using Git.

$ 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

Now that the Ruby dependencies have been installed, enter in these commands to install rbenv and have it run when you first log in.

$ cd $ git clone git:// .rbenv $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile $ exec $SHELL

$ git clone git:// ~/.rbenv/plugins/ruby-build $ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile $ exec $SHELL

This will install rbenv to your home directory and set the appropriate environment variables that will be used, allowing rbenv to use the active version of Ruby.

Installing Ruby

Ruby is an open source programming language that is dynamic and relies heavily on Object-Orented design pattern. Everything in Ruby is an object. Allowing anything to have a basic function that it could use at any time or even override those functions and adding your own custom ones.

To install Ruby, you will need to use rbenv. As of this writing, the current version of Ruby is 2.2.2. Go to the Ruby Official site to know what is the latest version or if you want to use another version of Ruby, then you may replace my version with your own.

Type in those commands which will install Ruby and make that version the default globally. This may take a while depending on your connection.

$ rbenv install 2.2.2 $ rbenv global 2.2.2

We should verify that the version of Ruby. Type in this command

$ ruby -v

If the version is 2.2.2, then we are good to go. Next, we should stop gems from installing documentation on our Droplet, since this takes up space and I am hoping that you are not developing on the Droplet but using it as your production server.

$ echo "gem: --no-document" > ~/.gemrc

Gems are similar to packages as mentioned before however, they are meant for to be use in Ruby to add functionality and allow development of Ruby application easier.

We should also install bundle gem. This is similar to apt which keeps track of gems, in Rails.

$ gem install bundler

Installing Rails

Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pages. It encourages and facilitates the use of web standards such as XML or JSON for data transfer, and HTML, CSS and JavaScript for display and user interfacing, according to Wikipedia.

To install Rails, use this command

$ gem install rails

To verify that the correct versions of Rails has been installed, type in this command:

$ rails -v

You should see the latest version of Rails

Install Javascript Runtime

Because a few Rails features, such as the Asset Pipeline, depend on a Javascript runtime, we will need to install Node.js.

Add the Node.js PPA to apt-get:

$ sudo add-apt-repository ppa:chris-lea/node.js

Update apt-get and install the Node.js package:

$ sudo apt-get update $ sudo apt-get install nodejs

Conclusion for the Second Part

Our Droplet is taking shape. We created a Droplet, added in the SSH Keys to secure a connection between your computer and the Droplet, installed rbenv, Ruby and Rails.

Next time we will be explaining Git and how to bring over your code from Development into the Droplet, installing Postgres, Passenger and Nginx.


The articles that were used in the creation of this post:

Ruby (Programming Language) - Wikipedia Ruby on Rails - Wikipedia Initial Server Setup with Ubuntu 14.04 How To Add and Delete Users on an Ubuntu 14.04 VPS How To Install Ruby on Rails with rbenv on Ubuntu 14.04