Dark Moon Velvet

Howto: ruby as cgi script

Posted on: April 1, 2009

Short summary

  • Apache’s <Directory /> needs to read Options Indexes FollowSymLinks +ExecCGI
  • Ruby cgi scripts need to have #!C:/path/to/ruby/bin/ruby.exe on Windows or #!/usr/bin/ruby for unix systems.

From the basics

Install wamp, just because its fast, simple, easy and stupid. Done? ok start it! You should see a little icon notication in your taskbar (near the clock). Click it! You should now see a set of menus, these are all shortcuts to whichever little thing you shall ever need (well, almost).

Now, to run a cgi script you will need to set up apache for it. Simply click Wamp, go to Apache, then httpd.conf. This will probably open it in notepad, if you do not have anything better setup. Press Ctrl+F and search for the following: <Directory. Found it? if not go to the top and click on the first line (notepad search is pretty stupid). If you are not following this tutorial using Wamp then this is probably the place you should edit, but since this is wamp its just slightly more... Wamp sets up its own special directory structure, so search again for <Directory and you should find something like (depending on where you installed wamp). These settings override the non specific ones so when using wamp or if you have something similar set up, edit here.

The alterations are relatively simple, simply, where it says Options make sure it says Options Indexes FollowSymLinks +ExecCGI.

Search for AddHandler cgi-script .cgi (it should be commented out, uncomment it by removing the sharp sign in front). then add a extra .rb at the end so it reads AddHandler cgi-script .cgi .rb

All done.

Optionally, you can search for DirectoryIndex and add index.rb to the list of files so that apache can auto execute them.

Now for some ruby scripting

Depending on the system your are using the first line will differ slightly, but in more or less it means the same ("execute the script using this"). For mac/unix systems type something like #!/usr/bin/ruby, while on windows it is: #!C:/path/to/ruby/bin/ruby.exe. Next type in your code, here's a sample snippet:

puts "Content-Type: text/html"
puts "<html><body>"
puts "<p>Congratiolations on completing the intro.</p>"
puts "</body></html>"

This was meant for ruby. But, replace ruby references to Python etc and you get the same effect.

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s