Monday, November 17, 2008

Mac(OSX) Jruby and Rails 2.2 -- Part 2

Running webrick isn't doing what we want... How about Tomcat? Or maybe Glassfish? Well I have heard that glassfish is VERY EASY, lets checkit out...

Step 1:
~$ sudo jruby -S gem install glassfish
Successfully installed glassfish-0.9.0-universal-java
1 gem installed
Installing ri documentation for glassfish-0.9.0-universal-java...
Installing RDoc documentation for glassfish-0.9.0-universal-java...

DONE... no I'm serious... THAT WAS IT!!!

Lets start the server:

~$ jruby -S glassfish_rails myapp
Nov 17, 2008 7:21:23 PM com.sun.enterprise.glassfish.bootstrap.ASMain main
INFO: Launching GlassFish on Apache Felix OSGi platform
Nov 17, 2008 7:21:23 PM com.sun.enterprise.glassfish.bootstrap.ASMainOSGi findDerbyClient
INFO: Cannot find javadb client jar file, jdbc driver not available
...

Now I can access the rails app at http://localhost:3000/

NICE!!

Here is some more information about the glassfish gem:

-c, --contextroot PATH: change the context root (default: '/')
-p, --port PORT: change server port (default: 3000)
-e, --environment ENV: change rails environment (default: development)
-n --runtimes NUMBER: Number of JRuby runtimes to crete initially
--runtimes-min NUMBER: Minimum JRuby runtimes to crete
--runtimes-max NUMBER: Maximum number of JRuby runtimes to crete
APPLICATION_PATH (optional): Path to the application to be run (default:
current).


This is almost too easy for a development environment...

THIS IS NOT A BENCHMARK BUT even so here is the info...

My laptop with a test app I have and seed data installed thin 1.0 and Glassfish...

With threading:
============
Requests per second: Thin 10 [#/sec]
Requests per second: Glassfish 14 [#/sec]

Without threading:
==============
Requests per second: Thin 3 [#/sec]
Requests per second: Glassfish 2.6 [#/sec]

:) Not what I was hoping for ...

Mac(OSX) Jruby and Rails 2.2

Not Sure why but I wanted to try JRuby and Rails(2.2) new threads...

These are the steps I used to get a application on my mac running with Rails 2.2 and JRuby 1.1.3(I used ports to install JRuby thats why 1.1.3).

Here are the steps:

- First Install JRuby .. # sudo port install jruby

- Fix an annoying warning .. # sudo jruby -S gem install jruby-openssl

- Install rails .. # sudo jruby -S gem install rails

- Install the database connector .. # sudo jruby -S gem install activerecord-jdbc-adapter
  • We Also need to download the mysql java (JDBC) driver
  • Copy mysql-connector-java-x.x.x-bin.jar to $JRUBY_HOME/lib. (/opt/local/share/java/jruby)
  • http://dev.mysql.com/downloads/connector/j/5.1.html

- Upgrade the gem environment .. # sudo jruby -S gem update

- Install latest RubyGems (Rails 2.2 Needs 1.3.1 or higher.. ) .. # sudo jruby -S gem install rubygems-update

- Now you have to force install the update .. # sudo jruby -S update_rubygems

- Install Rails 2.2.1 .. # jruby -S gem install rails -s http://gems.rubyonrails.org

# jruby -S rails myapp
# cd myapp
# vi config/database.yml
  development:
adapter: jdbc
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/testapp_development
username: root
password:
# vi config/environment.rb
Add this code:

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
if RUBY_PLATFORM =~ /java/
require 'rubygems'
RAILS_CONNECTION_ADAPTERS = %w(jdbc)
end
# jruby script/server
=> Booting WEBrick...
=> Rails 2.2.1 application started on http://0.0.0.0:4000
=> Ctrl-C to shutdown server; call with --help for options
[2008-11-17 16:40:34] INFO WEBrick 1.3.1
[2008-11-17 16:40:34] INFO ruby 1.8.6 (2008-11-17) [java]
[2008-11-17 16:40:34] INFO WEBrick::HTTPServer#start: pid=13484 port=4000


DONE!

Can't access my VPS via ssh ??

This was the errors message:

PTY allocation request failed on channel 0

Searching around on the internet I found that there is a device ptmx that might be missing with this error message. SO I did the following:

[root]# ls -l /dev/ptmx
ls: /dev/ptmx: No such file or directory


So is that the problem? hmm, I recreated the device:

[root]# /sbin/MAKEDEV -d /dev ptmx


and restarted sshd and Now everything works again!!

Thursday, November 13, 2008

Mysql5 in OSX

Install mac ports

Install Mysql:
freddy@new-host-2:~$ port list | grep mysql5
mysql5 @5.0.67 databases/mysql5
mysql5-devel @5.1.28-rc databases/mysql5-devel
freddy@new-host-2:~$ port install mysql5


  • First run this command in your terminal to install the MySQL 5 package:

    sudo port install mysql5 +server
  • Second load the MySQL server by running the following command: (Note: Mac OS X Tiger and above use launchd for starting programs, and this command is worth getting familiar with. In this case the startup configuration file was created for us when we installed MySQL 5.)

    sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
  • Third figure out the path for your server socket. It should be /opt/local/var/run/mysql5/mysqld.sock but verify this by running this command:

    mysql_config5 --socket
    /opt/local/var/run/mysql5/mysqld.sock
  • Fourth you want to create a shortcut to the MySQL socket so that PHP, Ruby on Rails, Python, and your other languages can access it. These commands should make MySQL work for PHP and Ruby on Rails:

    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock
    sudo mkdir /var/mysql
    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /var/mysql/mysql.sock
  • Fifth you will need to shut down the MySQL server for now. Run this command:

    sudo launchctl unload -w /Library/LaunchDaemons/org.macports.mysql5.plist
  • Sixth you need to setup the root user and the default MySQL database. Sometimes called the grant tables. To do this run this command:

    sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql
  • And last you’ll need to start up the MySQL server and then login and change the root password.

    sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
    mysql5 -u root
    UPDATE mysql.user SET Password = PASSWORD('password') WHERE User = 'root';
    FLUSH PRIVILEGES;
    quit
  • Now you should be able to login using your new password.

    mysql5 -u root -p

That’s it you’re all setup with MySQL on Mac OS X.