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.
- 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:
bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
If it tells you to install some dependancies, log in as root and do that. In my case it told me to run this:
yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel
Now (back as the ruby user) load RVM:
You should also configure Bash to load RVM automatically at login:
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
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.
rvm install 1.9.2 rvm --default 1.9.2
4. Install Bundler
You’ll also need Bundler installed, so let’s install it now:
gem install bundler
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
gem install passenger passenger-install-apache2-module
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:
LoadModule passenger_module /home/ruby/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.11/ext/apache2/mod_passenger.so PassengerRoot /home/ruby/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.11 PassengerRuby /home/ruby/.rvm/wrappers/ruby-1.9.2-p290/ruby
I recommend also adding this line to the same file:
Now tell cPanel to rebuild the main Apache config file:
cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.bak-modrails /usr/local/cpanel/bin/apache_conf_distiller --update /scripts/rebuildhttpdconf /etc/init.d/httpd restart
6. Make sure all the RVM files are world-readable
As the ruby user again:
chmod ugo+x ~ chmod ugo+rX -R ~/.rvm
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:
bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer) source "$HOME/.rvm/scripts/rvm" echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc rvm install 1.9.2 rvm --default 1.9.2 gem install rails
Creating a new Rails app
Nothing special here:
rails new hello_world
For the following examples I’ll assume your app is installed in
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:
bundle install --deployment
Or if you are using the site for development not production:
bundle install --path vendor/bundle
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
mv ~/public_html ~/public_html.bak ln -s ~/hello_world/public ~/public_html
Alternatively you can put Rails into a subdirectory (or subdomain, add-on domain, etc.):
ln -s ~/hello_world/public ~/public_html/hello-world
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:
echo "export RAILS_ENV=production" >> ~/.bashrc
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.