Posted on August 18th, 2008 in Apache, ruby | No Comments »
Adding Ruby HTML support to your existing MAMP setup is great for experimenting and for building quick single serving apps where a whole Rails setup would be overkill. This is how I added Ruby RHTML page support to my current MAMP setup:
Download and compile eRuby
- Download and unzip http://www.modruby.net/en/index.rbx/eruby/download.html
- Go to the unzipped directory in terminal
- type ./configure.rb
- type make
- type make install
- Copy the generated eruby file to your local cgi-bin
Modify your httpd.conf settings
- add this line to add the rhtml support:
AddType application/x-httpd-eruby .rhtml Action application/x-httpd-eruby /cgi-bin/eruby
- And modify this line so your server will look for index.rhtml pages in directories:
DirectoryIndex index.html index.shtml index.rhtml
Running straight .rb files outside your cgi-bin folder (optional)
You can run any scripts from your cgi-bin if you properly add the #!(she bang) location to your ruby intepreter at the top of your scripts, but you can also make these scrips run outside of your cgi-bin by just adding this line to your httpd.config, just add the .rb extension to the filename:
AddHandler cgi-script .rb
I was doing this for awhile so I could execute scripts through the web but the RHTML method is better. Still, there could be applicable reasons for a person to want to do this. Just keep in mind, that when accessing Ruby scripts directly from a webpage you must at least specify a content-type (puts: “content-type: text/plain\n”) in your header or use the convenient cgi class from the Ruby standard library that does that (and a whole lot more) for you.
Improving Performance (optional)
The above is a really quick way to add Ruby support to your local webserver however, there’s one downside – it has to start up the Ruby interpreter everytime you hit an rhtml file with your browser. If you want something a little more dedicated look into the mod_ruby apache module which embeds a Ruby interpreter into Apache’s (inside your web server’s memory), to let Ruby CGI scripts execute natively, so scripts start up and run far faster. This is the equivalent as mod_php for PHP, that’s built into your MAMP setup.
Additionally, as a speed boost, you could use fast cgi to keep the eRuby interpreter instantiated, if for some reason you can’t use mod_ruby.