CentOS 5 still only has ruby 1.8.5 in its repository, while the latest stable release of ruby is 1.9.2. This article describes how to install ruby 1.9 from source, install mod_ruby from source, and configure Apache to execute ruby scripts.
1. Prepare to install ruby by installing the gnu c compiler.
[root@hostname ~]# yum -y install gcc
This brings with it the following dependencies which are automatically resolved by yum:
cpp glibc-devel glibc-headers kernel-headers
2. Download and install ruby
At the time of writing this article the latest stable version is 1.9.2-p180. You can always find the latest version of ruby here: http://www.ruby-lang.org/en/downloads/
[root@hostname ~]# cd /usr/src [root@hostname src]# wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz [root@hostname src]# tar xvfz ruby-1.9.2-p180.tar.gz [root@hostname src]# cd ruby-1.9.2-p180 [root@hostname ruby-1.9.2-p180]# ./configure [root@hostname ruby-1.9.2-p180]# make [root@hostname ruby-1.9.2-p180]# make install [root@hostname ruby-1.9.2-p180]# make clean
Verify Installation
[root@hostname ruby-1.9.2-p180]# which ruby /usr/local/bin/ruby [root@hostname ruby-1.9.2-p180]# ruby -v ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux] [root@hostname ruby-1.9.2-p180]# gem -v 1.3.7
3. Prepare to install mod_ruby
Install the Apache Runtime Environment (apr-devel) and the Apache development tools(httpd-devel).
[root@hostname ~]# yum -y install apr-devel httpd-devel
4. Download and install mod_ruby.
At the time of writing this article the latest stable version of modruby is 1.3.0. You can always find the latest version of mod_ruby here: http://www.modruby.net/en/
[root@hostname ~]# cd /usr/src/ [root@hostname src]# wget http://www.modruby.net/archive/mod_ruby-1.3.0.tar.gz [root@hostname src]# tar xvfz mod_ruby-1.3.0.tar.gz [root@hostname src]# cd mod_ruby-1.3.0 [root@hostname mod_ruby-1.3.0]# ./configure.rb --with-apr-includes=/usr/include/apr-1 [root@hostname mod_ruby-1.3.0]# make [root@hostname mod_ruby-1.3.0]# make install [root@hostname mod_ruby-1.3.0]# make clean
5. Configure Apache and Verify the mod_ruby Installation
Create a file called ruby.conf in the httpd module configuration directory. All files in this directory are automatically loaded.
[root@hostname test_ruby]# cat /etc/httpd/conf.d/ruby.conf LoadModule ruby_module /usr/lib64/httpd/modules/mod_ruby.so AddHandler cgi-script .rb
Restart apache to reload the new configuration.
[root@hostname test_dir]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
If apache starts ok this means mod_ruby was successfully loaded.
6. Test it!
Make a web directory under the webroot for our test files. Create a couple of ruby scripts in there. In this example I have created one executable ruby script, and one erb file that will be loaded by the ruby cgi and rendered.
mkdir /var/www/html/test_ruby
[root@hostname test_ruby]# cat /var/www/html/test_ruby/index_cgi.rb
#!/usr/local/bin/ruby
require 'cgi'
require 'cgi/session'
require 'erb'
cgi = CGI.new
puts cgi.header
#here is variable i prepared here in the controller
test_var = "kentest"
rhtml = ERB.new(File.new("./example.erb.html").read)
rhtml.run(binding)
[root@hostname test_ruby]# cat /var/www/html/test_ruby/example.erb.html
<html>
<body>
Now i will access this variable here in the view
<%= test_var %>
and again
<% puts test_var %>
</body>
</html>
Make the ruby script executable.
[root@hostname test_ruby]# chmod a+x index_cgi.rb
Add the test directory to apache configuration:
[root@hostname test_ruby]# cat /etc/httpd/conf.d/ruby.conf
LoadModule ruby_module /usr/lib64/httpd/modules/mod_ruby.so
AddHandler cgi-script .rb
<Directory /var/www/html/test_ruby>
Options ExecCGI
</Directory>
Restart apache again to reload the new configuration with the new directory.
[root@hostname test_ruby]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
In your browser go to hostname:80/test_ruby/index_cgi.rb
Voila!









