Installing Ruby on Rails on Plesk 12

installing ruby on rails on plesk 12

Plesk 12 comes with a range of pre-built installation wizards for a number of the most popular PHP CMS’ on the market, such as WordPress, Drupal and Joomla, but not for the major PHP frameworks, such as Zend Framework 2, Ruby on Rails, FuelPHP and Symfony.

This is a little unfortunate, but it’s no deal breaker; and to be fair, as they’re frameworks, not complete applications, it makes sense. Given the flexibility of Plesk 12, as administrators, developers and devops, we’re still able to do quite a bit manually.

In today’s tutorial, we’re going to guide you through the process of setting up and deploying a basic Ruby on Rails 4 application on Plesk 12. By the time we’re done, you’ll see the site as in the screenshot below.

the running ruby on rails application

Before we get started, I should say that it won’t be a shining example of world-beating application, but it will have the core Ruby on Rails 4 framework installed, ready and configured with a connection to a MySQL backend database.

What is Ruby on Rails (RoR)

In a nutshell, according to David Heinemeier Hansson, the creator and lead developer of Ruby on Rails:

Most frameworks and languages were focused on neither fun nor productivity. The forging ground was quick ‘n’ dirty with PHP or slow ‘n’ clean with Java. There was room for something quick ‘n’ clean in the middle.

So with that in mind, Ruby on Rails is an open source, web application framework, that’s “optimized for programmer happiness and sustainable productivity. It let’s you write beautiful code by favouring convention over configuration“.

Like Zend Framework 2, Laravel and FuelPHP, it’s a full stack framework which allows creating pages and applications which talk to or query the database, and render templates out of the box.

Prerequisites

To complete today’s tutorial, you’re only going to need a few prerequisites. These are:

  • Curl
  • Linux build tools (such as make and configure)
  • Root access (or user with sufficient privileges or who’s in the sudoers list)

Getting Setup

Right, so what do we need to do to set it up? Thanks to the modern UI, and intelligent layout available in Plesk 12, it’s a real no brainer, requiring only a few steps to have it live, which are as follows:

  • Create a Sub-Domain
  • Setup SSH Access
  • Install Ruby on Rails
  • Configure Ruby on Rails
  • View The Application

Let’s work through the process now.

Create a Sub-Domain

When you first login to your account, click the Domains in the left-hand side navigation bar, under Hosting Services. This will then show you the list of your websites & domains. To keep this simple, we’re going to create a new sub-domain, specifically under conetix.com.

On the far right of that domain’s row in the table, click Manage Hosting. This will display the domain and sub-domain entries, which you can see in the screenshot below.

plesk 12 add subdomain screenshot

Above the first entry, you’ll see three buttons:

  • “Add New Domain”
  • “Add new Subdomain”
  • “Add New Domain Alias”

Click “Add New Subdomain”, and on the form which appears, enter “ror” for the subdomain name. The Document root field will be pre-populated, based on the Subdomain name you entered. At the end, add in “/ror/public”.

The reason for this is that the command we’ll run to install Ruby on Rails creates a sub-directory called Ruby on Rails and the application’s bootstrap file is located in the public directory, inside it.

It may take a minute or two for the subdomain to be created, based on your server’s specifications. But it shouldn’t take too long.

When it is, you’ll be back on the domains page you started at, with your new domain listed at the bottom of the list. In the entry, you’ll see all the pertinent details, along with links to more, as well as to make changes.

Setup SSH Access

Now we need to check that SSH access is setup properly, so that we can login and run a few command line scripts. If you’re not too familiar with command line scripts, that’s ok.

The one’s I’ve listed here are quite simple and have sufficient documentation, should it be needed.

plesk 12 setup ssh screenshot

At the bottom of the subdomain’s entry, click the tab titled “Show More”, then click the first entry in the exposed list, titled “Web Hosting Access”. On the page that appears, you’ll see four sections.

plesk 12 web hosting access screenshot

In the second section, titled “System user”, make sure that the last option, “Access to the server over SSH”, is set to /bin/bash or /bin/sh and click OK at the bottom.

The reason for this is because these scripts need that environment to run in. sh may work, but bash works best for this example. Now you’re ready to login and begin installing the application.

Note: if you’re not sure of the user’s password, either regenerate it or check with your systems administrator.

Installing Rubygems

Now that the database is installed, we need to install all Ruby on Rails along with all the required dependencies; we’ll start with the first dependency, Rubygems. To do so, use the following command which suits your platform:

Platform Command
Centos/Redhat yum install rubygems
Ubuntu/Debian apt-get install rubygems

Installing Rails

Now that the Rubygems is installed, it’s time to install Rails. To do so, from the command line run the following command:

gem install rails

Potential Errors

Missing Development Packages

You may encounter errors attempting to install it; one of the main ones is not having the necessary development packages or libraries. If so, you’ll likely see an error like this one:

Building native extensions.  This could take a while...
ERROR:  Error installing rails:
    ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.h

If you see this error, first make sure you have both gcc and ruby-devel installed. You can do that use one of the following commands, which best suits your platform:

Platform Command
Centos/Redhat yum -y install gcc ruby-devel sqlite-devel
Ubuntu/Debian apt-get -y install gcc ruby-devel libsqlite3-dev

Incorrect Version of Ruby

The next error you may encounter, is the following, which indicates you need a higher version of Ruby than what’s installed.

ERROR:  Error installing rails:
    activesupport requires Ruby version >= 1.9.3.

If that’s the case, you may find that your distribution’s package manager is already at the latest version. This can be done by running yum info ruby.

If so, don’t worry, you don’t need to install a version of Ruby from source; though you can if you want to. Instead, install the Ruby Version Manager (RVM) which handles the installation, uninstallation and configuration of multiple versions of Ruby on your system for you.

To do so, run the following commands in your terminal or console:

yum groupinstall -y development
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm reload
rvm install 2.1.0

What these commands do is first to ensure that you have all the development packages required to compile the code, then RVM is downloaded and the shell environment is updated to support the package. Finally version 2.1.0 of Ruby is installed.

When that’s done, if you’d like to verify the installation, run the following two commands:

ruby --version
rvm list rubies

After all this prep work, you shouldn’t encounter any further errors. So run gem install rails again, where you should see output like the following, (truncated for readability):

Installing ri documentation for treetop-1.4.15
Parsing documentation for tzinfo-1.2.1
Installing ri documentation for tzinfo-1.2.1
Done installing documentation for actionmailer, actionpack, 
actionview, activemodel, activerecord, activesupport, arel, 
builder, erubis, hike, i18n, mail, mime-types, minitest, multi_json,
polyglot, rack, rack-test, rails, railties, sprockets, sprockets-rails,
thor, thread_safe, tilt, treetop, tzinfo after 479 seconds

Installing Additional Libraries

Now that Rails is installed, we need to install some additional libraries which it will need (just to be sure that they’re available on your system), which they aren’t by default. What we’ll need is a JSON gem and a JavaScript runtime, provided by Node.js. To get them installed, from your terminal, run the following commands:

gem install json -v '1.8.1'
curl -sL https://rpm.nodesource.com/setup | bash -
yum install -y nodejs

That will take just a little while to complete, but will be the last Rails dependency you’ll need to be up and running.

Creating the Ruby on Rails Application

Now that we’ve gotten all the Ruby on Rails dependencies out of the way, let’s create a basic, template, application. Gladly, there’s not a lot to do, just two steps. From the RoR directory, run the following commands. What these do are to firstly generate a stub application, then to precompile the static asset files, such as CSS, JavaScript.

rails new .
bundle exec rake assets:precompile RAILS_ENV=development

If however you’re deploying to production, change development above to production.

Install Unicorn with Apache

Now that we have all of the Ruby-based components installed and ready to go, and our basic Ruby on Rails application created, we need to connect it to the Apache Webserver. To do so, we’re going to configure Apache as a reverse proxy. This will have Apache pass all requests to the rails sub-domain to the Ruby server, Unicorn (which we’ll also setup).

What is Unicorn?

According to the official website, Unicorn is:

an HTTP server for Rack applications designed to only serve fast clients on low-latency, high-bandwidth connections and take advantage of features in Unix/Unix-like kernels. Slow clients should only be served by placing a reverse proxy capable of fully buffering both the the request and response in between Unicorn and slow clients.

To install it, from your terminal, run the following command:

gem install unicorn

Configuring Apache as a Reverse Proxy

Thanks to the smooth Plesk 12 interface, configuring Apache as a reverse proxy is quite straight-forward. Under the hosting settings for the sub-domain, click Show More at the bottom.

Then click the second option, in the middle column, labeled Web Server Settings, which you see in the screenshot below.

plesk 12 web server settings

Then, when the page’s loaded, under Additional directives for HTTP, add the following configuration directives:

<Proxy balancer://unicornservers>
    BalancerMember https://127.0.0.1:5001
</Proxy>

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://unicornservers%{REQUEST_URI} [P,QSA,L]

ProxyPass / balancer://unicornservers/
ProxyPassReverse / balancer://unicornservers/
ProxyPreserveHost on

<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>

What this does is to pass all traffic through to the Unicorn server listening on port 5001, for requests to the sub-domain.

As Plesk restarts Apache when you make any configuration changes, there’s no need to seek out a restart option or to do it from the command line.

Start Unicorn

To start Unicorn, go to the project directory, which you’ll find at/var/www/vhosts/conetix.com/ror.conetix.com/rails.app and run the following command:

unicorn_rails -p 5001 -E development -D

This will start the server in development mode. If you want to run it in production, change development to production and set the secret_key_base for the production configuration in config/secrets.yml.

Set Unicorn to Reload if Terminated

Finally, there’s one last step. Sometimes Unicorn may crash, which can occur for a variety of reasons; it also may just need to be restarted.

So we need to ensure that it will automatically be restarted. To do that, open `/etc/rc.local in your editor of choice and add in the following code snippet:

ROR_MODE=development
ROR_DIR=/var/www/vhosts/conetix.com/ror.conetix.com/
cd $ROR_DIR && unicorn_rails -p 5001 -E $ROR_MODE -D

I’ve already added some documentation, but to reiterate, I’ve set to shell variables, one for the runtime mode and the other for the application directory. Change these as suits your configuration.

Then I’ve added the boot script, passing in the configuration variables. Now, if the daemon crashes, Linux will restart it automatically for us.

View the Application

Right, now that it’s installed, let’s have a look at the running app. It’s not special, as there’s no database, logging, or caching server connections. It’s just a one page application, but it works.

the running ruby on rails application

Wrapping up

So there you go. In just a few minutes, we’ve stepped through creating and configuring a sub-domain, to installing a basic Ruby on Rails application, served via a combination of Unicorn and Apache acting as a reverse proxy.

Now it’d be great if it was simpler, such as having an installer for Drupal, Joomla and WordPress. But it’s a bit hard to compare off the shelf applications, such as these, to frameworks which allow for creating nearly any kind of application.

What I’m hoping for is to have an installer which lays the foundation for this process, which may just be available in the future.

Till then, share your thoughts in the comments. Has this made it simpler for you to start creating applications with Ruby on Rails (ROR) on Plesk? I hope so. I’d love to hear how you feel.

Links

Back to the Blog

avatar of matthew setter

Matthew Setter

  • Conetix
  • Conetix

Let's Get Started

  • This field is for validation purposes and should be left unchanged.