Installing RVM, Ruby on Rails, and Passenger on Centos 6

Installing RVM Multi-user on CentOS can be a bit tricky. Here's my documentation - mostly so I remember, but hopefully it helps someone else as well.

A quick note - the people behind RVM are clear that tutorials outside their site are not supported. They're not discouraged, but they won't support them. Pretty reasonable. These directions worked for me, but you should understand what you're doing before following directions on the internet.

My install was for Ruby 1.9.3 (current at the time of writing) on CentOS 6.2 x86_64. I installed on a VMware Fusion virtual machine. I'll be installing this on an EC2 instance in the future - I don't expect much in the way of changes, but will update if needed. I'll also be assuming a minimal install. As this was for a local devel environment I did a few things I wouldn't suggest doing on a production or publicly available site. I want to use Apache and a local MySQL instance, so I'll be installing those as well.

Ok, with that out of the way:

First, as root:

yum update

yum -y groupinstall "Development Tools"

yum -y install ntp

ntpdate pool.ntp.org

yum -y install zlib zlib-devel sqlite-devel httpd mysql-server mysql-devel curl-devel httpd-devel apr-devel apr-util-devel mlocate man libxml2-devel libxslt-devel libffi-devel readline-devel

adduser your_name

passwd your_name

usermod -G wheel your_name

The last step in this section is to run visudo. Uncomment the line %wheel ALL=(ALL) NOPASSWD:ALL towards the bottom of the file.

Now logout and log back in as your_name.

curl -L get.rvm.io |bash-s stable

logout

Now back to root and run

usermod -G wheel,rvm your_name

and log back out.

Now we're getting somewhere. RVM is installed and we're about ready to install ruby. Let's do a couple of sanity checks first.

Log in as your_name.

which rvm

As a multi-site install RVM should have been installed to /usr/local/rvm.

Next,

type rvm |head -1

should return

rvm is a function.

OK, we've successfully installed RVM. Now let's install Ruby. We'll include an openssl directive so we can install Passenger in a bit. Still as your_name:

rvm install 1.9.3 --with-openssl-dir=/usr

Now let's set the default version:

rvm use 1.9.3 --default

And finally:

gem install rake

gem install rails

Now rake and rails are installed. Let's install passenger (and a few gems for database connectivity).

gem install mysql2 sqlite3 passenger

OK, now to set up passenger for Apache:

passenger-install-apache2-module

Pay attention to the paths that the install script gives you at the end. Copy them to a text file somewhere - we'll need them in a bit.

Now let's configure Apache.

I like the way Ubuntu lays out the Apache config - especially virtual hosts - so we'll mimic that here.

sudo mkdir /etc/httpd/sites-available

sudo mkdir /etc/httpd/sites-enabled

We'll also need a few virtual host specific folders:

sudo mkdir /var/www/application_name

sudo mkdir /var/log/httpd/application_name

Now that our folder layout is set, let's get to the actual configuration files:

First up, passenger. Let's create a passenger.conf file and paste in the paths we saved previously:

sudo vi /etc/httpd/conf.d/passenger.conf

and populate

LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.3-p0/gems/passenger-3.0.11/ext/apache2/mod_passenger.so

PassengerRoot /usr/local/rvm/gems/ruby-1.9.3-p0/gems/passenger-3.0.11

PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.3-p0/ruby

Save the file. This will load the passenger module when Apache starts.

Next let's set up the virtual hosts:

sudo vi /etc/httpd/sites-available/application_name.conf

<VirtualHost *:80>

ServerAdmin youremail@example.com

DocumentRoot /var/www/application_name/public

ServerName yourservername

ErrorLog /var/log/httpd/application_name/error.log

CustomLog /var/log/httpd/application_name/access.log common

</VirtualHost>

It's important to note we're pointing the DocumentRoot to /public of your application.

Now let's tell Apache where to look for the virtual host definitions:

sudo vi /etc/httpd/conf/http.conf

Towards the bottom add:

Include sites-enabled/*.conf

Save the file.

Two more steps and we're there.

First link the virtual host definition to sites-enabled

sudo ln -s /etc/httpd/sites-available/application_name.conf /etc/httpd/sites-enabled/application_name.conf

Second, we need to suspend SELinux:

sudo setenforce 0

I would absouletly, positively run SELinux on a publicly facing website. Spend some time to learn to configure it - it's not nearly as scary as it looks at first.

Go ahead and deploy your application to /var/www/application_name. Finally, start apache to make sure it runs sudo service httpd start.

That's it. You should be able to visit your site at http://yourservername.