Installing Glassfish 2 on Ubuntu
Today I decided to install Glassfish on my Ubuntu 7.10 box. It had come to my attention that Glassfish was a standard package for Ubuntu, so I started the package manager and checked Glassfish for installation. All went well until I found out Glassfish 1 was just installed.
Bummer. I had assumed version 2 was packaged. I undid the installation together with the dependencies.
I don’t really like installing Linux software without the standard package manager, but I decided to make an exception for Glassfish. Below I describe my findings.
My requirements for the Glassfish installation are:
- it must run as a non privileged system user
- it must start before Apache and after MySQL. Apache links to Glassfish and Glassfish uses MySQL. Shutdown must be the exact opposite.
Here is how I achieved those requirements:
1. Preparation
- Download the binary most recent stable binary build for linux from the Glassfish site. I downloaded version 2 UR 1.
- Make sure you have installed a java jdk. I use the sun jdk 6. You can install it like this:
sudo apt-get install sun-java6-jdk
- Create a system user called glassfish:
sudo adduser --system glassfish
2. Installation
- The unpack and installation must be run as the newly created glassfish user. First copy the installation file (in my case glassfish-installer-v2ur1-b09d-linux.jar) to /tmp. After that unpack the installation file as the glassfish user. I use /tmp, because the glassfish user has write permissions there. You could use any directory that the glassfish user has write permissions to.
sudo -u glassfish java -Xmx256M -jar glassfish-installer-v2ur1-b09d-linux.jar
- Move the glassfish directory to opt:
sudo mv glassfish /opt
- To be able to traverse the glassfish directory without root privileges, you must change the group of all files in the glassfish directory from nogroup to admin.:
sudo chgrp -R admin /opt/glassfish/
3. Configuration
Glassfish comes with a configuration script, which creates a server instance. Next are the steps to run that script.
- Go to the glassfish directory with the installation:
cd /opt/glassfish/
- Glassfish comes with a custom version of ant. Make the ant scripts executable:
sudo chmod -R +x lib/ant/bin/
- Run the configuration script:
sudo -u glassfish lib/ant/bin/ant -f setup.xml
This should end with the message: “BUILD SUCCESSFUL”
After this, Glassfish is installed. You can test your installation by starting Glassfish in the console window:
sudo -u glassfish bin/asadmin start-domain domain1
You can open a browser and go to the admin-console at http://localhost:4848/. Login as user “admin” and password “adminadmin”.
If this all works: congratulations. You have successfully installed Glashfish!
Now we need to make sure Glassfish starts when the computer boots up and that it shuts down when the computer shuts down.
- With an editor, create the file /etc/init.d/glassfish (e.g. by
sudo gedit /etc/init.d/glassfish) with the following contents:
#! /bin/shGLASSFISHPATH=/opt/glassfish/bin
case “$1″ in
start)
echo “starting glassfish from $GLASSFISHPATH”
sudo -u glassfish $GLASSFISHPATH/asadmin start-domain domain1
;;
restart)
$0 stop
$0 start
;;
stop)
echo “stopping glassfish from $GLASSFISHPATH”
sudo -u glassfish $GLASSFISHPATH/asadmin stop-domain domain1
;;
*)
echo $”usage: $0 {start|stop|restart}”
exit 3
;;
esac
:
- Make the init script executable:
sudo chmod a+x /etc/init.d/glassfish
- Test the script by stopping the currently running server:
sudo /etc/init.d/glassfish stop
- After that, try starting it with the init script:
sudo /etc/init.d/glassfish start
- If all goes well you can set the init script up for automatic startup and shutdown:
sudo update-rc.d glassfish defaults 90 10
When you start the computer the next time, Glassfish will be running with the privileges of the glassfish user.
Have fun with Glassfish!
4. Optional components
Below are some things you probably need, depending on your requirements.
4.1. MySQL
I will not discuss a complete MySQL installation here, but only the installation of the MySQL jdbc driver. Install the jdbc driver as follows:
sudo apt-get install libmysql-java
Next, create a symbolic link in the lib directory of Glassfish to an existing symbolic link to the most recent MySQL jdbc driver:
sudo -u glassfish ln -s /usr/share/java/mysql-connector-java.jar /opt/glassfish/lib/
4.2. Using awt classes
I have a web application that manipulates some pictures with the use of some awt classes. Unfortunately, that resulted in some nasty exceptions on my linux box. After some research, I found out that it was caused by the fact that the linux version of java uses X11 for the awt classes. A fix for the exceptions is using a certain jvm option:
-Djava.awt.headless=true
You can add this option for Glassfish in the admin console under Application Server->JVM Settings->JVM Options.







