Running Ruby on Rails on Sun Java System Web Server 7.0

   
By Seema Alevoor and Marina Sum, January 30, 2007, updated: February 27, 2008  

Ruby on Rails (henceforth, RoR) is an open-source project in the Ruby interpreted scripting language with the goal of delivering a framework for efficiently developing Web applications. Sun Java System Web Server 7.0 (henceforth, Web Server 7.0), the latest Web server release from Sun, bundles FastCGI, an extension to the Common Gateway Interface (CGI), for scripting environments such as RoR. In addition, Web Server 7.0 offers many robust capabilities, including support for the Solaris 64-bit platform and cluster management as well as integration with Sun IDEs.

This article first describes how to install the Ruby language; RubyGems, the project for the standard that creates and manages libraries; Ruby on Rails; and the FastCGI Ruby gem. A gem is a packaged Ruby application or library. Later in the article, you learn how to create an RoR application and configure Web Server 7.0 to run RoR.

Note the following:

  • The procedures in this article apply to the Solaris 10 Operating System on the SPARC platform.
  • Type each of the command lines in this article on one line even though, because of screen width constraints, some of them wrap to the next line.
Contents
 
Installing Ruby, RubyGems, and Rails
Creating a HelloWorld RoR Application
Configuring Web Server 7.0
Conclusion
References
 
Installing Ruby, RubyGems, and Rails

The installations for Ruby, Ruby Gems, and Rails copy the executables to the /usr/local/bin directory. For easy execution, add that path to your PATH variable.

Note: If you cannot write to the /usr/local/bin directory, become root before starting the installation process.

Installing Ruby

To install Ruby:

  1. Download and extract version 1.8.4 under /ruby.
  2. Run these commands:

    % cd /ruby/ruby-1.8.4
    % ./configure
    % make
    % make install


    Note: If you compile with gcc but encounter compile-time errors, delete the gcc directory from your PATH variable and switch to one of the Sun Studio compilers.

Installing RubyGems

To install RubyGems:

  1. Download and extract version 0.9.0 under /ruby.
  2. Run these commands:

    % cd /ruby/rubygems-0.9.0
    % export DLN_LIBRARY_PATH=/ruby/ruby-1.8.4/.ext/sparc-solaris2.10
    % export RUBYLIB=/ruby/ruby-1.8.4/ext:/ruby/ruby-1.8.4/lib:/ruby-1.8.4:/ruby/ruby-1.8.4/.ext/sparc-solaris2.10
    % ruby setup.rb --rbconfig=/ruby/ruby-1.8.4/rbconfig.rb

Installing Rails

To install Ruby on Rails:

  1. Optional. If you are behind the firewall, set the proxy. Type:

    % export http_proxy=http:// proxy_hostname : proxy_portnumber

    where proxy_hostname is the proxy's machine name and proxy_portnumber is the proxy's port number.
  2. Run this command:

    % gem install rails --include-dependencies

Installing FastCGI Ruby Gem

FastCGI offers a more efficient way than CGI for Web servers to call applications. To install the FastCGI Ruby gem:

  1. Download the source under /ruby.
  2. Download the FastCGI development kit under the /fcgi-2.4.0 directory.
  3. Run these commands:

    % cd /ruby
    % gem install fcgi -- --with-fcgi-include=/fcgi-2.4.0/include --with-fcgi-lib=/fcgi-2.4.0/libfcgi/.libs
Creating a HelloWorld RoR Application

To create a Hello World application in RoR:

  1. Run these commands:

    % mkdir /ruby/samples
    % cd /ruby/samples
    % rails hello-world
    % cd hello-world
    % ruby script/generate controller hello
  2. Create a file named index.rhtml in the app/views/hello directory with the following one-line content:

    Hello!
    
     
  3. Start the default WEBrick Web Server to test the application. Type:

    % ruby script/server
  4. Go to http://localhost:3000/hello on your browser.

    The greeting, Hello!, is displayed.

Next, add some substance to the application:

  1. Go to the controllers directory. Type:

    % cd /ruby/samples/hello-world/app/controllers
  2. In the hello_controller.rb file, add the following three boldfaced lines.

    class HelloController < ApplicationController
         
                           def sayhello         render_text "Hello! This is a simple example."     end
    end
                        
     
  3. Restart WEBrick. Type:

    % ruby script/server
  4. Access the newly added action, sayhello, at http://localhost:3000/hello/sayhello.

    Note: The URI is of the form controller/action.

    The greeting—
    Hello! This is a simple application.
    —is displayed.
Configuring Web Server 7.0

To configure Web Server 7.0:

  1. Add the following line to the magnus.conf file to load the FastCGI plug-in that is bundled with Web Server 7.0:

    Init fn="load-modules" shlib="libfastcgi.so" shlib_flags="(global|now)"
    
     
  2. Add the following code lines to the obj.conf file.

    <Object name="default">
    ...
    ...
    #
    # Pass requests for /dispatch.fcgi to rubyTest object.
    #
    NameTrans fn="assign-name" from="/dispatch.fcgi/*" name="rubyTest"
    #
    # Prefix /dispatch.fcgi/ to the original URI, which does not contain dispatch.fcgi, and
    resend the request.
    #
    <If $uri !~ '^/dispatch.fcgi/.*'
    and $uri !~ '^/stylesheets/*'
    and $uri !~ '^/javascripts/*'>
    <If defined $query>
        NameTrans fn="restart" uri="/dispatch.fcgi$uri?$query"
    </If>
    <Else>
        NameTrans fn="restart" uri="/dispatch.fcgi$uri"
    </Else> 
    </If>
    
    #
    # Set the document root to RoR sample's public directory.
    #
    # Note: This is the last NameTrans directive.
    #
    NameTrans fn=document-root root="/ruby/samples/hello-world/public"
    ...
    ...
    </Object>
    ...
    ...
    #
    # Object to handle the RoR application requests.
    # Here, app-path must point to the dispatch.fcgi script of the RoR sample.
    #
    <Object name="rubyTest">
    Service fn="responder-fastcgi" app-path="/ruby/samples/hello-world/public/dispatch.fcgi"
        bind-path="localhost:4334" app-env="RAILS_ENV=production"
        app-env="RUBYLIB=/usr/local/lib/ruby/1.8"
    </Object>
    
     
  3. Transpose the manual edits to the Web Server 7.0 Administration Server's configuration repository. Perform that task on the command line or from the Administration Console.

    Start the Administration Server and then type:

    % Web-Server-install-dir /bin/wadm pull-config --user=admin --config= CONFIG hostname

    where CONFIG is the name of the configuration that contains the file changes.

    For details on the command-line interface (CLI), see the Sun Java System Web Server 7.0 CLI Reference Manual .
  4. Start Web Server 7.0 and then go to http://localhost:80/hello/sayhello, where localhost and 80 are the host name and port number of Web Server 7.0, respectively.

    The greeting—
    Hello! This is a simple example.
    —is displayed.
Conclusion

Configuring Web Server 7.0 to run RoR with FastCGI is simple and hassle-free. A future article will describe how to run JRuby on Web Server 7.0. Stay tuned!

References
Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
Your email address (no reply is possible without an address):
Sun Privacy Policy

Note: We are not able to respond to all submitted comments.
Left Curve
Java SDKs and Tools
Right Curve
Left Curve
Java Resources
Right Curve
JavaOne Banner
Java 8 banner (182)