How to make a quick deploy of a Ruby on Rails application in a DigitalOcean server


Enrique Vargas

@kike77

linkedin

23 de Diciembre de 2015

Nothing more fascinating than to deploy a Ruby on Rails application within minutes , this is something that the company digitalocean offers a low price and without any complications , this article explains in detail the installation of a RoR application with the Droplet more economic.

I must admit I've been working a long time with Amazon Web Services and have ignored a little the world of VPS, but obviously not all of our customers are willing to pay such high rates (after the year), plus not all applications require a distributed and scalable architecture. Therefore, DigitalOcean gives us a perfect hybrid platform between a classic VPS and a cloud computing service payed by consumption.

 

When I started working with these virtual servers called Droplets I had little hope that the economic ones might effectively support the workload, but to my surprise those exceeded my expectations and I have become an immediate fan of this company.

 

Now we get into the technical side that surely you are interested in. The technology that I most enjoy working is Ruby, coupled with the framework Ruby on Rails, why is my favorite?, it is a long topic that surely I could dwell on one or more articles, so let’s go to what we want; make a deployment (deploy) of our application faster in the most economical Droplet of DigitalOcean.

 

DigitalOcean has a service called 1-click application. This service let us create a Droplet with an atmosphere made specific to the technology that we need to work, in our case it is a Droplet with Ruby on Rails 4+, Nginx, and PostgreSQL, this latter recently added and that eliminates the classic MySQL of the recipe, in my opinion; the best they could do. PostgreSQL vs. MySQL is another issue that surely arouse much controversy.

 

Create the Droplet is easy, by just putting the name, size (in our case $5), region and select the type of application (Ruby on Rails) we will obtain in less than a minute our Rails server.

 

Instalación Ruby on Rails en DigitalOcean     Deploy Ruby on Rails en DigitalOcean

Instalacion ruby on rails en DigitalOceandeploy RoR en DigitalOcean

 

When you finish creating, it gives us an IP address with a Ruby on Rails application working perfectly, easiest impossible.

 

Now it's time to check our email where DigitalOcean sent us the data to make a SSH to our server, something like this:

 


  Your new droplet has been created!
  You can access it using the following credentials:
  IP Address: 104.131.38.121
  Username: root
  Password: hlbjnspvzodd

 

We enter to our SSH session and the first thing it will ask us is to change the password, then it will give us a welcome message together with the connection data we need to install our application. The first thing we will see is the FTP.

 


  You can use the following SFTP credentials to upload your webpages (using FileZilla/WinSCP/Rsync):
  Host: 104.131.38.121
  User: rails
  Pass: hlbjnspasdasdvzodd

 

Next, we find the credentials of the database installed:

 


  You can use the following Postgress database credentials:
    * User: rails
    * Pass: KHSYSRSOYcGT


 

By connecting via FTP will lead us to the project path (/home/rails/rails_project). There we will supplant all files for our application, it is worth noting that we do this assuming that you know Git and that you are keeping the code in some repository (GitHub, BitBucket, etc), we neither wanted to include the deployment with capristrano that we consider the best way to make deployments based on our Git repository. This will be deepen in a future article.

 

Now to replace the files of our project we have to keep in mind that we need to change certain details of our configuration that DigitalOcean places as environment variable, these data can be seen in /etc/default/unicorn

 

What we must change in our application:

 


  rails/config/secrets.yml
	  production:
       secret_key_base: <%= ENV['SECRET_KEY_BASE'] %>

  rails/config/database.yml
	  production:
       <<: *default
       database: app_production
       username: rails
       password: <%= ENV['APP_DATABASE_PASSWORD'] %>


 

Before we start, we need to update the server libraries

 


    apt-get update

 

Then something important, for applications that use image management, for example CarrierWave (Ruby Gem) we must install ImageMagick.

 


  sudo apt-get install imagemagick libmagickwand-dev

 

 

Now the next step is optional and is about using the Linux swap, I do that because many times I use Gems that when installed, consume a lot of memory, and nothing worse than seeing the hateful message of lack of memory, obviously, this we wouldn’t have to do it if we had a more powerful server.

 


  first step:
    sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k
	
  Second step:
    sudo mkswap /swapfile

  the result must be something like this:
    Setting up swapspace version 1, size = 262140 KiB no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb

  to Activate
    sudo swapon /swapfile

  now set each time you restart your server must soloists do this:
   Abrir este archivo
    sudo nano /etc/fstab

  and add this line
    /swapfile       none    swap    sw      0       0

 

Since we have done with the operating system, it's time to our application server, remember that the server is running, so it is a good idea to turn it off for the moment.

 

  service unicorn stop 

 

Before running the Bundle we must add the environment variable data base (APP_DATABASE_PASSWORD) to create structure and do migrate at any time, this is in /etc/default/unicorn at the end of the file, just copy and add it as session variable. We will do it with the user we are connected (root) that was sent to us by DigitalOcean, in order to make it faster but for safety reasons you should use a different user.

 

  Copy the environment variable database file as follows :
    vi /etc/default/unicorn 
  
  And run
    export APP_DATABASE_PASSWORD=PASSWORD-FROM-FILE

  And then we paste in the end of the file .profile for the last time we are connected
    vi ~/.profile


 

Then we start to upgrade and install the Gems of our project

 

  cd /home/rails/rails_project
  bundle update

(Review) Now you should change PostgreSQL configuration file, the reason? well, we don't want to see this awful error "Peer authentication failed for user"

Modified the following file: /etc/postgresql/9.3/main/pg_hba.conf

local   all             all                                     peer

You must change "peer" with "trust"

Now you just have to reboot the server, you can do from the console with this command (sudo shutdown -r now) or you can do it directly from in your digitalocean  session, within your Droplet go to the Power option and then you click on the option "Power Cycle"

 

We create the database and project structures

 


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

 

Precompile all assets


  RAILS_ENV=production rake assets:precompile

 

If we have no problems, we must be prepared to start the Unicorn server applications, before this if we work with Devise remember that the file rails/config/initializers/devise.rb has a config.secret_key that we must configure with our environment variables.

 


  service unicorn start

Now by going to our IP we must see our deployed application.

 

The log file of Unicorn can be see it by the following route

/var/log/unicorn/unicorn.log

 

I hope this little guide has helped, if you have any questions or suggestions feel free to email me.