Strange Symphonies The whole is greater than the sum of its parts

7Jun/070

JRuby on Ubuntu Feisty Fawn

If there is one short coming about Ruby, its the limited number of libraries available for it. Sure its growing by the day, but its nowhere in comparison to the Java Programming Language (which will be referred to as just "Java" hence forth). Though they are around the same age, Java has always had the financial and corporate support and through that has established itself in the industry as one of the languages any programmer has gone through. High chances are you'll learn it in University.

So how do we leverage off Java's libraries? Lucky for Ruby (and Java) fanatics, the team behind JRuby is bridging the gap between the two languages, by providing an implementation of Ruby on the Java Virtual Machine (JVM).

Sounds almost like a sick and twisted joke. Meshing one with the other, like Frankenstein!

For the People of Ruby:

  • You get to play with all the Java libraries
  • Ability to run Ruby on machines that support the Java Virtual Machine

For the People of Java:

  • You get to play with Ruby

Alright, there are a lot more reasons...

This is great news, especially since Java and the JVM are both free and open source software now.

Note: JRuby is supported by Sun Microsystems, to the extent that they have hired the core developers to continue working full time on JRuby.

Getting Java

I opted to use the Java version 5 to get it running as I've seem to have run into trouble getting it working on version 6, but I'll keep trying. (Problem is the tests run forever!)

sudo apt-get install sun-java5-jdk ant ant-optional junit</code>

Getting JRuby

You can grab the JRuby source via their tarball, or if you prefer, via subversion.

I would recommend installing via Subversion for now, as the current version of JRuby (1.0.0RC3) fails their own tests (at least on my Machine)!

Installing JRuby via Subversion

I like installing via subversion as I can easily update to the latest version of JRuby.

svn co http://svn.codehaus.org/jruby/trunk/jruby/ ~/src/jruby

Installing JRuby via Tarball

Once you get the tarball, untar it.

tar -zxvf jruby-src-1.0.0RC3.tar.gzp

I like storing any source related things in my ~/src, you can put it wherever you like (perhaps /usr/src).

mv jruby-1.0.0RC3/ ~/src/jruby</code>

Note: Your filenames/directory names may vary, as the latest version of JRuby as of this writing is 1.0.0 RC3

Setting up the Environment Variables

The environment variable for JAVA_HOME on Ubuntu doesn't seem to be set up properly for some reason. In fact, I think its broken. So I'll set it up temporarily in the shell.

export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun-1.5.0.11</code>

JRuby also needs its own environment variable.

export JRUBY_HOME=/home/aizat/src/jruby</code>

Finally its to add the JRuby executables into your PATH

export PATH=$PATH:/home/aizat/src/jruby/bin</code>

Note:

  • If you close the shell, you'll have to define the JAVA_HOME environment variable again. There are various ways to fix this. One is to stuff these commands into a file and "source file [image16x16:terminal]" them whenever needed. To have them automatically load, append the above lines into ~/.bashrc.
  • Your paths may vary, especially the location of JRuby

Compile and Test

ant test

This will take a while to execute...just go out and have a cup of coffee.

There is no progress bar, so just wait it out.

6 and a half minutes later

So it took about 6 and half minutes to compile and test JRuby on my IBM R52 1.6Ghz Centrino 2Gb Ram laptop. Seems quite decent.

Conclusion

There you have it, you have a working version of JRuby that is (or will be) 100% compatible with Ruby v1.8.5. I haven't tested it yet, but there are several tutorials you can start with.

Just pull up jirb and play along, it'll look and feel like Ruby.

I'll have to look at performance issues when doing this. But hey Ruby isn't a speed king to begin with. There are various articles covering performance issues with different implementations of Ruby.

There is also a growing wiki on JRuby, so if you have anything interesting, add it to that!

Looking Further Ahead

I am looking at this to harness the power of Ruby on the S60 platform for my new Nokia e61i. There exists an implementation of of Ruby for Symbian phones, but its not mature (some might consider JRuby isnt as well!)

I chose to use JRuby instead of Ruby for Symbian because Java already has all the APIs I need. Also worse comes to worse, the experience of knowing JRuby over Ruby for Symbian is a lot more worth it.

Wish me luck, I hope I don't break my phone!

20May/0718

Deploying Ruby on Rails on Ubuntu Feisty Fawn via Mongrel Cluster and Apache

Note:

  • A quick, down and dirty solution. There are more verbose resources on how to setup Mongrel Cluster and Apache. But this is also specifically tailored for Ubuntu Feisty Fawn.
  • After installing mongrel, when executing ./script/server, you will not use Webrick anymore, but instead mongrel for development. You can still use Webrick if you want, but there isn't much of a difference.

Plan of Attack

  • Install, configure, and test Mongrel Cluster
  • Install and test Apache
  • Start Mongrel Cluster on boot

Installing Mongrel Cluster

sudo apt-get install ruby1.8-dev
sudo gem install -y mongrel mongrel_cluster

Note: If you see the following make sure you select the latest version of mongrel for ruby. In this case option 2.

Select which gem to install for your platform (i486-linux)
1. mongrel 1.0.1 (mswin32)
2. mongrel 1.0.1 (ruby)
3. mongrel 1.0 (mswin32)
4. mongrel 1.0 (ruby)
5. Skip this gem
6. Cancel installation
>

Configuring Mongrel Cluster


Go to your Rails application directory and execute:

mongrel_rails cluster::configure -p 8000 -n 3

This allows mongrel to spawn 3 instance to handle the load. Each instance will open on port 8000, and succeeding. So that is 8000, 8001, and 8002.

Note: The mongrel_rails command has alot of options. Executing mongrel_rails -h will get you more information. All of these options can be changed later.

This will create the a mongrel_cluster configuration file in RAILS_ROOT/config/mongrel_cluster.yml File

Testing Mongrel Cluster


In your Rails application directory, execute:

mongrel_rails cluster::start

Now open up http://localhost:8000/, http://localhost:8001/, and http://localhost:8002/ to ensure that your site is working perfectly fine.

Installing Apache


Enter into the Terminal:

sudo apt-get install apache2
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod rewrite

We will now create a common rails configuration file. As root open up /etc/apache2/mods-available/rails.conf File, and insert the following:

  ServerName railsapp.com
  DocumentRoot /var/www/railsapp.com/public

  <Directory "/var/www/railsapp.com/public">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>

  RewriteEngine On

  # Uncomment for rewrite debugging
  #RewriteLog logs/railsappapp_rewrite_log
  #RewriteLogLevel 9

  # Check for maintenance file and redirect all requests
  #  ( this is for use with Capistrano's disable_web task )
  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html [L]

  # Rewrite index to check for static
  RewriteRule ^/$ /index.html [QSA]

  # Rewrite to check for Rails cached page
  RewriteRule ^([^.]+)$ $1.html [QSA]

  # Redirect all non-static requests to cluster
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

  # Deflate
  AddOutputFilterByType DEFLATE text/html text/plain text/css
  # ... text/xml application/xml application/xhtml+xml text/javascript
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

  # Uncomment for deflate debugging
  #DeflateFilterNote Input input_info
  #DeflateFilterNote Output output_info
  #DeflateFilterNote Ratio ratio_info
  #LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
  #CustomLog logs/myapp_deflate_log deflate

Now we'll set up the virtual host for our site railsapp.com For the last time as root open up /etc/apache2/sites-available/railsapp File and insert the following:

Listen 8080
<VirtualHost *:8080>
  <Location />
    SetHandler balancer-manager
    Deny from all
    Allow from localhost
  </Location>
</VirtualHost>

<Proxy balancer://mongrel_cluster>
  BalancerMember http://127.0.0.1:8000
  BalancerMember http://127.0.0.1:8001
  BalancerMember http://127.0.0.1:8002
</Proxy>

<VirtualHost *:80>
  Include /etc/apache2/mods-available/rails.conf
  ErrorLog /var/log/apache2/rails_errors_log
  CustomLog /var/log/apache2/rails combined
</VirtualHost>

The first VirtualHost enables a front end for load balancing, available only to localhost on port 7999.

The second VirtualHost is our rails application. Notice how it is pulling in our default configuration (/etc/apache2/mods-available/rails.conf File). If you want to make changes you can specify it here. Need to specify a ServerName directive? Put it in here.

Lets enable the virtual host

sudo a2ensite railsapp

and restart Apache.

sudo /etc/init.d/apache2 restart

Now pull up your the domain you entered and your Rails application should load!

Note: If you can't seem to access your site, its because the default virtual host has a higher precedence. Execute sudo a2dissite default Terminal. Similarly ensure that you are access your site via the domain you specified in the ServerName directive. In this case railsapp.com

If everything is work, you are on your way to more production based hosting solution.

Start Mongrel Cluster on Boot

Create mongrel_cluster conf directory (/etc/mongrel_cluster).

sudo mkdir /etc/mongrel_cluster

In the Mongrel Cluster gem, there is an init.d script you need to copy.

sudo cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-0.2.1/resources/mongrel_cluster/mongrel_cluster /etc/init.d

Now let's make the script executable.

sudo chmod +x /etc/init.d/mongrel_cluster

Now we need to add th init.d script to startup.
On Ubuntu:

sudo update-rc.d mongrel_cluster defaults

On RHEL/CentOS:

/sbin/chkconfig --level 345 mongrel_cluster on

Now create a symbolic link from RAILS_ROOT/config/mongrel_cluster.yml to a file in /etc/mongrel_cluster:

sudo ln -s /path/to/railsapp/config/mongrel_cluster.yml /etc/mongrel_cluster/railsapp

There you have it, you should be good to go!

Comments, bugs, problems, improvements

Have any bug, or problem? Leave a comment please.

15May/075

Setting up Ruby on Rails Database Connection using MySQL on Ubuntu Feisty Fawn

MySQL is a rock solid database, used all around the world, for all kinds of situations. Its use escalated with the usage of PHP and together became known as the LAMP (Linux Apache MySQL and PHP) stack.

Warning: By default, root access to MySQL is open. We will deal with this later.

Installation

Enter into the Terminal:

sudo apt-get install mysql-server

Edit the file RAILS_ROOT/config/database.yml File. For now we are using the development Database environment. The environment you are deploying your database should look like so:

development:
  adapter: mysql
  database: railsapp_development
  username: root
  password:
  host: localhost

Note: Modify to fit your needs

If you don't have a database created yet, execute:

mysqladmin -u root create railsapp_development

Note: Where railsapp_development should be the same as the database field we configured.

And there you have it! Your Rails application will now work with MySQL.

Continue if you want to learn more about MySQL.

Locking down MySQL

By default 3 accounts are associated to the 'root' user. Each one without a password. Definitely not safe.

So we'll remove 2 of those accounts, and give a password to the remaining one.

Lets enter MySQL:

mysql -u root
use mysql;
DELETE FROM user WHERE User = 'root' &amp;&amp; Host != 'localhost';
UPDATE user SET Password=PASSWORD('new-password') WHERE User='root';
FLUSH PRIVILEGES;

Note: Make sure to specify the password in place of 'new-password'!

Now in order to access MySQL via root, you'll have to use:

mysql -u root -p

And you should be prompted for your password.

Note: People tend to append your password after the -p flag. Do not do this. It will make the password visible via ps

.

Even man mysql states:

Specifying a password on the command line should be considered insecure. See Section 7.6, Keeping Your Password Secure.

If you immediately want to access your railsapp_development database, you can just specify it like so.

mysql -u root -p railsapp_development

Easy MySQL Administration

Coming from a PHP background, I am familiar with phpMyAdmin, but there are other solutions out there. The problem with phpMyAdmin is that it requires the installation of both Apache and PHP, which opens more ports on your computer, and adds uneeded extra software on your computer.

Another solution is to use mysql-admin (sudo apt-get install mysql-admin Terminal), though I haven't tried using it.

phpMyAdmin can be installed via:

sudo apt-get install phpMyAdmin

Conclusion

There you have it, your database is now set up!