Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Apache with mod_ruby

Tags:

ruby

apache

I really want to run some ruby code on Apache server. I've got libapache2-mod-ruby and libapache-ruby1.8 installed (complete list of what is installed is here). What do I do now to make it run (here`s an example)?

like image 709
George Avatar asked Oct 13 '09 11:10

George


2 Answers

Here's a quick howto to get mod_ruby up and running. A short summary of the steps I used on Ubuntu 12.04 is replicated here:

Install mod_ruby for apache:

sudo apt-get install libapache2-mod-ruby

To enable mod_ruby for all files under /var/www, edit your apache config file, i.e. /etc/apache2/sites-enabled/000-default

I had to add +ExecCGI to Options, and add the section pertaining to Ruby:

<Directory /var/www/>
    Options Indexes +ExecCGI

    # Ruby with mod_ruby
    RubyRequire apache/ruby-run
    <Files *.rb>
      SetHandler ruby-object
      RubyHandler Apache::RubyRun.instance
      AddType text/html rb
    </Files>
</Directory>

The main drawback that I see is that I can't seem to find a way to set the mime/content type from within the script. You have to set it globally for all ruby scripts with the AddType text/html rb directive.

Finally, you need a test script in /var/www and it must be executable. For example, I have /var/www/test.rb:

#!/usr/bin/ruby

puts "Hello World!<br><pre>"

ENV.each { |k,v|
  puts "#{k}=#{v}"
}

puts "</pre>"

Don't forget to make it executable:

chmod a+x /var/www/test.rb

And this outputs:

Hello World!
HTTP_HOST=10.0.1.3
HTTP_CONNECTION=keep-alive
HTTP_CACHE_CONTROL=max-age=0
HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
HTTP_USER_AGENT=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
HTTP_ACCEPT_ENCODING=gzip,deflate,sdch
HTTP_ACCEPT_LANGUAGE=en-US,en;q=0.8
HTTP_COOKIE=__test=1;
PATH=/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin
SERVER_SIGNATURE=
Apache/2.2.22 (Ubuntu) Server at 10.0.1.3 Port 80

SERVER_SOFTWARE=Apache/2.2.22 (Ubuntu)
SERVER_NAME=10.0.1.3
SERVER_ADDR=10.0.1.3
SERVER_PORT=80
REMOTE_ADDR=10.0.1.3
DOCUMENT_ROOT=/var/www
SERVER_ADMIN=webmaster@localhost
SCRIPT_FILENAME=/var/www/test.rb
REMOTE_PORT=38188
SERVER_PROTOCOL=HTTP/1.1
REQUEST_METHOD=GET
REQUEST_URI=/test.rb
SCRIPT_NAME=/test.rb
MOD_RUBY=mod_ruby/1.2.6
GATEWAY_INTERFACE=CGI-Ruby/1.1

I've run an Apache benchmark on mod_ruby vs. simple Ruby as CGI (which spins up the Ruby interpreter every request) and mod_ruby was about 8X faster and on par with PHP performance.

like image 173
Jeff Ward Avatar answered Sep 18 '22 17:09

Jeff Ward


mod-ruby is not really the preferred way to go in the ruby community.

The easiest way for you to start it to install passenger (trough gem) and configure apache to use it.

It is really easy to do,

you can follow the official tutorial here: http://www.modrails.com/install.html

like image 33
Aurélien Bottazini Avatar answered Sep 20 '22 17:09

Aurélien Bottazini