Ruby on Rails under cPanel
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
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
RewriteCond %{HTTP_HOST} ^net\-testing\.cpsc\.leung\.xyz$ [OR]
RewriteCond %{HTTP_HOST} ^www\.net\-testing\.leung\.xyz$
RewriteRule ^(.*)$ "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 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}}.