Fresh Blurbs

Installing Ruby On Rails 3 on CentOS with Nginx

This tutorial assumes that you are working with the local account on CentOS 5.x and sudo into root as necessary.

Configuring Yum

Yum is a popular package manager for CentOS and RedHat Linux distributions. Unfortunately, standard Yum repositories carry significantly outdated packages. Fortunately, there are third-party Yum repositories with more updated LAMP packages which we can use. One such repository is provided and hosted by RackSpace itself. Let's set it up:

on a 64-bit CentOS 5.x, you need to run:

sudo rpm -Uvh 
sudo rpm -Uvh 

On 32-bit CentOS 5.x the commands would be:

sudo rpm 
sudo rpm 

For more information about setting up Rackspace's IUS Community repository, please refer to the following blog post: Reliable Yum Repo for Easy Upgrades to the Latest Packages, on RedHat and CentOS

Setting Up Packages

sudo yum install openssl-devel zlib-devel gcc gcc-c++ make autoconf  \
         readline-devel curl-devel expat-devel gettext-devel patch
sudo yum install libjpeg libpng telnet libxslt freetype freetype-devel
sudo yum install mysql51-server mysql51-devel

Erase any possible existing installations:

sudo yum erase ruby ruby-libs ruby-mode \
       ruby-rdoc ruby-irb ruby-ri ruby-docs

Install Ruby 1.8.7 (currently recommended version for RoR development) from source:

cd /usr/local/src/
sudo wget
sudo tar xzvf ruby-1.8.7-p330.tar.gz
cd ruby-1.8.7-p330
sudo ./configure --enable-shared 
sudo make
sudo make install
ruby -v
>  ruby 1.8.7 (2010-12-23 patchlevel 330) [x86_64-linux]

Install Gems:

sudo wget
sudo tar xzvf rubygems-1.4.2.tgz 
cd rubygems-1.4.2
sudo ruby setup.rb 
gem --version
> 1.4.2
sudo gem install rubygems-update
sudo update_rubygems

Install Rake and Rails:

sudo gem install rake
sudo gem install rails

If you want/need to install SQLite and SQLite support for Ruby:

cd /usr/local/src/
sudo wget
sudo tar xzvf sqlite-autoconf-3070400.tar.gz 
cd sqlite-autoconf-3070400
sudo ./configure
sudo make
sudo make install
sudo gem install sqlite3-ruby
sqlite3 -version
> 3.3.6

Install the latest Nginx from source:

sudo yum install pcre-devel
sudo wget
sudo tar xzvf nginx-0.8.54.tar.gz 
cd nginx-0.8.54
sudo ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module \
         --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module \ 
         --with-http_stub_status_module --with-http_realip_module
sudo make
sudo make install
sudo ln -s /usr/local/nginx/conf /etc/nginx

To make sure the newly installed Nginx properly restarts on server restarts, please follow instructions in the very well-written SliceHost tutorial for installing Nginx startup scripts

Install Passenger for NginX:

sudo gem install passenger

Run step-by-step passenger for NginX installer by typing:

sudo passenger-install-nginx-module

Important: Please note that Nginx does not support dynamic modules. Adding a module to Nginx means re-compiling it. passenger installer will guide you through the steps of recompile. Please make sure you answer the questions in the process similar to what's shown below, otherwise your NginX will not be re-compiled the way it was before passenger "injection":

> 2. No: I want to customize my Nginx installation. (for advanced users)

> Where is your Nginx source code located?
> Please specify the directory: /usr/local/src/nginx-0.8.54

> Where do you want to install Nginx to?
> Please specify a prefix directory [/opt/nginx]: /usr/local/nginx
> Extra arguments to pass to configure script: --sbin-path=/usr/local/sbin \ 
--without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module \
 --with-http_stub_status_module --with-http_realip_module

Configure Firewall

Edit /etc/sysconfig/iptables-config and make sure following variables are set to “yes�:


Open Web ports:

sudo /sbin/iptables -I INPUT -p tcp -m state --state NEW,ESTABLISHED --dport 80 -j ACCEPT
sudo /sbin/iptables -I INPUT -p tcp -m state --state NEW,ESTABLISHED --dport 443 -j ACCEPT

Restart iptables with:

sudo /sbin/service iptables stop
sudo /sbin/service iptables start


Install MongoDB, following the steps from my previous blog post.

Install MySQL and MongoDB gems:

sudo gem install mysql -- --with-mysql-config=/usr/bin/mysql_config
sudo gem install mongo bson bson_ext

Configure Bootup Scripts

To make sure essential services come up when/if your server is restarted, you need to run following commands:

sudo chkconfig --add nginx
sudo chkconfig --add mysqld
sudo chkconfig --add mongod

sudo chkconfig --level 2345 nginx on
sudo chkconfig --level 2345 mysqld on
sudo chkconfig --level 2345 mongod on

Wiring your first app:

sudo chgrp -R webmaster /opt
mkdir /opt/reailsapps
cd /opt/railsapps
rails new firstapp

Edit file: /etc/nginx/nginx.conf and insert the following lines at the end of the "http" section (typically: before the very last closing curly brace "}" in the file, unless Nginx changes the default config file):

  passenger_root /usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.2;
  passenger_ruby /usr/local/bin/ruby;

  include sites/*.conf;

create /etc/nginx/sites/firstapp.conf with the following content:

server {
  listen 80;
  root /opt/railsapps/firstapp/public;   # <--- be sure to add 'public' to the app path!
  passenger_enabled on;
  #rails_env development;
  gzip on;

Reload nginx configuration with:

sudo /etc/init.d/nginx reload (if you have install startup scripts) or
/usr/local/sbin/nginx -s reload

You should see default welcome screen of a rails app.

At that point you have successfully set up the environment.

Further Optimizations

If you are interested in some more advanced scalability and efficiency optimizations you may want to take a look at the: Ruby Enterprise Edition

comments powered by Disqus