Wednesday, November 24, 2010

Install Apache Geronimo (as service) on Ubuntu Linux

Apache Geronimo is an Open Source J2EE server. And it can be installed rather easily on Ubuntu Linux (10.04).

As a reminder, the following instructions presume that one uses its current ubuntu account. For an enterprise environment it is recommended to create a new user account, for example g_user, that will own all the geronimo files and will start and stop the server (will be referred from now on as GERONIMO_OWNER and ${user}) and add this user to the sudoers as needed.


- First step is to install sun java.
sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
sudo apt-get update
sudo apt-get install sun-java6-jdk


- Second step is to download and extract (install) Geronimo:
Download from :
http://geronimo.apache.org/apache-geronimo-v22-release.html
I downloaded the latest stable release with tomcat:
geronimo-tomcat6-javaee5-2.2-bin.tar.gz

Then md5sum it (to gain problem-solving time):
md5sum geronimo-tomcat6-javaee5-2.2-bin.tar.gz
and crosscheck it with the md5 sum on the site.

Extract the archive:
tar -xvzf geronimo-tomcat6-javaee5-2.2-bin.tar.gz

Rename the extracted directory to something smaller:
mv geronimo-tomcat6-javaee5-2.2 geronimo.2.2
From now on when we will refer to the full path of the geronimo.2.2 directory as ${GERONIMO_HOME}.
 
Open .bashrc file:
gedit /home/${user}/.bashrc

Add JAVA_HOME, JRE_HOME and JAVA_OPTS at the end of .bashrc:
export JAVA_HOME=/usr/lib/jvm/java-6-sun   
export JRE_HOME=/usr/lib/jvm/java-6-sun/jre
export JAVA_OPTS="-Xmx256m -XX:MaxPermSize=128m"
Run .bashrc (just this once):
. .bashrc
Or close the terminal and open a new one.

- Last step is to start the server:
cd ${GERONIMO_HOME}/bin
./geronimo start

And the server is up and running!
You can browse the Administration console at
http://localhost:8080/
Click the link "Administration -> console" on the left.
Provide the default credentials :
system/manager
And geronimo is at your disposal!

To maximize security, it is time to change the default admin username and password. You need to stop geronimo:
cd ${GERONIMO_HOME}/bin
./geronimo.sh stop --user system --password manager
Open user/passwords file
gedit ${GERONIMO_HOME}/var/security/users.properties
and add a new line with the new administrator user and his password like this:
g_admin=g_admin_pass
Next time you start the server the password will be automatically encrypted.
Open permission/groups file:
gedit ${GERONIMO_HOME}/var/security/groups.properties
Assign administration and monitoring privileges only to the new user. Change the two uncommented lines like this:
admin=g_admin
monitor=g_admin
Lastly you should change the file permissions on these files:
chmod -R 600 ${GERONIMO_HOME}/var/security

To create a service in order to automate startup open/create the following file:
sudo gedit /etc/init.d/geronimo2
Add the following code. You only need to set correctly for your system the variables GERONIMO_HOME, GERONIMO_OWNER, GERONIMO_ADMIN and GERONIMO_PASS inside the script.

#!/bin/bash
# chkconfig: 234 20 80
# description: Geronimo Server start/stop script
# processname: geronimo2

JAVA_HOME=/usr/lib/jvm/java-6-sun
JRE_HOME=/usr/lib/jvm/java-6-sun/jre
JAVA_OPTS="-Xmx256m -XX:MaxPermSize=128m"

GERONIMO_HOME=/home/gfuser/geronimo.2.2
GERONIMO_OWNER=g_user
GERONIMO_ADMIN=g_admin
GERONIMO_PASS=g_admin_pass

export JAVA_HOME JRE_HOME JAVA_OPTS GERONIMO_HOME GERONIMO_OWNER GERONIMO_ADMIN GERONIMO_PASS

start() {
  geronimo_state=`ps -ef | grep "geronimo" | grep "/bin/server.jar" | grep -v grep | wc -l`
  if [ $geronimo_state -gt 0 ]; then
    echo "Geronimo is already UP!"
  else 
    echo -n "Starting geronimo: "
    cd $GERONIMO_HOME/bin
    su $GERONIMO_OWNER -c "$GERONIMO_HOME/bin/geronimo.sh start"
    sleep 20
    echo "done."
  fi
}

stop() {
  geronimo_state=`ps -ef | grep "geronimo" | grep "/bin/server.jar" | grep -v grep | wc -l`
  if [ $geronimo_state -gt 0 ]; then
    echo -n "Gracefully stoping geronimo: "
    su $GERONIMO_OWNER -c "$GERONIMO_HOME/bin/geronimo.sh stop --user $GERONIMO_ADMIN --password $GERONIMO_PASS"
    sleep 10
    echo "done."
  else
    echo "Geronimo is already DOWN!"
  fi
}

force_stop() {
  geronimo_state=`ps -ef | grep "geronimo" | grep "/bin/server.jar" | grep -v grep | wc -l`
  if [ $geronimo_state -gt 0 ]; then
    echo -n "Forcefully stoping geronimo: "
    su $GERONIMO_OWNER -c "$GERONIMO_HOME/bin/geronimo.sh stop --force --user $GERONIMO_ADMIN --password $GERONIMO_PASS"
    echo "done."
  else
    echo "Geronimo is already DOWN!"
  fi
}

status() {
  geronimo_state=`ps -ef | grep "geronimo" | grep "/bin/server.jar" | grep -v grep | wc -l`
  if [ $geronimo_state -gt 0 ]; then
    echo "Geronimo is UP!"
  else
    echo "Geronimo is DOWN!"
  fi
}

case "$1" in
  start)
    start
;;
  stop)
    stop
;;
  status)
    status
;;
  force_stop)
    force_stop
;;
  restart)
    stop
    sleep 2
    start
;;
  *)
  echo "Usage: $0 {start|stop|force_stop|restart|status}"
esac

exit 0

Now you need to change the file permissions:
sudo chmod 755 /etc/init.d/geronimo2

Next create the upstart conf file:
sudo gedit /etc/init/geronimo2.conf
and add the following code

# geronimo2

start on runlevel [2345]
stop on runlevel [16]

respawn

pre-start exec /etc/init.d/geronimo2 start
post-stop exec /etc/init.d/geronimo2 stop


Reload upstart configuration:
sudo initctl reload-configuration
Start geronimo service :
sudo initctl start geronimo2

And the geronimo service is securely installed and will be started on every boot.

p.s.
You can find me on fiverr for more personalized requests on any java app server configuration problem or java error that you encounter, with deliverance of less than a day (true!) and money back guarantee if not satisfied.

Sunday, November 21, 2010

GlassFish 2 Cluster Configuration on the Same Machine

This tutorial will guide you through a GlassFish 2.1.1 Cluster with two instances on the same machine. We are going to use the CLI interface to make our life simpler.
[Whenever you encounter the \ symbol in one of the commands, it means that the command continues on the following line. So you have to copy both lines (including the \ symbol) at your terminal.]
After a successful installation of glassfish with cluster profile (setup-cluster.xml), as explained on the download page:

https://glassfish.dev.java.net/downloads/v2.1.1-final.html

Go to directory ${GF_HOME}/bin and delete default domain domain1 (or you can just ignore it).
asadmin delete-domain domain1
and create a new domain with adminport 4000, name das :
asadmin create-domain --adminport 4000 --profile cluster das
Use the following credentials (or your own, just make sure to make the changes on the password file later on)
Admin user : admin
Admin pass : admin111
Master pass : admin111
We are going to use a password file inside ${GF_HOME}/bin in order to make the steps quicker with the following three lines :
AS_ADMIN_ADMINPASSWORD=admin111
AS_ADMIN_PASSWORD=admin111
AS_ADMIN_MASTERPASSWORD=admin111
You should better change the file preferences so that no one else can view this file except the GF admin:
chmod 600 passwords
First step is to start the new domain:
asadmin start-domain --user admin --passwordfile passwords das
Create the first node agent, connecting on DAS at 4000 port, (and then start it):
asadmin create-node-agent --user admin --port 4000 \
   --passwordfile passwords agent1
asadmin start-node-agent --user admin --passwordfile passwords agent1
Create the cluster farm named cl1 (connecting on DAS at 4000 port) that will group both of the clustered instances:
asadmin create-cluster --user admin --passwordfile passwords \
   --port 4000 cl1
Create the first instance (gf1 on http port 9080) and the second instance (gf2 on http port 9090) that communicate to the domain (DAS on port 4000) through the node agent (agent1) and reference our cluster (cl1):
asadmin create-instance --user admin --passwordfile passwords \
   --port 4000 --nodeagent agent1 --systemproperties \
   HTTP_LISTENER_PORT=9080 --cluster cl1 gf1
and
asadmin create-instance --user admin --passwordfile passwords \
   --port 4000 --nodeagent agent1 --systemproperties \
   HTTP_LISTENER_PORT=9090 --cluster cl1 gf2
Last step is to start the cluster and the instances with the following command:
asadmin start-cluster --user admin --port 4000 \
   --passwordfile passwords cl1
Now when you point your browser to http://localhost:4000/ and login as admin/admin111 you will see on your left the "Clusters" Task, with cl1 child. And cl1 will have two children gf1 and gf2. To deploy an application to the cluster, all you need to do is remove the target "server" and add the target "cl1" (either at deployment or after deployment).
You will also see the "Node Agents" Task that has the agent1 node agent.
Here are the startup commands :
asadmin start-domain --user admin --passwordfile passwords das
asadmin start-node-agent --user admin --passwordfile passwords agent1
asadmin start-cluster --port 4000 --passwordfile passwords cl1
and the shutdown commands:
asadmin stop-cluster --user admin --passwordfile passwords cl1
asadmin stop-node-agent agent1 
asadmin stop-domain das


p.s.
Here are all the commands one after another in order to run them as a script:

asadmin create-node-agent --user admin --port 4000 --passwordfile passwords agent1
asadmin start-node-agent --user admin --passwordfile passwords agent1
asadmin create-cluster --user admin --passwordfile passwords --port 4000 cl1
asadmin create-instance --user admin --passwordfile passwords --port 4000 \
  --nodeagent agent1 --systemproperties HTTP_LISTENER_PORT=9080 --cluster cl1 gf1
asadmin create-instance --user admin --passwordfile passwords --port 4000 \
  --nodeagent agent1 --systemproperties HTTP_LISTENER_PORT=9090 --cluster cl1 gf2
asadmin start-cluster --user admin --port 4000 --passwordfile passwords cl1
And the shutdown-delete commands in order to clean up your installation.

asadmin stop-cluster --user admin --passwordfile passwords cl1
asadmin stop-node-agent agent1 
asadmin delete-instance --user admin --passwordfile passwords --port 4000 gf1
asadmin delete-instance --user admin --passwordfile passwords --port 4000 gf2
asadmin delete-cluster --port 4000 --user admin --passwordfile passwords cl1
asadmin delete-node-agent agent1
asadmin delete-node-agent-config --port 4000 --user admin --passwordfile passwords agent1
asadmin stop-domain das
asadmin delete-domain das


p.s.
You can find me on fiverr for more personalized requests on any java app server configuration problem or java error that you encounter, with deliverance of less than a day (true!) and money back guarantee if not satisfied.