Forum: Ruby Newbie tearing hair out

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-01-04 13:30
(Received via mailing list)
I am trying to get to grips with ruby as an alternative to PHP.

My plan is to convert a simple PHP application to ruby, as I definitely
like the language better from what I have seen.

I have downloaded instantrails and after adding the missing log
directories, it runs the sample appications, mycookbook and typo.

So now I have mysql and apache running - great - and it was an almost
one-click install.

So now I decide Rails is far too involved for me at my stage of
development and I do not really appreciate what it is doing, other than
providing framework for pages.  I am sure it will be very good, but for
now I want to just use ruby as my scripting language for web pages on
an intranet.

I have chosen instantrails, because I wanted Apache and Mysql and
phpMyAdmin, for my current PHP application, I had used WAMP and it
worked seamlessly.  I was looking to do the same with Ruby.  I suppose
I want WAMR - Windows, Apache, Mysql and Ruby.

I am running Windows XP SP2,  but if the project is successful, I will
be moving it to a virtual linux box, using Debian.  I am very flaky
with the Linux as I did a bit of UNIX, years ago - but I try.

So to my problem.  I have created a 'helloworld' web page and called it
'testcgi.rb'. I have put it in various directories, but whenever I try
to execute it as a web page on my localhost - it just prints out the
contents.  I look at httpd.conf and tremble, although I am not sure
which is the right one.  I have installed 'instantrails' under
c:\rubyrail

Contents of testcgi.rb

#!/usr/bin/ruby
# HTTP response headers, including double newline
print "Content-type: text/plain\n\n"
# Contents
print "Hello, world from Tim\n"

I suspect it may be something to do with mod_ruby, fastcgi, scgi, but
at the moment as my 'sample rails apps' work, why won't my simple
xxxx.rb execute ?

So how do I get an xxxxx.rb file to execute  and where an earth do
rhtml files fit in to the mix.

I appreciate I am probably misunderstanding some fundamental concept,
but having been around computers for 40 years, I like to know how the
nitty gritty fits together.

I have accessed many web pages with howto's etc, but none seem to
answer my questions, so any help or pointers would be greatly
appreciated.

As an aside my testcgi.rb will execute as a ruby script on my virtual
linux with ruby testcgi.rb but not as ./testcgi.rb and my permissions
are 755 - any pointers ?

Thanks

Richard
unknown (Guest)
on 2006-01-04 13:33
(Received via mailing list)
removed_email_address@domain.invalid wrote:

> So to my problem.  I have created a 'helloworld' web page and called it
> 'testcgi.rb'. I have put it in various directories, but whenever I try
> to execute it as a web page on my localhost - it just prints out the
> contents.  I look at httpd.conf and tremble, although I am not sure
> which is the right one.  I have installed 'instantrails' under
> c:\rubyrail
>
> Contents of testcgi.rb

Chances are it just needs to be called "testcgi.cgi" (i.e. using the
..cgi extension). This would be an Apache setup aspect.

The code is fine, by the way, though standards-wise you'd be better
using "\r\n\r\n" to end the HTTP headers, not just "\n\n".


Dave.
unknown (Guest)
on 2006-01-04 13:45
(Received via mailing list)
removed_email_address@domain.invalid wrote:
>
> Chances are it just needs to be called "testcgi.cgi" (i.e. using the
> .cgi extension). This would be an Apache setup aspect.
>
> The code is fine, by the way, though standards-wise you'd be better
> using "\r\n\r\n" to end the HTTP headers, not just "\n\n".
>
>
> Dave.

Thanks for the tip.  I have had it called testcgi.cgi and I just get it
printed out, not executed.

Richard
Dave S. (Guest)
on 2006-01-04 13:45
(Received via mailing list)
removed_email_address@domain.invalid wrote:
> The code is fine, by the way, though standards-wise you'd be better
> using "\r\n\r\n" to end the HTTP headers, not just "\n\n".

To head off any other comments: My reference is RFC2616
(http://www.ietf.org/rfc/rfc2616.txt), section 4.1 (CRLF=="\r\n" before
body).


Dave.
Curt H. (Guest)
on 2006-01-04 14:06
(Received via mailing list)
You'll need to do some searching for how to sertup CGI to execute Ruby
scripts (you probably beed to install mod_ruby). This does not come
with Instant Rails, which is designed specifically to run Rails web
apps via SCGI.

Curt
Robert K. (Guest)
on 2006-01-04 14:24
(Received via mailing list)
removed_email_address@domain.invalid wrote:
>>> Contents of testcgi.rb
> Thanks for the tip.  I have had it called testcgi.cgi and I just get
> it printed out, not executed.

Once when I dealt with CGI scripts (a while ago) you had to create a
directory called "cgi-bin" in the root of the web application and place
your scripts there (with executable permissions).  That was all that was
necessary to make them executable as CGI scripts.

Kind regards

    robert
Dave S. (Guest)
on 2006-01-04 14:33
(Received via mailing list)
removed_email_address@domain.invalid wrote:
> Thanks for the tip.  I have had it called testcgi.cgi and I just get it
> printed out, not executed.

Then I'll be a bit more explicit, this time for Apache:

(1) Turn on the CGI handler. You'll need a line like
        AddHandler cgi-script .cgi
    (this would also allow you to use the .rb extension)

(2) In whatever directory the script is in, you'll need the ExecCGI
option enabled. Either that, or use the ScriptAlias option of
mod_alias.

You do *not* need mod_ruby; it is optional, though it might come in
handy. You'll also need FastCGI setup for Ruby on Rails, though I could
be wrong on this point since I've never actually used Rails myself.


Dave.
Walter Prins (Guest)
on 2006-01-04 14:36
(Received via mailing list)
I think you need to configure apache to know that .rb extension files
are to be treated as CGI scripts to be run via the Rub interpreter (or
module.)  (Note I have no experience with this setup, just limited
general experience with this sort of thing with Apache.  )  Basically
you want to look into the Apache "ScriptAlias" directive and/or
"ExecCGI" option.  See here:
http://www.serverwatch.com/tutorials/article.php/1128971

Also you need to tell apache to add a handler for .rb files, for this
look into AddHandler directive.  See e.g. here:
http://www.ricocheting.com/server/cgi.html

Hope this helps.
James B. (Guest)
on 2006-01-04 19:12
(Received via mailing list)
Walter Prins wrote:
> http://www.ricocheting.com/server/cgi.html
>


You'll need something like this in either a .htaccess file in the Web
app's document root directory, or in the httpd.conf file:


<Files ~ "\.(rb|rbx|cgi)$" >
  SetHandler cgi-script
</Files>

Options +ExecCGI
DirectoryIndex   index.cgi index.rb index.html


Also note that Apache on Windows can be configured in two different ways
to execute CGI scripts.  In one approach, it looks at the #! line to
locate the interpreter,  In the other, it ignores that line and just
uses whatever app is associated with the file extension of the scripts.


James



--

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
unknown (Guest)
on 2006-01-04 23:46
(Received via mailing list)
Many thanks to all who responded.  It turned out that the problem was
that apache was configured to execute cgi scripts, but I did not have
the right first line.

For Windows/DOS the correct first line is

#!  /ruby/bin/ruby.exe
  or wherever your path to ruby is.  The important part is to include
the .exe and use forward slashes /

I can now test my ruby scripts, by having them end in .cgi

I will investigate getting .rb to work based on your help.

Thanks to all - Richard
This topic is locked and can not be replied to.