Skip to content

JRubyOnRailsWithGlassfishGem

olecr edited this page Jan 6, 2011 · 10 revisions

Table of Contents

JRuby on Rails with Glassfish gem

Prerequisites

JRuby 1.3.1 or newer

I suggest you install it in /opt/jruby-1.3.1 and symlink /opt/jruby to /opt/jruby-1.3.1 for future convenience.

 # cd /opt && curl -O http://url.to/jruby-1.3.1.tar.gz
 # tar xzvf jruby-1.3.1.tar.gz
 # ln -s jruby-1.3.1 jruby

Glassfish gem by Arun Gupta

 $ gem i glassfish

If you install glassfish in userland (as non-root user), you should for your own convenience add ~/.gem/jruby/1.8/bin to your PATH:

You should also have JRuby in your path. If you install glassfish as root, the glassfish bin will by default be installed in the JRuby bin directory.

 $ export JRUBY_HOME=/opt/jruby 
 $ export PATH=~/.gem/jruby/1.8/bin:$JRUBY_HOME/bin:$PATH

I recommend putting something this in your .bashrc so it's set whenever you log in.

Configure your glassfish instance for your Rails Application

 $ cd /path/to/rails-app
 $ jruby -S gfrake config         # Sets up initial config/glassfish.yml

Now configure the instance to your liking. For instance:

 environment: production
 http:
    port: 3000
    contextroot: /
 log:
    # Logging level. Log level 0 to 7. 0:OFF, 1:SEVERE, 2:WARNING, 3:INFO (default), 4:FINE, 5:FINER, 6:FINEST, 7:ALL.
    log-level: 2
 jruby-runtime-pool:
    initial: 1
    min: 1
    max: 1
 daemon:
    enable: true
    pid: tmp/pids/glassfish-production.pid
    jvm-options: -server -Xmx2500m -Xms64m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:NewRatio=2 -XX:+DisableExplicitGC -Dhk2.file.directory.changeIntervalTimer=6000

Running the application

Running your glassfish application server is very simple:

 $ jruby -S glassfish 

The application is started with the Akuma wrapper, which by default exits when passed a SIGINT (2) (default value of kill). For instance:

 $ kill `cat tmp/pids/glassfish-production.pid`  

Integrating glassfish gem with Solaris Management Facility (SMF)

In Solaris 10 and OpenSolaris, you get an excellent service management facility called SMF which you can easily configure to run your applications on startup (and much more). It can be compared with Linux' init.rd / rc.d, but is much more powerful (out of scope).

Importing an application into SMF is not so trivial for first time users, so here is a brief description.

First off you need to define a manifest. This is done with a simple XML file. Underneath you will find a manifest with support of two instances or rails applications. Modify these to your liking. I usually prefer to run applications with a non-root-user. I have not included ACLs, and tried to minimize the complexity in the example... I usually recommend placing webapplications such as rails somewhere under /var. In the example I use /var/apps, I also use i.e. /var/rails, etc, but you can place them in your home directory if you please.

          <instance name='my-railsapp_production' enabled='false'>
                  <method_context working_directory='/var/apps/my-railsapp/development/my-railsapp'>
                          <method_credential user='railsuser' group='daemon' />
                          <method_environment>
                            <envvar name="PATH" value="/opt/jruby/bin:/usr/bin:/bin" />
                          </method_environment>
                  </method_context>
          </instance>
          <instance name='MYRAILSAPP_ENVIRONMENT' enabled='false'>
                  <method_context working_directory='/FULL/PATH/TO/MY/RAILS/APP/ENVIRONMENT'>
                          <method_credential user='RUNASTHISUSER' group='RUNASTHISGROUP' />
                          <method_environment>
                            <envvar name="PATH" value="/PATH/TO/JRUBY/bin:/usr/bin:/bin" />
                          </method_environment>
                  </method_context>
          </instance>
  </service>
 </service_bundle>

Import the manifest

 $ pfexec svccfg validate config/glassfish-gem.smf.xml
 $ pfexec svccfg import config/glassfish-gem.smf.xml

Now run it

 $ pfexec svcadm enable glassfish-gem:smidig2009_oc

Nginx Frontend

TODO

Apache 2.2 Frontend

On OpenSolaris you can install Apache 2.2 with IPS:

 # pkg install SUNWapch22

On Ubuntu/Debian Linux, you would use apt: (TODO: verify package name on Ubuntu/Debian)





  1. aptitude install apache2.2
This example commands also refer to Solaris. You configure Apache 2.2. the same way in other OSes, but paths to configurations may vary. For instance on Debian/Ubuntu, apache config files are placed in /etc/apache2/sites-available/MY-CONFIG-FILE and these are enabled by a symlink in /etc/apache2/sites-enabled to the config file.

File /etc/apache2/2.2/sites/myrailsapp.jruby.org

 <VirtualHost *:80>
 ServerName myrailsapp.jruby.org
 DocumentRoot /var/apps/my-railsapp/production/my-railsapp/public
 <Directory /var/apps/my-railsapp/production/my-railsapp/public>
  Options FollowSymLinks
  AllowOverride All
  Order allow,deny
  Allow from all
 </directory>
 <Proxy balancer://myrailsapp>
  BalancerMember http://127.0.0.1:3000
 </proxy>
 ProxyPass / balancer://myrailsapp/
 CustomLog /var/log/apache2/myrailsapp_production_apache_access_log combined
 </virtualhost>

Since I place my virtual hosts in separate files, I need to add include to these at the bottom of the main configuration file /etc/apache2/2.2/httpd.conf. If you haven't already configured your Apache server to use Virtual Hosts, you may have to add NameVirtualHost to your configuration:

 NameVirtualHost *:80
 Include /etc/apache2/2.2/sites/*

Now just enable (or restart your apache 2.2 server)

 $ pfexec svcadm enable apache22 
 $ pfexec svcadm restart apache22
Clone this wiki locally