Apache2 + ubuntu edgy + ruby

I’m running Ubuntu edgy linux, apache2, php5, and ruby 1.8.4

I’ve been able to get apache2 to run php scripts easily but am having
problems getting ruby to do so. I’ve read references to eruby,
libapache2-mod-ruby, fastcgi etc, etc. So, I’m not sure what I really
need. I did install Ubuntu’s libapache2-mod-ruby and I now see
/etc/apache2/mods-enabled/ruby.load ->
/etc/apache2/mods-available/ruby.load.
I also found a /usr/lib/apache2/modules/mod_ruby.so.

I’ve tried many variations in the site .conf file in
/etc/apache2/sites-available/www.site.local. What I currently have in
this file is:

<VirtualHost *>
ServerAdmin [email protected]
ServerName www.site.local
ServerAlias site.local

AddHandler cgi-script .rb

DirectoryIndex index.php
DocumentRoot /var/www/site

CGI Directory

ScriptAlias /cgi-bin/ /var/www/site/

Options +ExecCGI

Logfiles

ErrorLog /var/www/site/logs/error.log
CustomLog /var/www/site/logs/access.log combined

Since php is working here is what I have added to
/etc/apache2/apache2.conf:

AddType application/x-httpd-php .php
AddType application/x-httpd-php .html

I’m only trying to get apache2 to run ruby programs (.rb files) like it
does .php files. Can someone tell me what is needed to make this work?

When I try to run a test.rb file I’m getting this in the error.log file:

[Fri Dec 08 13:52:30 2006] [error] [client 127.0.0.1] (2)\xd9\x05i\xb7:
exec of
‘/var/www/site/test.rb’ failed
[Fri Dec 08 13:52:30 2006] [error] [client 127.0.0.1] Premature end of
script headers: test.rb

and the browser shows:

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.

test.rb just contains this:

#!/usr/bin/ruby
print “Content-type: text/html\r\n\r\n”
print “Hello World!\r\n”

thanks,

Sam

Sam W. wrote:

/ …

[Fri Dec 08 13:52:30 2006] [error] [client 127.0.0.1] (2)\xd9\x05i\xb7:
[exec of
‘/var/www/site/test.rb’ failed
[Fri Dec 08 13:52:30 2006] [error] [client 127.0.0.1] Premature end of
[script headers: test.rb

This reveals a common error. Your Ruby script has to emit this as its
first
output:

print “Content-type:text/html\r\n\r\n”

There is a similar feature that is automatically provided in the CGI
support
library. But if you will add the above to your script, it should prevent
the failure.

test.rb just contains this:

#!/usr/bin/ruby
print “Content-type: text/html\r\n\r\n”
print “Hello World!\r\n”

Well, that takes care of my theory. The error message is consistent with
my
analysis, but this appears to meet the requirement I suggested. You
could
take the space out, the one between “type:” and “text/html”, but that is
a
long shot.

Other possibilities are wrong file permissions, wrong path to the Ruby
interpreter, things like that.

You could get more information by reading the URL using the ‘net/http’
library:

require ‘net/http’

http = Net::HTTP.new(‘localhost’, 80)
resp, page = http.get(’/(path to page)’, nil )

This provides a lot of detail, some of which might be useful in
resolving
the error.

When I try to run a test.rb file I’m getting this in the error.log file:

[Fri Dec 08 13:52:30 2006] [error] [client 127.0.0.1] (2)\xd9\x05i\xb7: exec of
‘/var/www/site/test.rb’ failed
[Fri Dec 08 13:52:30 2006] [error] [client 127.0.0.1] Premature end of script headers: test.rb

It might be a problem with suexec. Check suexec.log. You probably have
to change the permissions on the script – suexec doesn’t like scripts
(or the directories they’re in) to be group-writable.

Sam W. wrote:

When I try to run a test.rb file I’m getting this in the error.log file:

[Fri Dec 08 13:52:30 2006] [error] [client 127.0.0.1] (2)\xd9\x05i\xb7: exec of
‘/var/www/site/test.rb’ failed
[Fri Dec 08 13:52:30 2006] [error] [client 127.0.0.1] Premature end of script headers: test.rb

You’ll get that if you have DOS style (CRLF) line endings in your
script. Convert to Unix (LF) and see if that fixes it.

Steve

On Sat, 9 Dec 2006 06:44:27 +0900, Grant Hollingworth
[email protected] wrote:

When I try to run a test.rb file I’m getting this in the error.log file:

[Fri Dec 08 13:52:30 2006] [error] [client 127.0.0.1] (2)\xd9\x05i\xb7: exec of
‘/var/www/site/test.rb’ failed
[Fri Dec 08 13:52:30 2006] [error] [client 127.0.0.1] Premature end of script headers: test.rb

It might be a problem with suexec. Check suexec.log. You probably have to change the permissions on the script – suexec doesn’t like scripts (or the directories they’re in) to be group-writable.

‘which ruby’ say
/usr/bin/ruby

the permissions and ownership of test.rb:
-rwxr-xr-x 1 www-data www-data 110 2006-12-08 12:37 test.rb*

the directory that test.rb was in: /var/www/site was owned by root and I
changed it to
www-data:www-data and I get the same error.

I tried changing test.rb to the following but still get the same error
and couldn’t find additional
information:

require ‘net/http’
http = Net::HTTP.new(‘localhost’, 80)
resp, page = http.get(‘/var/www/site’, nil )

I also tried removing the space as mentioned but get the same results.

I also have run into problems with carriage returns and linefeeds in
scripts so I dumped test.rb
just to be sure:

00000000 23 21 2f 75 73 72 2f 62 69 6e 2f 72 75 62 79 0d
|#!/usr/bin/ruby.|
00000010 0a 70 72 69 6e 74 20 22 43 6f 6e 74 65 6e 74 2d |.print
“Content-|
00000020 74 79 70 65 3a 74 65 78 74 2f 68 74 6d 6c 5c 72
|type:text/html\r|
00000030 5c 6e 5c 72 5c 6e 22 0d 0a 70 72 69 6e 74 20 22
|\n\r\n”…print “|
00000040 3c 68 74 6d 6c 3e 3c 62 6f 64 79 3e 48 65 6c 6c
|Hell|
00000050 6f 20 57 6f 72 6c 64 21 3c 2f 62 6f 64 79 3e 3c |o
World!<|
00000060 2f 68 74 6d 6c 3e 5c 72 5c 6e 22 0d 0a
|/html>\r\n”…|
0000006d

Thanks for your help,
Sam

On Sat, 09 Dec 2006 12:15:37 GMT, Steven J Masta [email protected]
wrote:

You’ll get that if you have DOS style (CRLF) line endings in your
script. Convert to Unix (LF) and see if that fixes it.

Steve

That was the problem: I needed to get rid of the carriange returns
(0x0d). I used the ‘fromdos’ in
Ubuntu’s packages. Thanks.