Archive for Ubuntu Feisty Fawn

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

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.gz

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

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

JRuby also needs its own environment variable.
export JRUBY_HOME=/home/aizat/src/jruby

Finally its to add the JRuby executables into your PATH
export PATH=$PATH:/home/aizat/src/jruby/bin

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 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!

Comments (3)

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.

Comments (18)

Migrating Your Rails Application To Subversion

Note: Once again, a quick, down and dirty solution to moving your Rails application to subversion. The Rails wiki already has this covered, but I find it so messy.

Plan Of Attack

  • Install Subversion
  • Create Rails Application Repository
  • Migrate Rails Application
  • Tips and Tricks
    • Using Plugins
    • Accessing your repository via the Web

Installing Subversion

On Ubuntu Fesity Fawn execute:

sudo apt-get install subversion

Creating Application Repository

I like keeping my version control repositories in my home directory (ala /home/aizat/repo File).

mkdir /home/aizat/repo
svnadmin create /home/aizat/repo/railsapp


Note: If you plan to share the repository with other people, or plan to access it via the web, I suggest you move it into some where public. This could be either /repo or /home/repo. You can move your repository later if required.

Migrating Rails Application

Warning: If you are running any server (webrick or mongrel) shut it down.
Note: This is all executed from the root of your rails application directory.

Move your application to a backup directory.

mv railsapp railsapp.backup

Check out the repository

svn co file:///home/aizat/repo/railsapp

Create the common subversion directories:

mkdir railsapp/{branches,tags}

Copy the backup as the trunk in the repository

cp -a railsapp.backup railsapp/trunk

Move to your trunk directory.

cd railsapp/trunk

Remove temporary files, logs, and other useless cruft.

rm log/*.log
rm -Rf tmp/*

Note: I don’t use rake log:clear because it empties the various logs, but leaves the log files there. Similarly I don’t use rake tmp:clear so that we can keep the tmp/ completely clean.

Now we remove database.yml as it poses a security risk, because it contains your database information (duh!).

rm config/database.yml

Optional: If you want you can keep an example template, to remind you how the database.yml file looks like. You can grab some from the Rails trunk in their repository.

If you do decide to do this, save it as database.example in your config/

Warning: Similarly if you have other files that store passwords (for example connecting to your mail server) DO NOT store them in your repository. Even if you don’t share your repository, because one day you may just do that.

SQLite specific: If you are using sqlite and the database is stored in your application directory. Remove the database files.

rm db/*.db

Note: If you created documentation for your application via Rake, chances are you don’t want it to be saved in your repository

rm -Rf doc/app

Now we go down a level, and indicate we want these directories saved in our repository

cd ..
svn add trunk branches tags

To ensure that your database configuration, logs, and tmp stuff are not stored in subversion, we tell svn to ignore them.

svn propset svn:ignore database.yml trunk/config/
svn propset svn:ignore "*.log" trunk/log/
svn propset svn:ignore "*" trunk/tmp/
svn propset svn:ignore app trunk/doc/
svn propset svn:ignore plugins trunk/doc/
svn propset svn:ignore api trunk/doc/

SQLite specific: Let’s ensure that the database is not stored in subversion.

svn propset svn:ignore trunk/db/*.db

Your rails setup should now be prepared to commit to the repository.

svn ci -m "Initial commit"
svn update

Note: After the initial commit, I remove the railsapp/ directory, and checkout again, like below. This simplifies the directory structure for me.

You are now good to go, and can checkout the repository anytime via

svn co file:///home/aizat/repo/railsapp/trunk railsapp 

Tips and Tricks

Using Plugins

You can now keep your plugins up to date with subversion as well. When installing a plugin, use the -x flag. Example:

./script/plugin install -x acts_as_taggable

Accessing your repository via the Web

Let’s first install Apache.

sudo apt-get install apache libapache2-svn

Now the big question is. Where did you create your repository, because Apache needs access to it. If you’ve created your repository in ~/repo, how do we move it? Let’s assume you’ll be moving it to /home/svn.

So lets create the repository location, and create the necessary groups

sudo mkdir /home/svn
sudo groupadd svn
sudo adduser aizat svn
sudo adduser www-data svn
sudo chown -R www-data:svn /home/svn
sudo chmod -R 0660 /home/svn

Note: In the third line sudo adduser aizat svn, replace ‘aizat‘ with your actual username. You may need to login, and out to update the groups you are in.

So we’ll prepare the repository for access by Apache.

svnadmin dump /home/aizat/repo/railsapp > /home/aizat/repo/railsapp.dump
cd /home/repo
svnadmin create /home/svn/railsapp
svnadmin load /home/svn/railsapp < /home/aizat/repo/railsapp.dump

To ensure that no one can access the repository, we will use simple authentication.

htpasswd /home/svn/railsapp.passwd aizat

Open up /etc/apache2/mods-enabed/dav_svn.conf File, and insert:


<Location /svn/railsapp>
  DAV svn
  SVNPath /home/svn/railsapp
  AuthType Basic
  AuthName "Rails Application"
  AuthUserFile /home/svn/railsapp.passwd
  Require valid-user
</Location>

This will make your subversion repository accessible via http://example.com/svn/railsapp.

So you can check out your code ala:

svn co http://example.com/svn/railsapp/trunk railsapp

Note: Pay Attention to the SVNPath and AuthUserFile those should be set accordingly to your setup. There is also support for ACL, but I won’t cover that now.

Warning: You are still vulnerable to man in the middle attacks, but it is good enough in some cases. To ensure another layer of security, it is recommended to use SSL. Easy creation of SSL is currently broken in Ubuntu Fesity Fawn. There is the traditional method, but that is really long, and really goes out of scope of this post. I’ll cover it if people really want it.

Subversion Resources

If you are new to subversion, have a look at the book Version Control With Subversion. It’s freely available online in multiple formats (HTML, PDF, DocBook).

Comments (3)

« Previous entries