Ruby on Rails under cPanel

From Leo's Notes
Last edited on 1 September 2019, at 06:20.

This guide will get a modern Ruby on Rails application working on a cPanel account without using cPanel's outdated ruby support.

Overview

To get a Ruby on Rails application running, we'll need to get a recent version of ruby installed.

cPanel officially supports ruby version 1.8.7 and rails version 2.3.18. This guide will install ruby 2.2.0 and rails 4.2.5 using RVM.

Setup

Install Ruby using RVM.

As root, install all the prerequsites.

# yum install gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make  bzip2 autoconf automake libtool bison iconv-devel sqlite-devel

As the user running the website, install ruby and rails.

# curl -L get.rvm.io | bash -s stable --ruby
# rvm install 2.2.0
# source ~/.rvm/scripts/rvm
# gem install rails

At this point, you should have ruby and rails installed under your ~/.rvm directory.

# which ruby
/home/net/.rvm/rubies/ruby-2.0.0-p643/bin/ruby
# which rails
/home/net/.rvm/gems/ruby-2.0.0-p643/bin/rails
# ruby --version
ruby 2.0.0p643 (2015-02-25 revision 49749) [x86_64-linux]
# rails --version
Rails 4.2.5

You may need to install additional gems depending on the rails application you're trying to run.

# cd $rails_application
# bundle install

If everything went smoothly, you should now be able to start the rails application.

# cd $rails_application
# rails s

Ensure that your login profile sources the rvm script, or that your paths are set so that running ruby or rails runs the proper binary. If you want to invoke ruby in a shell script, you might need to adjust the PATH environment variable like so:

export PATH=/home/fnet/.rvm/gems/ruby-2.0.0-p643/bin:/home/net/.rvm/gems/ruby-2.0.0-p643@global/bin:/home/net/.rvm/rubies/ruby-2.0.0-p643/bin:/home/net/.rvm/bin:$PATH
export GEM_PATH=/home/net/.rvm/gems/ruby-2.0.0-p643:/home/net/.rvm/gems/ruby-2.0.0-p643@global
export GEM_HOME=/home/net/.rvm/gems/ruby-2.0.0-p643
export RUBY_VERSION=ruby-2.0.0-p643

Website Rewrite

Your rails application should be running on a high port such as 3000. In order for your main domain to serve the rails application on port 80, you will need to create a .htaccess file with the following contents.

RewriteEngine on
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]

If you wish to serve the application in a sub directory, change the RewriteRule accordingly. Eg: To serve the site from /rails:

RewriteEngine on
RewriteRule ^rails\/(.*)$ http://127.0.0.1:3000/$1 [P,L]

Starting the Application Automatically

A quick and dirty solution is to make use of a cronjob to start the website automatically in case the server goes down.

Create a script with the following:

#!/bin/bash

ps ux | grep -v grep | grep -q ruby || screen -S railsapp -dm rails s

Set the script to execute every minute.

Troubleshooting

Installing Rails Hangs

When installing rails on version 2.2.3 of ruby, gem will hang on the line installing ri documentation for rail. Ruby will use up 100% of the CPU while doing nothing. The same thing happens when running gems install rails --no-ri --no-rdoc.

This was fixed by using an older version of ruby (2.0 worked for me}}.

No route matches [GET] "/index.html.var"

When using Apache 2.4 and the solution above, you may get an error on the main page:

No route matches [GET] "/index.html.var"

This was fixed by adding this line to the .htaccess file:

DirectoryIndex disabled