Rhtml and apache trouble

I am trying to run embedded ruby inside rhtml.

$cat test.rhtml

<% print “Content-type: text/html\n\n” %>

Testing <% foo = "Ruby"; print "#{foo}!" %>

It works fine from the command line:

$eruby test.rhtml

Content-type: text/html

Testing Ruby!

But inside my apache setup it doesn’t work.

Accessing “http://localhost/cgi-bin/test.rhtml” gives following error:

"Internal Server Error

The server encountered an internal error or misconfiguration and was
unable to complete your request.

Please contact the server administrator, [email protected] and inform them
of the time the error occurred, and anything you might have done that
may have caused the error.

More information about this error may be available in the server error
log."

Error log says:

[Wed Jan 26 13:21:53 2011] [error] [client ::1] (8)Exec format error:
exec of ‘Dir/cgi-bin/test.rhtml’ failed
[Wed Jan 26 13:21:53 2011] [error] [client ::1] Premature end of script
headers: test.rhtml

I have copied “eruby” executable to “Dir/cgi-bin/” directory and
configured apache (2.2) as follows:

AddType application/x-httpd-eruby .rhtml
Action application/x-httpd-eruby Dir/cgi-bin/eruby

Any ideas? Thanks!

On 01/26/2011 01:40 PM, Mitul Ti wrote:

Testing<% foo = “Ruby”; print “#{foo}!” %>

of the time the error occurred, and anything you might have done that
headers: test.rhtml

Your .rhtml files shouldn’t be in cgi-bin/, they should be in
public_html/ or similar. Looks like Apache’s trying to execute the
.rhtml file, which isn’t an executable.

-Justin

On 01/26/2011 02:36 PM, Mitul Ti wrote:

and I double checked the permission:

-rwxr-xr-x test.rhtml*

But I am still getting the same error. Any ideas? Thanks!

Are you using http://localhost/test.rhtml as the URL?

Justin C. wrote in post #977732:

On 01/26/2011 01:40 PM, Mitul Ti wrote:

Testing<% foo = “Ruby”; print “#{foo}!” %>

of the time the error occurred, and anything you might have done that
headers: test.rhtml

Your .rhtml files shouldn’t be in cgi-bin/, they should be in
public_html/ or similar. Looks like Apache’s trying to execute the
.rhtml file, which isn’t an executable.

-Justin

Thanks Justin for your comments!

I tried keeping .rhtml file in htdocs directory (DocumentRoot directory)
and I double checked the permission:

-rwxr-xr-x test.rhtml*

But I am still getting the same error. Any ideas? Thanks!

Mitul Ti wrote in post #977720:

I am trying to run embedded ruby inside rhtml.

$cat test.rhtml

<% print “Content-type: text/html\n\n” %>

One problem is that you must not output a blank line before the HTTP
headers. That will give the Apache error you saw, at least it does with
CGI scripts. (Having said that, I’m not 100% sure that rhtml files
should generate HTTP headers in the first place)

A second problem is that I don’t think you should use ‘print’ inside
rhtml. Use <%= … %> instead.

$cat test.rhtml
Content-Type: text/html

Testing <% foo = "Ruby" %> <%= "#{foo}!" %>

AddType application/x-httpd-eruby .rhtml
Action application/x-httpd-eruby Dir/cgi-bin/eruby

Have you installed a copy of eruby inside your cgi-bin directory in that
location? Is it executable?

To be honest, I think you’ll find almost nobody uses ruby this way any
more, because it gives you the worst of all worlds: the inefficiency of
cgi-bin (forking a new ruby interpreter for every request) with the
PHP-like muddle of mixing your application logic inside your HTML
templates.

You should probably look at writing an application using a simple
framework like Sinatra, which is just a layer on top of Rack. For
development you can run it as a standalone webserver on its own port.
For production you can either run it inside Apache using Phusion
Passenger, or you can run a pool of mongrel/thin/unicorn/rainbows!
webservers and proxy to them.

Justin C. wrote in post #977779:

On 01/26/2011 02:36 PM, Mitul Ti wrote:

and I double checked the permission:

-rwxr-xr-x test.rhtml*

But I am still getting the same error. Any ideas? Thanks!

Are you using http://localhost/test.rhtml as the URL?

Yes, http://localhost/test.rhtml

As I mentioned I kept test.rhtml in “htdocs” directory (Root directory)
and it’s executable. But it still gives the same error message.

Thanks,
Mitul