Saturday, June 8, 2013

Installing Glassfish on Debian

Typical installation process consist of the following steps:

Step 1: To install Glassfish on Debian you have to have installed appropriate JDK. Minimum and certified versions for Glassfish are Oracle's JDK 1.6.0_31 and JDK 7 Update 3. That means these are the minimum versions you want to use.

Download last JDK version. In our case it will be 6.0.45 for x64 machines, eg. jdk-6u45-linux-x64.bin

Extract Oracle's JDK in your home folder:

Typically JDK would be extracted to autocreated directory (in our case jdk1.6.0_45). Move this folder to /usr/lib/jvm.
mv ./jdk1.6.0_45 /usr/lib/jvm/jdk1.6.0_45

Create (or modify) symbolic link java-6-sun in /usr/lib/jvm/
Next modify permisiions
chgrp -R root /usr/lib/jvm/jdk1.6.0_45
chown -R root /usr/lib/jvm/jdk1.6.0_45

If you are replacing previous version make sure environment points to a valid install - update alternatives
update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.6.0_45/bin/java" 1
update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.6.0_45/bin/javac" 1
update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.6.0_45/bin/javaws" 1
update-alternatives --config java

Step 2: Prepare operating system envinronment to install and run Glassfish.
Add a new user called glassfish
adduser --home /home/glassfish --system --shell /bin/bash glassfish

Add a new group for glassfish administration
groupadd glassfishadm

Add your users that shall be Glassfish adminstrators
usermod -a -G glassfishadm root
usermod -a -G glassfishadm my_glassfish_admin

Create and set environment variables JAVA_HOME and AS_JAVA globally for all users (only for bash). Edit /etc/bash.bashrc and append the following lines:
export GLASSFISH_HOME=/home/glassfish/glassfish3
export JAVA_HOME=/usr/lib/jvm/java-6-sun
#we will set this here because it might prevent problems later
export AS_JAVA=/usr/lib/jvm/java-6-sun

Reload bashrc
exec bash

#Eventually you can set JAVA_HOME and AS_JAVA for everyone (best place for setting env vars globally). To do this edit /etc/environment and append the following lines:
#we will set this here because it might prevent problems later

If you dont't have "unzip" installed run this here first
apt-get install unzip

Step 3: Install and run Glassfish Now switch user to the glassfish user we created before (see below)
su glassfish
Change to home dir of glassfish
cd /home/glassfish

Download glassfish Open Source Edition Full Platform
or Web Profile from

Install Glassfish
chmod +x ./
./ -s

Exit to root

Change group of glassfish home directory to glassfishadm
chgrp -R glassfishadm /home/glassfish/glassfish3

Just to make sure: change owner of glassfish home directory to glassfish
chown -R glassfish /home/glassfish/glassfish3

Make sure the relevant files are executable/modifyable/readable for owner and group
chmod -R ug+rwx /home/glassfish/glassfish3/bin/
chmod -R ug+rwx /home/glassfish/glassfish3/glassfish/bin/

Others are not allowed to execute/modify/read them
chmod -R o-rwx /home/glassfish/glassfish3/bin/
chmod -R o-rwx /home/glassfish/glassfish3/glassfish/bin/

Now switch user to the glassfish user
su glassfish

Start glassfish
/home/glassfish/glassfish3/bin/asadmin start-domain domain1
Check the output:

Waiting for domain1 to start .....
Successfully started the domain : domain1
domain  Location: /home/glassfish/glassfish3/glassfish/domains/domain1
Log File: /home/glassfish/glassfish3/glassfish/domains/domain1/logs/server.log
Admin Port: 4848
Command start-domain executed successfully.

Stop glassfish
/home/glassfish/glassfish3/bin/asadmin stop-domain domain1
Check the output:

Waiting for the domain to stop ...
Command stop-domain executed successfully.

Exit from glassfish user

Create file /etc/init.d/glassfish and paste the lines below
#! /bin/sh
#to prevent some possible problems
export AS_JAVA=/usr/lib/jvm/java-6-sun
case "$1" in
echo "starting glassfish from $GLASSFISHPATH"
su - glassfish -c "$GLASSFISHPATH/asadmin start-domain domain1"
$0 stop
$0 start
echo "stopping glassfish from $GLASSFISHPATH"
su - glassfish -c "$GLASSFISHPATH/asadmin stop-domain domain1"
echo $"usage: $0 {start|stop|restart}"
exit 3

Save the file

Add init script to the runlevels, to start glassfish when system starts.
To do this make the init script file executable
chmod a+x /etc/init.d/glassfish

Next configure Glassfish for autostart on ubuntu boot
update-rc.d glassfish defaults

Test if glassfish starts
/etc/init.d/glassfish start

And voila ... it works!

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.