Installing Ruby on Rails 3.1 + Ruby 1.9 on a cPanel 11.30 server

cPanel 11.30 doesn’t support Ruby 1.9 or Ruby on Rails 3.0/3.1, only Ruby 1.8 and Rails 2.3. They are working on supporting Rails 3 in cPanel 11.34, but that could be 12 months away still, so for now we have to install them manually.

To do this you’ll need root access to the server – reseller or normal user access will not do. You also need to be comfortable using the command line and editing configuration files.

Warnings

  • You won’t be able to use the Ruby on Rails section in cPanel – everything will have to be done using the command line instead.
  • If you mess up the Apache configuration you could break your web server – so make sure you know what you’re doing!
  • This is what worked for me – there may be better solutions I’m not aware of.

What we’re going to install

Ruby Version Manager makes it easy to install multiple versions of Ruby into your home directory. We’ll use that to install Ruby 1.9 separately from the cPanel-provided version 1.8.

Phusion Passenger (a.k.a. mod_rails) is an Apache module that lets you run multiple Rails apps under a single Apache instance – no need for backend servers like Mongrel any more.

Install RVM, Ruby & Phusion Passenger

1. Create a new user

First you need to decide which user to install it under. It’s probably best not to do it as root (I don’t even know if it would let you), so I created a new user especially for this. That way I know I won’t accidentally break it.

To do that, go to WebHost Manager (WHM) > Account Functions > Create a New Account. I used the username ruby and the domain name ruby.myservername.com (using my actual server name of course).

2. Install RVM

Now log in as that user via SSH and install RVM:

If it tells you to install some dependancies, log in as root and do that. In my case it told me to run this:

Now (back as the ruby user) load RVM:

You should also configure Bash to load RVM automatically at login:

3. Install Ruby

Now it’s time to install Ruby. I installed version 1.9.2 because I couldn’t get ruby-debug to work with the latest version, 1.9.3.

4. Install Bundler

You’ll also need Bundler installed, so let’s install it now:

This is the only Rails gem you need to install under this user account – the rest can be installed locally for each app.

5. Install Phusion Passenger

Passenger will give you some code to add to your Apache config – make a note of it.

Now log in as root again and add that code to /usr/local/apache/conf/includes/pre_main_global.conf. e.g. My code was:

I recommend also adding this line to the same file:

Now tell cPanel to rebuild the main Apache config file:

6. Make sure all the RVM files are world-readable

As the ruby user again:

That’s it – you’re ready to run Rails! The next section shows how to set up each app.

Using Ruby on Rails

Installing RVM, Ruby and Ruby on Rails

You will need to install RVM, Ruby and Ruby on Rails for each user that will be creating Rails apps. The commands are the same as we used above:

Creating a new Rails app

Nothing special here:

For the following examples I’ll assume your app is installed in ~/hello_world.

Installing Gems to the vendor/ directory

If you run bundle, that will install the Gems to your RVM directory, which won’t be picked up by Passenger. There’s instructions on the RVM website how to make that work, but I wasn’t able to make it work.

A much simpler solution is to install the Gems to the vendor/ directory, where they are picked up automatically:

Or if you are using the site for development not production:

Bundler will remember the path after that, so you can just run bundle install (or even just bundle) as normal.

Pointing a site to Rails

The simplest way to make the new Rails app accessible is to make a symlink to the Rails public/ directory:

Alternatively you can put Rails into a subdirectory (or subdomain, add-on domain, etc.):

Other tips

Using the production environment automatically

You will probably want to use the production environment instead of the development one. To make that the default for all commands, simply set the RAILS_ENV environment variable:

To set it automatically when you log in:

Precompiling the assets

Since this is a production site you will need to precompile the assets:

(Later you should look into setting up Capistrano to do this automatically whenever you deploy changes.)

Restarting the app

The final thing I had to do after making all those changes is restart the app:

This tells Passenger to restart the app next time you visit it, and saves you having to restart Apache completely every time.

Sources