# Apache2, CGI, Ruby & Hello-World

**URL:** <https://rubytalk.org/t/apache2-cgi-ruby-hello-world/31006>\
**Category:** ruby-talk\
**Created:** [14 September 2006 14:50 UTC](https://rubytalk.org/t/apache2-cgi-ruby-hello-world/31006 "2006-09-14T14:50:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Farrar](https://avatars.discourse-cdn.com/v4/letter/n/ecae2f/32.png) [@Nathan\_Farrar](https://rubytalk.org/u/Nathan_Farrar)\
**Post date:** [14 September 2006 14:50 UTC](https://rubytalk.org/t/apache2-cgi-ruby-hello-world/31006/1 "2006-09-14T14:50:08Z")

</div>

According to ProgrammingRuby :  
You can use Ruby to write CGI scripts quite easily. To have a Ruby  
script generate HTML output, all you need is

Code:  
#!/usr/bin/env ruby print "HTTP/1.0 200 OK\r\n" print "Content-type:  
text/html\r\n\r\n" print "\<html\>\<body\>Hello World!\</body\>\</html\>\r\n"

I have a fresh install of ubuntu, almost no experience with apache, cgi,  
or ruby, but a lot of patience.

I haven't modified my apache2 config, I haven't installed any extra  
apache-ruby modules.

If you go to [http://atraeyu.org/](http://atraeyu.org/) you'll notice a ruby.htm file.  
Obviously this won't execute. I'm wondering what I need to do to make it  
execute.

Do I need to install to install an apache module? Can I simply change  
the extension and set it to executable?

I imagine there are a few simple concepts I really don't understand that  
I need to.  
I will really appreciate any help. Thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [14 September 2006 15:50 UTC](https://rubytalk.org/t/apache2-cgi-ruby-hello-world/31006/2 "2006-09-14T15:50:47Z")

</div>

Nathan Farrar wrote:

> According to ProgrammingRuby :  
> You can use Ruby to write CGI scripts quite easily. To have a Ruby  
> script generate HTML output, all you need is
> 
> Code:  
> #!/usr/bin/env ruby print "HTTP/1.0 200 OK\r\n" print "Content-type:  
> text/html\r\n\r\n" print "\<html\>\<body\>Hello World!\</body\>\</html\>\r\n"

This would work, assuming the file had a Ruby extension, and assuming Apache  
was configured to execute Ruby CGI scripts.

> I have a fresh install of ubuntu, almost no experience with apache, cgi,  
> or ruby, but a lot of patience.
> 
> I haven't modified my apache2 config, I haven't installed any extra  
> apache-ruby modules.
> 
> If you go to [http://atraeyu.org/](http://atraeyu.org/) you'll notice a ruby.htm file.  
> Obviously this won't execute. I'm wondering what I need to do to make it  
> execute.

First, as long as it has that extension, Apache will interpret it as an HTML  
file, not a Ruby file. You need to submit your Ruby CGI scripts to Apache  
in such a way that Apache understands what they are.

> Do I need to install to install an apache module? Can I simply change  
> the extension and set it to executable?
> 
> I imagine there are a few simple concepts I really don't understand that  
> I need to.

The problem you face is not to write a Ruby CGI script (for which examples  
abound), it is to get Apache to execute it. I would go on, but this isn't  
about Ruby, it's about Apache configuration.

[http://groups.google.com/groups/search?q=apache+configure+group%3A\*ruby](http://groups.google.com/groups/search?q=apache+configure+group%3A*ruby)\*

Good luck!

> **···**
>
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Michael\_Greenly](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@Michael\_Greenly](https://rubytalk.org/u/Michael_Greenly)\
**Post date:** [14 September 2006 16:28 UTC](https://rubytalk.org/t/apache2-cgi-ruby-hello-world/31006/3 "2006-09-14T16:28:47Z")

</div>

Nathan Farrar wrote:

> According to ProgrammingRuby :  
> You can use Ruby to write CGI scripts quite easily. To have a Ruby  
> script generate HTML output, all you need is
> 
> Code:  
> #!/usr/bin/env ruby print "HTTP/1.0 200 OK\r\n" print "Content-type:  
> text/html\r\n\r\n" print "\<html\>\<body\>Hello World!\</body\>\</html\>\r\n"
> 
> I have a fresh install of ubuntu, almost no experience with apache, cgi,  
> or ruby, but a lot of patience.
> 
> I haven't modified my apache2 config, I haven't installed any extra  
> apache-ruby modules.
> 
> If you go to [http://atraeyu.org/](http://atraeyu.org/) you'll notice a ruby.htm file.  
> Obviously this won't execute. I'm wondering what I need to do to make it  
> execute.
> 
> Do I need to install to install an apache module? Can I simply change  
> the extension and set it to executable?
> 
> I imagine there are a few simple concepts I really don't understand that  
> I need to.  
> I will really appreciate any help. Thanks.

You should rename the file to something \*.rb (hello.rb) for example and  
make it's executable to apache. Set it's owner to www-data, the user  
account apache runs under with the command "chown www-data:www-data  
hello.rb". Then set it's permissions so it can be run "chmod 700  
hello.rb".

Now you need to configure apache. Edit  
"/etc/apache2/sites-available/default". In the \<Directory /var/www\>  
section add the following line...

&nbsp;&nbsp;AddHandler cgi-script .rb

In the options line make sure it includes +ExecCGI so it should look  
something like this....

&nbsp;&nbsp;Options +FollowSymlinks +ExecCGI

Have apache reload the config....

&nbsp;&nbsp;sudo /etc/init.d/apache2 reload

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
