Configure Apache To Serve Any Ruby Erb File (Like a mod_ruby or mod_erb) « I Am James’ Blog

Over the years there have been lots of cries for a mod_ruby on Apache that allowed users to ftp upload single ruby files to anywhere on their website and have Apache interpret the files and serve the resulting html, just like you can do with php files.  Granted, there has been a project called mod_ruby, but that project has long been unmaintained and plagued with issues.  Whether or not it is a good idea to copy the php method of writing webpages gets discussed at length every other week, so I will not get in to that here.

As it turns out, it’s pretty easy to configure Apache to do this, thanks to cgi and mod_actions.  This guide will show you how to configure Apache, so you can upload any erb file to anywhere on your Apache website and serve it up like a php file.  These erb files are html files embedded with ruby script inside special tags, just like php files are html files with php script embedded in them.

Install Apache

For this I’m using Ubuntu 10.04, the latest LTS release of Ubuntu.  There should be little or no difference doing this on Debian or other versions of Ubuntu.

sudo aptitude install apache2

Install Ruby

You’ll need ruby and an eruby library.  We’re keeping things simple and just installing what is in apt.

sudo aptitude install ruby1.9.1 liberubis-ruby1.9.1

Configure Apache

You just need ScriptAlias, AddHandler, and Action directives to tie everything together.  They can go anywhere in your Apache config files, but here is how I do it.

First, you need to enable the Apache Actions modules.

sudo a2enmod actions

Then put this in /etc/apache2/conf.d/erb-cgi

        AddHandler application/x-httpd-erb .erb        Action application/x-httpd-erb /cgi-bin/erb-cgi.rb

In the default site configuration under /etc/apache2/sites-available/default there is already a ScriptAlias directive, so I just left it there.  Here it is in case you don’t have it in your site config.

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

Now restart apache.

sudo service apache2 restart

The CGI Script

We’ve configured Apache so anytime a file ending with “.erb” is requested, Apache instead executes the erb-cgi.rb script and displays the output.  Lets write this script so it processes and outputs the requested file.

Put the following in /usr/lib/cgi-bin/erb-cgi.rb

#!/usr/bin/env rubyrequire 'erubis'puts "Content-type: text/htmln"# Check that the script is being executed through a redirectif ENV["REQUEST_URI"] =~ /^#{ENV["SCRIPT_NAME"]}.*/  puts "Script can not be executed directly!"else  # Get the file location from the ENV hash, read it, and process it through erubis  puts["PATH_TRANSLATED"])).result(binding())end

Make sure the file is executable by Apache.

sudo chgrp www-data /usr/lib/cgi-bin/erb-cgi.rbsudo chmod ug x /usr/lib/cgi-bin/erb-cgi.rb

Try It Out

You are now ready to upload erb embedded html files to anywhere under the DocumentRoot of your website.  This will handle any file with a .erb extension, however I prefer to use a .html.erb extension to differentiate these from other filetypes embedded with erb, such as .xml.erb.  Note:  this will actually handle erb embedded xml files, but you will have to modify the cgi script to check the file extension and change the content type.

Here is a quick example of an erb embedded html file.  Assuming you are using Ubuntu’s default site with the default DocumentRoot, save this in /var/www/test.html.erb

Hello !  The time is now .

You should now be able to open a browser  and go to http://your_server/test.html.erb and see the results.  You can now upload and serve erb files just like php files!

Feel free to ask any questions or make any suggestions in the comments!

Sources And More Information

For high traffic sites, using fastcgi (fcgid) instead of cgi would eventually become a necessity.  I don’t plan on using this for anything that gets much traffic, though, so I’m not exploring the configuration for fastcgi at the moment.  I also don’t know how this will handle form data through a POST request.  Also keep in mind that this will not allow you upload a Rails application and have it just work through this cgi script. It would probably be pretty easy to modify this to use rack, but running a full Rails app through cgi is a pretty bad idea, and should probably just be done with mod_passenger.

Advertisement –>

Like this:

Be the first to like this post.

Leave a Reply

Your email address will not be published. Required fields are marked *