Setup and Deploy Ruby on Rails on Ubuntu 16.04 or Latest

Setup and Deploy Ruby On Rails on Ubuntu 16.04 or Latest

Since we setup Ubuntu for our development environment, we also want to use it in production. This keeps your application running consistently between development and production.

Install Ruby

The first step is to install some dependencies for Ruby:

1
2
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’re going to be installing Ruby using rvm:

1
2
3
4
5
6
7
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install 2.3.1
rvm use 2.3.1 --default
ruby -v

The last step is to install Bundler

1
gem install bundler

Install Nginx

Phusion is the company that develops Passenger and they recently put out an official Ubuntu package that ships with Nginx and Passenger pre-installed.

We’ll be using that to setup our production server because it’s very easy to setup:

1
2
3
4
5
6
7
8
9
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates

# Add Passenger 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

So now we have Nginx and passenger installed. We can manage the Nginx webserver by using the service command:

1
sudo service nginx start

The service command also provides some other methods such as restart and stop that allow you to easily restart and stop your webserver.

Next, we need to update the Nginx configuration to point Passenger to the version of Ruby that we’re using. You’ll want to open up /etc/nginx/nginx.conf in your favorite editor. I like to use vim, so I’d run this command:

1
sudo vim /etc/nginx/nginx.conf

Find the following lines, and uncomment them:

1
2
3
4
5
6
7
##
# Phusion Passenger config
##
# Uncomment it if you installed passenger or passenger-enterprise
##

include /etc/nginx/passenger.conf;

Once you’ve configured /etc/nginx/nginx.conf , you can run sudo service nginx restart to restart Nginx with the new Passenger configuration.

Install MySQL and PostgreSQL

Setting up your production database is pretty easy. Make sure to keep in mind that you should use a different password for your production databases.

Depending on what database you want to use, follow the steps related to the database:

Install MySQL
All you need to do in order to install MySQL is to run the following command:

1
sudo apt-get install mysql-server mysql-client libmysqlclient-dev

You can use the root user and password set during installation for your database or you can add a new user to MySQL.

Install PostgreSQL
We can install PostgreSQL like so:

1
sudo apt-get install postgresql postgresql-contrib libpq-dev

Next we need to setup the postgres user:

1
2
3
sudo su - postgres
createuser --pwprompt
exit

The password you type in here will be the one to put in your my_app/current/config/database.yml later when you deploy your app for the first time.

Adding The Nginx Host

In order to get Nginx to respond with the Rails app, we need to modify it’s sites-enabled.

Open up /etc/nginx/sites-enabled/default in your text editor and we will replace the file’s contents with the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;

  server_name mydomain.com;
  passenger_enabled on;
  rails_env    production;
  root         /home/deploy/myapp/public;

  # redirect server error pages to the static page /50x.html
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   html;
  }
}

This is our Nginx configuration for a server listening on port 80. You need to change the server_name values to match the domain you want to use and in root replace “myapp” with the name of your application.

Connect The Database

The file config/database.yml needs to be updated for the production database server username, password, and host. You can set host to “localhost” and you will have to create a database on the server with the same name by using command:

1
2
rake db:create RAILS_ENV=production
rake db:migrate RAILS_ENV=production

Then run command below to minify or compress JavaScript and CSS assets:

1
rake assets:precompile RAILS_ENV=production

Setup Secret Key Base

Go to your rails app directory and run command below to generate secret key base:

1
rake secret RAILS_ENV=production

Go to /yourapp/config/secrets.yml and set production secret_key_base. Then reload Nginx using command line: sudo service nginx reload

So far so good, That’s it!!! See ya!!! :)