Skip to content

Setup CentOS 6, Ruby, Apache Passenger for deployment

Taras Danylak edited this page Dec 18, 2013 · 15 revisions

Installing Ruby 1.9.3 from source

When deploying Rails apps in general and Papyrus in particular, some people may suggest using RVM to install ruby on your server. Multiple reasons are given for this, one of the most popular being the ability to have multiple version or Ruby on the server.

In my opinion using RVM on production server is not a good idea. You will usually have one app running on a server, and that app will be tied to a specific Ruby version. Upgrading the Ruby version should be a controlled process and there is no better way to do it then by installing it using Yum or from source. Use RVM for development, install just one version of Ruby for production.


Install prerequisites

You have to install some libraries to help with Ruby source compilation. First you need to yum install some development packages.

YUM Packages

$ yum install -y gcc gcc-c++ patch readline readline-devel zlib zlib-devel openssl-devel make bzip2 autoconf automake libtool bison httpd-devel apr-devel apr-util-devel sqlite-devel curl-devel

You may already have some of these installed. The command will install the rest of them.

LibYAML

Libyaml is required by ruby's yaml implementation, so we need to compile and install it from source. If you can get the precomiled package, you can skip this part.

$ wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
$ tar xzvf yaml-0.1.4.tar.gz
$ cd yaml-0.1.4
$ ./configure --prefix=/usr/local
$ make
$ make install

After you have downloaded and installed lib yaml it's time to install ruby.

Install Ruby

Download the latest 1.9.3 source package from ruby-lang.org. Extract it, compile it and install it.

$ wget http://cache.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p484.tar.gz
$ tar xzf ruby-1.9.3-p484.tar.gz
$ cd ruby-1.9.3-p484
$ ./configure --enable-shared --disable-install-doc
$ make && make install

You should now have Ruby installed for all users. The --disable-install-doc option prevents the make process from generating RDocs for ya. You don't need it on your production system. And you can always get them online.

Test the installation

$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]

You should see the version printed out as shown above.

Install the rails gem

To test if the installation was success, install the rails gem. You can skip the documentation with --no-ri --no-rdoc

$ gem install rails --no-ri --no-rdoc

If the gem and it's dependencies install successfully, you are ready to move on to the next step.


Installing Passenger and Setting up Apache

I will assume that you have the Apache installed on your server as part of the OS installation process. If you don't, please find the documentation via Google and come back here when you have installed Apache.

Setting up passenger

Passenger installation is a two step process. First you have to install the gem and required libraries, then you have to load passenger into Apache.

Install passenger gem first. This will install Passenger version 4.0.x. For documentation refer to Passgener Website

$ gem install passenger --no-ri --no-rdoc

After the gem is install it's time to compile the module and install it:

$ passenger-install-apache2-module
(Read the info on screen and press Enter when ready

This command will install the passenger module for Apache. At the end it will display the configuration instructions.

The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-4.0.27/buildout/apache2/mod_passenger.so
   PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-4.0.27
   PassengerDefaultRuby /usr/local/bin/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

Copy the LoadModule and PassengerRoot, PassgenerDefaultRuby line and paste them in:

$ vi /etc/httpd/conf.d/1-passenger-ruby.conf
(Paste the configuration in that file. This file should be included automatically by Apache)

After you do that, press enter and the Passenger installation will provide you with some instructions on what to do next. In most cases, you will have your Rails app deployed to a domain or a subdomain, i.e. papyrus.your-university.edu. To get this setup follow passenger's instructions.

Additional Passenger configuration options are shown below. For more information, checkout Passenger documentation.

# Server Production Setup
RailsSpawnMethod smart
PassengerStatThrottleRate 120
RailsAutoDetect off

# Increase the number of Rails apps active at the same time
PassengerMaxPoolSize 30

# For production, set this to 0, so that apps are left in memory
PassengerPoolIdleTime 0

If you want to setup Papyrus (or any other Rails App) in a subdirectory, follow these instructions.

Setup Rails App in a subdirectory

Create a file called z-your-rails-app.conf inside /etc/httpd/conf.d

$ vi /etc/httpd/conf.d/z-your-rails-a.conf
(Tip: Prepending z to the file name, will ensure that Apache will load it last, after passenger)

Inside this file, you have to tell Apache and Passenger where to look for your application. This is an example of how Papyrus is setup:

Alias /papyrus /apps/papyrus/public

RailsBaseURI /papyrus
<Directory /apps/papyrus>
	Order allow,deny
	Allow from all
	AllowOverride All
</Directory>


# Basic Auth Example config
 <Location /papyrus/login>

 	AuthType basic
        AuthName "Papyrus"
        AuthBasicProvider dbm
        AuthDBMType SDBM
        AuthDBMUserFile /apps/papyrus_passwd
 	require valid-user
 </Location>

This configuration setups papyrus to be accessible via http:://yourserver/papyrus. It tells Passenger where the application is located and it can be accessed and it sets up the basic auth authentication mechanism.

After you've finished with this setup, you have to setup Papyrus to use Basic Auth Header to authenticate users.

Note: Using Basic Auth is not recommended if you have many users of Papyrus. Something similar LDAP or CAS is a preferred way. It's setup is similar to the Basic Auth setup.

MySQL Database setup

If you are using mysql as your database, you need to install mysql server, client and development libraries for the mysql2 gem to be compiled

$ yum install mysql mysql-server mysql-devel
$ gem install mysql2

If the mysql2 gem is installed without issues you can proceed to setting up a Papyrus instance.