Skip to main content

Installing GlassFish and PostgreSQL on Ubuntu Server Edition

Posted by cayhorstmann on July 18, 2006 at 8:09 AM PDT

??? style="float:left; padding-right: 1em;" />I am getting ready for the fall
semester. My software engineering students will be using href="http://glassfish.dev.java.net/">GlassFish for their projects. In
order to avoid the "but it worked on my computer" syndrome, I make them
deploy their apps on a server in my office. I just installed href="http://www.ubuntu.com/server">Ubuntu Server Edition. href="http://www.howtoforge.com/perfect_setup_ubuntu_6.06http://www.howtoforge.com/perfect_setup_ubuntu_6.06">These
instructions got me started with the Linux basics. This blog has the
(not so) gory details for installing Java, GlassFish, and PostgreSQL. At
the end are the obligatory reflective comments.

I ran all these steps as super-user. Note that Ubuntu Server has no GUI
interface by default. I simply put the in a corner, connected with ssh,
and ran the commands in a terminal. Required skills: bash shell basics and
a stone-age text editor such as vi.

Java

  1. Edit /etc/apt/sources.list, uncomment the lines for adding
    universe, and manually add multiverse at the end:
    deb http://us.archive.ubuntu.com/ubuntu/ dapper universe multiverse
    deb-src http://us.archive.ubuntu.com/ubuntu/ dapper universe multiverse
  2. Runapt-get install sun-java5-jdk
  3. Runjava -versionYou should get something like
    java version "1.5.0_06"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
    Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

Wow. That was phenomenally easy.

??? style="float:right; padding-left: 1em;" />GlassFish

  1. Go to and look for the version that you want to download.
  2. Download it with wget http://java.net/download/javaee5/promoted/Linux/glassfish-installer-v2-b09.jarbut
    substitute the correct version number.
  3. Runjava -jar -mx256M glassfish-installer-v2-b09.jar(Again,
    substitute the correct version number.) The installer automatically runs
    in headless mode, without a GUI.
  4. Move the installation somewhere permanent. I want to be able to get
    new versions as they become available without having to change many
    configuration files. I'll put this version into
    /opt/glassfish-v2-b09 and make a symlink
    /opt/glassfish.
    mv glassfish /opt/glassfish-v2-b09
    ln -s /opt/glassfish-v2-b09 /opt/glassfish
  5. Run setup.xml in the usual way:
    cd /opt/glassfish
    /opt/glassfish/lib/ant/bin/ant -f setup.xml
  6. I wanted the default domain to be in a separate directory so that I
    could install new versions. There must be a better way of controlling
    the domain directory, but for now I used a brute-force approach, again
    with symlinks.
    mv /opt/glassfish/domains/domain1 /opt
    ln -s /opt/domain1 /opt/glassfish/domains/domain1
  7. It seems prudent to change the admin password on a public
    server:
    /opt/glassfish/bin/asadmin change-admin-password --user admin
    Please enter the old admin password>adminadmin
    Please enter the new admin password>(password is echoed)
    Please enter the new admin password again>(password is echoed)
    The
    password needs to be at least 8 characters long.
    I then had to
    remove ~/.adminpass and run /opt/glassfish/bin/asadmin
    login
    to regenerate it, but that may have been a fluke.
  8. I want GlassFish to be a service that starts up automatically. There
    are probably better ways to do this, but here is my pedestrian approach.
    I produced a file /etc/init.d/glassfish with this content:
    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides:          glassfish
    # Required-Start:    $local_fs $remote_fs
    # Required-Stop:     $local_fs $remote_fs
    # Default-Start:     2 3 4 5
    # Default-Stop:      S 0 1 6
    # Short-Description: glassfish initscript
    # Description:       A simple initscript for the glassfish app server
    ### END INIT INFO
    #
    # Author:            Cay S. Horstmann (http://horstmann.com)
    #

    set -e

    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/glassfish/bin
    DESC="Java EE5 App Server"
    NAME=glassfish
    ASADMIN=asadmin
    PIDFILE=/var/run/$NAME.pid
    SCRIPTNAME=/etc/init.d/$NAME

    # Gracefully exit if the package has been removed.
    test -x $DAEMON || exit 0

    # Read config file if it is present.
    #if [ -r /etc/default/$NAME ]
    #then
    #       . /etc/default/$NAME
    #fi

    #
    #       Function that starts the daemon/service.
    #
    d_start() {
            $ASADMIN start-domain \
                    || echo -n " already running"
    }

    #
    #       Function that stops the daemon/service.
    #
    d_stop() {
            $ASADMIN stop-domain \
                    || echo -n " not running"
    }

    case "$1" in
      start)
            echo -n "Starting $DESC: $NAME"
            d_start
            echo "."
            ;;
      stop)
            echo -n "Stopping $DESC: $NAME"
            d_stop
            echo "."
            ;;
      reload|restart|force-reload)
            echo -n "Restarting $DESC: $NAME"
            d_stop
            sleep 10
            d_start
            echo "."
            ;;
      *)
            echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
            exit 3
            ;;
    esac

    exit 0
    No, I don't know what I am doing. I just modified
    /etc/init.d/skeleton. Then I ran update-rc.d glassfish defaults
  9. At this point, you should be able to reboot the machine and point a
    browser to http://machinename:4848. The admin interface should
    come up.
    ??? style="padding-top: 1em;" />

PostgreSQL

  1. Runapt-get install postgresql-8.1 postgresql-client-8.1
  2. Change the password:
    su postgres
    psql
    ALTER USER postgres WITH PASSWORD 'secret';
    \q
    exit
  3. Go to href="http://jdbc.postgresql.org/download.html">http://jdbc.postgresql.org/download.html
    and pick the most recent driver. Download it as wget http://jdbc.postgresql.org/download/postgresql-8.2dev-503.jdbc3.jar(Change
    the version number...)
  4. Copy the driver into /opt/glassfish/domains/domain1/lib and
    restart the server. Use either asadmin in the usual way or, if
    you installed GlassFish as a service, simply invoke-rc.d glassfish
    restart
    .
  5. Set up a connection pool and a JDBC resource. You can do this
    through the admin interface or directly from the command line. I did the
    latter. Each of the two commands should be on a single line.
    /opt/glassfish/bin/asadmin create-jdbc-connection-pool 
       --datasourceclassname org.postgresql.ds.PGSimpleDataSource
       --restype javax.sql.DataSource
       --property portNumber=5432:password=secret:user=postgres:serverName=localhost:databaseName=postgres
       PostgresPool
    /opt/glassfish/bin/asadmin create-jdbc-resource
       --connectionpoolid PostgresPool
       jdbc/Postgres
  6. ??? style="float:right; padding-left: 1em;" />Try it out. Download the href="http://horstmann.com/elvis/">Elvis quiz app.
    wget http://horstmann.com/elvis/quiz.zip
    mkdir
    quiz
    cd quiz
    jar xvf ../quiz.zip
    Edit build.properties and change the password. Edit
    conf/persistence.xml and specify the data source:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
        <persistence-unit name="defaultPersistenceUnit">
            <jta-data-source>jdbc/Postgres</jta-data-source>
            <properties>
                <property name="toplink.logging.level" value="FINEST"/>
            </properties>
        </persistence-unit>
    </persistence>
    Now run/opt/glassfish/lib/ant/bin/ant run-clientYou
    should get a message such as
    run-client:
         [java] elvis.entity.Quiz[id=26,title=A Java Trivia Quiz,questions={IndirectList: not instantiated}]

The Obligatory Reflective Comments

??? style="float:left; padding-right: 1em;" />First off, apt-get is great.
This was far and away the easiest JDK installation I have ever done. I
didn't have to fuss with the PATH. I'll get alerted when new versions
appear and can update them easily. There is some behind-the-scenes work
that makes Sun's Java the preferred one (rather than GCJ).

I'd love to be able to run apt-get install glassfish.

I never installed much software on a headless server before and I was
pleasantly surprised how easy it was. But if that isn't your cup of tea,
you can use the admin web interface to do most steps.

The Glassfish installer needs to do something about the service script.
Maybe someone can contribute this? It is an open source project, after
all.

Related Topics >>