JRuby and Glassfish on Rails Part II: Setting up a Production Environment on Red Hat Linux

By Freddy Andersen on Monday, December 1st, 2008 in Technical | Related Software Packages: , , , , | Keywords: ,

Now we’ll deploy a Ruby on Rails application (we’ll use a ready-to-deploy Rails application, Radiant, that drops into this environment easily) into a production-ready JRuby on Rails environment running GlassFish. This tutorial does not cover tuning any parameters, but focuses rather on getting the server installed and running, and successfully deploying a Rails application into the GlassFish container.

Before You Start

We will use a RedHat ES 5 server for this tutorial. We’ve chosen the latest stable binary of JRuby 1.1.5 and we will use GlassFish v2. I think that’s all we need to know before we jump into the …

Meat & Potatoes

Installing JRuby

  1. Download the latest JRuby release from OLEX.
  2. Unzip and copy the JRuby directory to your /usr/local/ directory. (If you don’t like the /usr/local/ area but are more of a /opt sort, that’s fine, too, but this tutorial will reference /usr/local/ as the install location.)
  3. Create a file /etc/profile.d/jruby.sh and add the following content:
  4. Log-out and log-in to your system, this will make sure the environment for JRuby is correctly setup.
  5. Test JRuby with the following command:
    export JRUBY_HOME=/usr/local/jruby
    export PATH=$PATH:$JRUBY_HOME/bin

    [freddy@wazi]# jruby -v
    jruby 1.1.5 (ruby 1.8.6 patchlevel 114) (2008-11-03 rev 7996) [amd64-java]

That’s working great!

Install Rails

The recommended way to run these commands (known as system-level executable commands) in JRuby is to always use jruby -S.

[freddy@wazi]# jruby -S gem install rubygems-update
[freddy@wazi]# jruby -S update_rubygems
[freddy@wazi]# jruby -S gem install jruby-openssl
[freddy@wazi]# jruby -S gem install rails activerecord-jdbcmysql-adapter warbler

[freddy@wazi]# jruby -S rails -v Rails 2.2.2

We are going to use Radiant CMS for some simple testing, so let’s install and setup Radiant…

[freddy@wazi]# jruby -S gem install radiant
[freddy@wazi]# jruby -S radiant --database mysql /usr/local/wazi-radiant
[freddy@wazi]# mysqladmin create wazi-radiant_production

We are not going to set up any security for the MySQL instance; that’s a different topic altogether. For this reason, a local MySQL server in conjunction with the root user should work.

=> /usr/local/wazi-radiant/config/database.yml <=
production:
adapter: mysql
database: wazi-radiant_production
username: root
password:
host: localhost

That should do it for the configuration, now let’s bootstrap the application:

[freddy@wazi /usr/local/wazi-radiant]# jruby -S rake production db:bootstrap

This task will destroy any data in the database. Are you sure you want to
continue? [yn] y

= 1 CreateRadiantTables: migrating ==========================================Create the admin user (press enter for defaults).
Name (Administrator):
Username (admin):Password (radiant):
Select a database template:
1. Empty
2. Simple Blog
3. Styled Blog[1-3]: 3
Creating Pages....OK
Creating Layouts....OK
Creating Snippets....OKCreating Page parts....OK
Finished.

Now, let’s run a quick test to see if everything is where it should be. We will start WEBrick and open a test page:

jruby -S script/server -e production -p 4444
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:4444
=> Ctrl-C to shutdown server; call with --help for options
[2008-11-19 16:29:03] INFO  WEBrick 1.3.1
[2008-11-19 16:29:03] INFO  ruby 1.8.6 (2008-11-03) [java]
[2008-11-19 16:29:03] INFO  WEBrick::HTTPServer#start: pid=3359 port=4444

Pointing the browser to http://localhost:4444/ should now result in a nice looking blog interface, with an admin section available at http://localhost:4444/admin.

Now, let’s install and configure GlassFish so that we can deploy this application in a Java engine.

Get open source software support

Installing GlassFish

To install and configure GlassFish, you need to have JDK v5 or JDK v6 installed on your system. For this demo we are using JDK1.6.0_02-b05.

You can download the Glassfish v2-ur1-b09d installer from OLEX here https://olex.openlogic.com/packages/glassfish. And here are the instructions for running the installer:

Make sure your JAVA_HOME is set to a JDK!

[freddy@wazi]# java -Xmx256m -jar filename.jar
[freddy@wazi]# mv glassfish /usr/local/
[freddy@wazi]# cd /usr/local/glassfish
[freddy@wazi]# chmod -R +x lib/ant/bin
[freddy@wazi]# lib/ant/bin/ant -f setup.xml
We are waiting for BUILD SUCCESSFUL

Now you can move the Glassfish directory where you would like your Glassfish installation to live. ( Like /usr/local/glassfish )

Now cd into the glassfish directory and start the server…

[freddy@wazi]# bin/asadmin start-domain

...
Domain listens on at least following ports for connections:
[8080 8181 4848 3700 3820 3920 8686 ].
Domain does not support application server clusters and other standalone instances.

You should now be able to access the GlassFish admin screen at http://localhost:4848, and the GlassFish landing page at http://localhost:8080/. The username/password for the admin console is admin/adminadmin.

We will now use Warbler to package our application into a war file, but we will need to setup the warble.rb configuration file first. First, we should create a generic configuration file for warbler. Strictly speaking, you don’t “need” a wrble.rb file, BUT it makes the process a lot easier, so we recommend it.

[freddy@wazi]# jruby -S warble config

The warble.rb file has a few issues with the Radiant gem, so we have to edit the file:

=> config/warble.rb <=

Warbler::Config.new do |config|
config.staging_dir = "tmp/war"
config.dirs = %w(config log vendor tmp)
config.gems += ["activerecord-jdbcmysql-adapter", "jruby-openssl"]
require "#{RAILS_ROOT}/config/boot"
BUILD_GEMS = %w(warbler rake rcov)
for gem in Gem.loaded_specs.values
next if BUILD_GEMS.include?(gem.name)
config.gems[gem.name] = gem.version.version
end
config.gem_dependencies = true
config.webxml.rails.env = ENV['RAILS_ENV'] || 'production'
end
[freddy@wazi /usr/local/wazi-radiant]# jruby -S warble
[freddy@wazi /usr/local/wazi-radiant]# ls *.war
wazi-radiant.war

We now have a .war file that is deployable, so let’s push that to our running GlassFish server:

[freddy@wazi /usr/local/wazi-radiant]# ../glassfish/bin/asadmin deploy --contextroot / wazi-radiant.war

Command deploy executed successfully.

Finishing Up

Now our Radiant application can be accessed via http://localhost:8080/.

This was a very high level tutorial geared to get you up and running fast using Glassfish as your production Ruby on Rails server. We used a ready-to-deploy Rails application, Radiant, that drops into this environment easily and shows something ‘real’ when the process is completed.

Freddy Andersen

Freddy supports OpenLogic customers from his home in California. Born in Norway, Freddy has been working in open source for almost 10 years. He drives a Ferrari truck when he isn't running (either for exercise or chasing Cade, his two-year-old, or simply because he's had too much Red Bull).

One Response to “JRuby and Glassfish on Rails Part II: Setting up a Production Environment on Red Hat Linux”

  1. [...] Swahili – an extension of OpenLogic Exchange comparing open source packages and licenses, sharing best practices and [...]

Leave a Reply

© 2010 OpenLogic, Inc. | Licensing | Privacy Policy | Terms of Use