Saturday 30 May 2015

With the Ruby developer community continuing to grow, we often get requests for Ruby support from providers wanting to be able to offer their customers the ability to run Rails applications.  We do not recommend using the current Ruby on Rails feature provided by cPanel, as it relies on the CentOS-provided version of Ruby as well as Mongrel 1, both of which are extremely outdated. While cPanel has indicated they have plans to implement Phusion Passenger-based RoR support, there has been little indication this project is still moving along.

We want to reiterate that a server running cPanel is probably not the best production platform for a production RoR application to be housed, but it’s certainly possible and sustainable.  And for some people, it may be their only option.

These instructions have been adapted from Digital Ocean’s instructions for Rails deployment on a bare CentOS 6 box.  This guide also assumes the following is true on your server:

1) You are on a cPanel server (if not, please refer to Digital Ocean’s guide linked above)
2) You have used EasyApache to install Apache 2.2 or Apache 2.4. To confirm this:

root@server [~]# httpd -v
 Server version: Apache/2.4.12 (Unix)
 Server built: May 27 2015 19:16:47
 Cpanel::Easy::Apache v3.30.0 rev9999
3) Your server has at least 1GB of memory, though 1.5GB is preferred for the installation itself
4) You have root-level access

Install ruby


Some guides will advise simply using Yum to install Ruby, however, we do not recommend that since CentOS uses a very old version. Use RVM (Ruby Version Manager) instead:

gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -L get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm requirements
At the time of this writing, 2.2.2 is the latest Ruby version. You might want to go to the Ruby website to see what the most recent version is.  Use that version in the below command:

rvm install 2.2.3
Configure 2.2.2 to be the default Ruby version:

rvm use 2.2.3 --default

Now, install Rails:

gem install rails

Confirm Ruby and Rails are installed:

root@server [~]# ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]root@server [~]# rails -v
Rails 4.2.4

Install Phusion Passenger


gem install passenger
yum -y install curl-devel sqlite-devel
passenger-install-apache2-module

If you get an unclear error on the last step, the most likely cause is running out of memory. While 1GB is recommended, we’ve seen that it often takes up to 1.5GB or more of RAM for the modules to compile.

Now we need to add this to our Apache configuration. On cPanel servers, this is the main point of difference from non-cPanel servers since you cannot directly edit certain configurations in httpd.conf. So you’ll need to create an include. In a text editor, create the following file:

/usr/local/apache/conf/passenger.conf

Inside of this file, add the following lines:

LoadModule passenger_module /usr/local/rvm/gems/ruby-2.2.3/gems/passenger-5.0.24/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/rvm/gems/ruby-2.2.3/gems/passenger-5.0.24
PassengerDefaultRuby /usr/local/rvm/wrappers/ruby-2.2.3/ruby

(There are three lines here – the formatting on our website may alter how it appears)

With the above configuration, you’ll want to make sure you’re using the correct paths based on the versions of Ruby an Passenger you installed.

Now open this file:

/usr/local/apache/conf/includes/pre_main_global.conf

And add the following line:

Include “/usr/local/apache/conf/passenger.conf”

Then rebuild the configuration and restart Apache:

/scripts/rebuildhttpdconf
 service httpd restart

Testing


Create a new cPanel account or use an existing one. Go into that user’s public_html:

cd /home/rubyapp/public_html
 rails new testapp
 cd testapp
Open GemFile and add this line before ‘end’:

gem ‘therubyracer’

Run:

bundle install
 rake db:migrate

From here, and in general, you’ll need to alter the domain’s document root to point to the ‘public’ folder of the application. On cPanel servers, you do this by editing /var/cpanel/userdata/$user/domain.

You’ll also need to create an include for the application environment:

mkdir /usr/local/apache/conf/userdata/std/2_4/$user/$domain/

(Hint: if you look at the Virtual Host for the domain in httpd.conf, you’ll see the path for the include you need to create)

Update: A reader commented on the following script that helps generate includes for this:

https://github.com/ramsy1980/phusion_passenger_script

Create a file in this folder called rails.conf with the following:


RackEnv development
<Directory /home/$user/public_html/testapp/public>
Options -MultiViews
</Directory>

Make sure the domain’s document root is pointed to the same folder.  For an addon or subdomain you can specify the directory when it is created in cPanel, or otherwise edit it in /var/cpanel/userdata/$user/$domain.

Then comment out the include line for this virtual host in httpd.conf and run the following commands:

/scripts/rebuildhttpdconf
 service httpd restart

You should be able to now go to the domain you set up and see the “Welcome aboard” message in your browser, indicating that Ruby and Passenger are working.

Facebook

Popular Posts

Blog Archive

Total Pageviews