XMPP4R and setting a VCard in Ruby on Rails

By Nick | July 13, 2007

Setting a VCard using the XMPP4r library wasn’t as straightforward as I thought it would be. Below is the code to do it:

#connect and authenticate to the server
@client = Client::new(@jid)
@client.connect
@client.auth(@pwd)

#get the vcard of the connected jid and fill it with new values
vcard = Jabber::Vcard::Helper.new(@client).get()
@vcard = Vcard::IqVcard.new
@vcard["FN"] = "test"
@vcard["NICKNAME"] = "test"
@vcard["BDAY"] = "15-07-1954"
@vcard["ADR/CTRY"] = "Belgium" #this is a tricky one
@vcard["NOTE"] = "Anything, you want to say."

It is best to use a Jabber IM client like Psi to initially set the vcard and see how the resulting xml looks like. Otherwise you wont find out the names of the fields (who on earth thought FN was a good name??).

Topics: Rails | No Comments »

Faceted search in Solr with Ruby on Rails

By Nick | July 13, 2007

Solr is a powerful, easy to use search framework build on top of Lucene. It currently powers the search of my RailsHostingInfo.com page. And it makes the nice faceting (grouping of data) possible. Recently I got the question how to do it with acts_as_solr. Well the truth is, I don’t know. I haven’t used acts_as_solr. But this is what I got from the rdocs at http://acts-as-solr.rubyforge.org/. You should use find_with_facet, like so, with facets obviously being your categories:

Electronic.find_with_facet “memory”, :facets => {:query => [”price:[* TO 200]”,
“price:[200 TO 500]”,
“price:[500 TO *]”],
:fields => [:category, :manufacturer]}

For my purposes (using Solr 1.1) I add the following parameters to the query url: “&facet=true&facet.field=plan_type&facet.field=company_server_location&facet.zeros=false”

For more information about using facets in Solr, take a look at the Solr wiki: http://wiki.apache.org/solr/SimpleFacetParameters. That gives you all the details about facets in Solr.

Topics: Rails | No Comments »

Haml and Rails rendering speed

By Nick | July 13, 2007

I was shocked when I read that the new 1.7 release of Haml now comes with speed. Haml 1.7 is now only 1.8 times slower than ERB. And I thought ERB was one of the slowest things in Rails. Check out http://www.kuwata-lab.com/erubis/ for different rendering speeds and notice that ERB is the slowest of them all. So saying that Haml now comes with speed is a nice euphemism. If you need more rendering speed I advice you to use Erubis (http://www.kuwata-lab.com/erubis/). The only thing you need to do to install it is:

gem install erubis

and put - require ‘erubis/helpers/rails_helper’ - in your environment.rb file.

Topics: Rails | 2 Comments »

Rails 1.2 doesn’t support composite primary keys

By Nick | July 13, 2007

Rails 1.2 doesn’t support composite primary keys out of the box!! Well, that certainly proves there is too much hype around Rails. I love it and it is really easy to build web applications in it. But no composite primary keys support, ouch. Luckily there are many plugins/extensions, so check out http://compositekeys.rubyforge.org/, if you need them. And it works after some configuration.

Topics: Rails | No Comments »

How to get Ruby OpenSSL to work on CentOS

By Nick | July 13, 2007

I recently needed the Ruby OpenSSL library to do some decoding and encoding. I had tested everything on Windows and there it worked without a glitch. But after moving my code to Linux CentOS it stopped working. I got the error: LoadError: no such file to load — openssl

After much searching I found the solution. This only works when you have installed Ruby from the source and is only tested with Ruby 1.8.4 (but probably also works with newer versions).

First you need to make sure openssl en openssl-devel are installed (yum list | grep openssl). If not, install them using yum install openssl and yum install openssl-devel. Both libraries are needed to compile the Ruby OpenSSL code. After that, locate the openssl folder in the Ruby source files (ruby-1.8.4/ext/openssl) and make sure you are in the folder, when you run the following commands:

ruby extconf.rb
make
make install

make install should write the compiled libraries to your Ruby folder. Open irb to check if it worked.

irb(main):001:0> require ‘openssl’
=> true

If it returns true you are good to go.

Topics: Rails | No Comments »

Setting up a Ruby on Rails server - Part 6 - Axigen easy mail server

By Nick | June 5, 2007

I was looking for an easy way to install my own mail server. I had looked at Postfix, Exim, Sendmail. But installing those mail servers isn’t enough you have to make sure that you at least integrate it with SpamAssassin (spam filter) and ClamAV (virus scanner). And once everything is installed you have to maintain it. And there aren’t so many great tools around to do it. After reading various tutorial, installing them, configuring them and making them work together seemed a daunting task. So I searched Google for an easy to install and easy to administrate mail server. I found Axigen (http://www.axigen.com): an easy to install, easy to configure and even easier to maintain webserver. There is a free version available, but it signs every mail you send or receive, which gets a bit irritating. There support is excellent, even if you use their free version.

How to install Axigen:
Download Axigen van www.axigen.com
rpm -ivh axigen…
yum install compat-libstdc++-33 (needed by Axigen)

#define extra repository for clamav-server, which isn’t in the normal CentOS repositories
# cat /etc/yum.repos.d/kbsingh-CentOS-Extras.repo
[kbs-CentOS-Extras]
name=CentOS.Karan.Org-EL$releasever - Stable
baseurl=http://centos.karan.org/el$releasever/extras/stable/$basearch/RPMS/
gpgcheck=1
gpgkey=http://centos.karan.org/RPM-GPG-KEY-karan.org.txt
enabled=1

Before starting clamav, run freshclam to update the virus db.

Install Axigen by following their installation procedure.

After you have installed Axigen, you should check it isn’t configured as a relay server. Go to http://www.abuse.net/relay.html - and enter your IP address. If everything is correct you have configured your own secure mail server.

Topics: Rails | No Comments »

Setting up a Ruby on Rails server - Part 5 - Java - Tomcat - Ant

By Nick | April 4, 2007

Some of you may think, why on earth or you using Java? Well, I use every language program that looks good to me. PHP to run this blog, Rails to develop my own sites and Java because it has an awesome search engine Lucene and on top some nice icing in the form of Solr.

In order to get Solr running you need to have Java, Tomcat and Ant installed first.

Java installation:

Download the latest Java jdk_xx.bin file
chmod +x jdk_xx.bin #to make it executable
./jdk_xx.bin

Now you make sure that all the paths are correct. I added the following lines to /etc/profile to do it (also added path for Tomcat and Ant), maybe there is a cleaner method for it.

export PATH=/usr/java/jdk_xx/bin:/usr/local/ant/bin:$PATH
export JAVA_HOME=/usr/java/jdk.xx/
export CATALINA_HOME=/usr/local/tomcat
export ANT_HOME=/usr/local/ant

Now you should restart your server to see if JAVA_HOME is still set. Run java -v to see if you get your Java version back. If you do, it means Java is installed correctly. Now on to Tomcat.

Tomcat installation:

- /usr/sbin/adduser tomcat #add a tomcat user
chown -R tomcat /usr/local/tomcat
- wget http://apache.scarlet.be/tomcat/tomcat-5/v5.5.17/bin/apache-tomcat-5.x.x.tar.gz #download latest Tomcat version
tar -zxvf apache-tomcat-5.x.x.tar.gz
- mv apache-tomcat-5.x.x /usr/local/tomcat

Now we have to make sure Tomcat starts up when the server goes down unexpectedly, so lets make a Tomcat startup script.

Create the file /etc/rc.d/init.d/tomcat and place the following in it:

#!/bin/bash
#
# Startup script for Tomcat

start_tomcat=/usr/local/tomcat/bin/startup.sh
stop_tomcat=/usr/local/tomcat/bin/shutdown.sh

start() {
echo -n "Starting tomcat: "
su -l tomcat -c ${start_tomcat}
echo "done."
}
stop() {
echo -n "Shutting down tomcat: "
su -l tomcat -c ${stop_tomcat}
echo "done."
}

# See how we were called
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 10
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac

exit 0

Now make the file executable and place it in the right directories:


chmod +x tomcat
cd /etc/rc3.d
ln -s /etc/init.d/tomcat S71tomcat
ln -s /etc/init.d/tomcat K01tomcat

BTW K stands for stopping and S stands for starting and the number is the order in which all the files are executed. So if Tomcat needs to be started before another service at startup make sure it gets a lower S number than the other service.

That’s it for Tomcat. Restart your server and see if Tomcat starts up.

Now the last part is installing Ant, which is also the easiest.

Ant installation:

wget http://apache.scarlet.be/ant/binaries/apache-ant-1.6.x-bin.tar.gz
tar -zxvf apache-ant-1.6.x-bin.tar.gz
mv apache-ant-1.6.x-bin /usr/local/ant

And now you are ready to install Solr or any other Java utility. Next time I’ll give some explanation about my mailserver.

Topics: Rails | 1 Comment »

Setting up a Ruby on Rails server - Part 4 - Litespeed a web server

By Nick | April 2, 2007

First of all why did I choose Litespeed and not Apache or Lighttpd. Well, I first choose Apache + Mongrel to be my web server, but setting it up wasn’t that easy and once I got it running and did some testing on it. It turned out to be a very memory intensive combination. And that was at the time a only had one site and I was planning on expanding. So if only one site is using this many memory, howh much memory will two, three or more sites consume. I wanted to have three Mongrel instances per site, each consuming between 40 and 50 Mb. So I would quickly run out of memory and as they say memory is cheap, I started to look at other possibilities. I had heard of Litespeed a couple of months ago, but that was at the time their site was running on a Nuke version. And well Nuke isn’t known for its security, so I was turned away from it. But just at that time the new version 2.2 of Litespeed came out and there was a blog posting from Bob Silva on http://i.nfectio.us/articles/2006/09/04/litespeed-web-server-2-2-includes-rails-support with two videos demonstrating just how easy it is to setup Litespeed and configure a new Rails site. After seeing both videos I just had to test it. And as it turned out I liked it a lot more than Apache + Mongrel. Main advantages of Litespeed are: easy to administer (setting up a new Rails site is a breeze), very fast, scales very well (doesn’t use up so much memory as Apache + Mongrel did) and very easy to update.

Installation (note I’m using the latest 2.2.x version of Litespeed, you can already download the 3.0.x version, which has a better administration interface)

gem install ruby-lsapi
wget http://litespeedtech.com/packages/2.2/std/lsws-2.2.x-std-i386-linux.tar.gz
unzip Litespeed and ./install.sh
follow the steps from the installer

For security reasons I restricted the access to the admin console:

WebAdmin Configuration > Security > Access Control
Allow: 127.0.0.1
Deny: ALL

With SSH you can tunnel to it and still have access to the admin console. For more information on getting a Rails site up and running look at the videos Bob Silva made, he did a wonderful job.

Some things you need to be aware of. Litespeed runs as the user nobody, so if you have caching turned on in your Rails project you have to make sure Litespeed is able to write to the tmp directory:

chown -R nobody:nobody rails-project-path/tmp (action caching)
chown -R nobody:nobody public/model-name (page caching)

You also need to change the rights of your production.log, otherwise Litespeed is unable to write to it:

chmod 666 /log/production.log

Oh, and another useful tip is, that when you encounter an error and something doesn’t work. Make sure you check lsws/logs/stderr.log, where the more obscure errors are reported.

So now you have setup your web server and you are able to start serving your pages to the world.

Topics: Rails, Litespeed | No Comments »

Setting up a Ruby on Rails server - Part 3 - Installing MySQL

By Nick | March 30, 2007

Today, I’ll explain how you can install MySQL on a CentOS box. You can use the MySQL version that is already installed on CentOS, but it is old. On my system it was 4.1.20, but I wanted to use the 5 series. So first step is to remove the old one:

yum remove mysql

Add some a mysql user and group to run MySQL and make sure the mysql can’t log in:

groupadd mysql (probably already exists)
useradd -s /sbin/nologin -g mysql mysql

Get the MySQL source from the MySQL website and downloaded the latest version. That is probably the biggest issues with installing packages from source, you tend not to upgrade them once they are up and running and your site is in production. If it works, don’t fix it!

Install MySQL and change some file settings:

tar -zxvf mysql-standard-5.0.xx-linux-i686.tar.gz
mv mysql-standard-5.0.xx-linux-i686 /usr/local/mysql
cd /usr/local/mysql
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data
chgrp -R mysql .

Check your MySQL installation:
1. /usr/local/mysql/bin/mysqladmin version
2. /usr/local/mysql/bin/mysql - start the mysql command utility

How to secure MySQL:

cp mysql-dir/support-files/my-large.cnf /usr/local/mysql/data/my.cnf
vi data/my.cnf

You have two possibilities:
1. bind-address = 127.0.0.1 #MySQL wont be accessible from the outside
2. uncomment skip-networking #only connections from localhost are allowed

Both options make sure that nobody from outside your machine is able to connect to your MySQL. You are still able to connect to it using SSH tunneling, I’ll probably explain this very handy option in one of my next posts.

Other security measures are:

You will probably have to restart your server at some point, so it would be handy if MySQL restarted automatically. Here is how you do it:

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --level 345 mysql on

Now the only thing left to do is to make Ruby talk to MySQL by installingen the Ruby MySQL bindings:

gem install mysql — –with-mysql-include=/usr/local/mysql/include/
–with-mysql-lib=/usr/local/mysql/lib/
–with-mysql-config=/usr/local/mysql/bin/mysql_config

I had to modify modify /usr/local/mysql/bin/mysql_config and add “-lmygcc” to the end of the libraries to link. I added it to the end of this line in the script:

libs=" $ldflags -L$pkglibdir -lmysqlclient -lz -lcrypt -lnsl -lm -lmygcc "

Otherwise I got the error ‘unresolved symbol __pure_virtual exists‘. Of course if you don’t get this error you don’t have to change the file.

You can test the Ruby MySQL binding with:

irb
require 'rubygems'
require 'mysql'

This should return true. That’s it, know you should be able to connect your Rails program with MySQL.

Topics: Rails | No Comments »

Just a quick Technorati intermezzo

By Nick | March 30, 2007

Technorati Profile

Topics: Various | No Comments »

« Previous Entries