How to Install GlassFish on Ubuntu 18.04

Install GlassFish on Ubuntu 18

GlassFish is a fully-fledged open-source reference implementation of Java EE application server for developing and deploying Java-based applications. It supports JPA, JSF, JSP/Servlet, JMS, RMI, as well as many other Java-based technologies. It also provides both web and CLI-based administration consoles for easier configuration and management of your Java applications and their respective components.

GlassFish is being maintained and developed through the GlassFish Project. It is licensed under the Common Development and Distribution License (CDDL) as well as the GNU General Public License (GPL). The project is currently sponsored by Oracle.

In this article, we will show you how to install GlassFish on a Linux VPS with Ubuntu 18.04 OS.

Prerequisites:

Make sure that your server meets the following minimum requirements:

  • A VPS with Ubuntu 18.04 running on it (preferably a fresh installation)
  • A user account with root access, or access to the “root” user

Install Updates and Fix Dependencies

Log in to your server via SSH:

ssh username@server_ip

Remember to replace “username” with the username which you wish to use on the server, as well as replacing “server_ip” with your server’s IP address.

Before starting with the GlassFish Server installation, it is a good idea to update the system packages to their latest versions, if any are available:

sudo apt-get update
sudo apt-get upgrade

Install software package dependencies needed by GlassFish by running the following command:

sudo apt-get install software-properties-common wget unzip

Installing and Configuring Java 8

Since GlassFish is powered by Java, it must be installed and configured on your Ubuntu server first. As of writing, GlassFish 5.0 only supports Java 8.

This command will install Oracle JDK 8 using the PPA repository:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

After the installation, you can check the active Java version using this command:

sudo java -version

You should see the following message:

java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

Updating the Java Environment Variable

Most Java applications use the variable JAVA_HOME in determining the location of your Java installation.

To check if you already have JAVA_HOME set, type the following command:

echo $JAVA_HOME

If you see a blank line, it means there is no configured JAVA_HOME variable. If there is, you can double check if it is using the correct path by following these next steps.

To determine the current Java location, use the following command:

sudo update-alternatives --config java

The output should show you the Java installation path, like the following:
/usr/lib/jvm/java-8-oracle/jre/bin/java

Now we need to append the Java location to our environment file. Modify the path accordingly based on your Java path:

sudo echo JAVA_HOME=\"/usr/lib/jvm/java-8-oracle/bin/java\" >> /etc/environment

After that, we will need to source the file to update our shell:

source /etc/environment

Now you can verify if JAVA_HOME is set by using either of the following commands:

echo $JAVA_HOME
$JAVA_HOME -version

Installing the Latest GlassFish Server

At the time of writing, the current latest version of GlassFish Server is 5.0.1.

First, we need to change our current directory to /opt:

cd /opt

We can now download the latest nightly build version of GlassFish from their official GlassFish website.

sudo wget http://download.oracle.com/glassfish/5.0.1/nightly/latest-glassfish.zip

Extract the files to the current directory. You can choose a different directory if you wish to keep your installation of GlassFish elsewhere.

sudo unzip latest-glassfish.zip

To extract to a specific directory:

sudo unzip latest-glassfish.zip -d /path/to/desired/directory

Setting Up GlassFish Service

Create the service file:

sudo nano /etc/systemd/system/glassfish.service

Copy and paste the following. Modify the GlassFish path in respect to the directory of your GlassFish install:
[Unit]
Description = GlassFish Server v5.0
After = syslog.target network.target

[Service]
ExecStart=/opt/glassfish5/bin/asadmin start-domain
ExecReload=/opt/glassfish5/bin/asadmin restart-domain
ExecStop=/opt/glassfish5/bin/asadmin stop-domain
Type = forking

[Install]
WantedBy = multi-user.target

Save and exit. Now we need to reload system services:

sudo systemctl daemon-reload

Managing the GlassFish Service

To enable GlassFish at boot:

sudo systemctl enable glassfish

To start GlassFish:

sudo systemctl start glassfish

Check the service status using this command:

sudo systemctl status glassfish

Accessing GlassFish

Once GlassFish has started, you can now access the default page on port 8080.
http://<server-ip-address>:8080

You can access the web administrator console on port 4848.

http://<server-ip-address>:4848

Remember to replace “server-ip-address” with your server’s IP address.

Setting Up the GlassFish Path

GlassFish has a utility called asadmin which accepts commands for setting up GlassFish via a command line. To run the asadmin executable anywhere, execute the following command:

export PATH=/opt/glassfish5/bin:$PATH

Securing the GlassFish Admin Console

The default admin password is blank. To update the admin password, run the following command:

asadmin change-admin-password

Enter admin user name [default: admin]>admin
Enter the admin password> <blank>
Enter the new admin password>
Enter the new admin password again>
Command change-admin-password executed successfully.

After changing the admin password, we now need to enable the “secure admin” feature. It will ask for the credentials we added earlier.

asadmin enable-secure-admin

We must restart GlassFish for the changes to take effect:

sudo systemctl restart glassfish

Testing GlassFish

Now that we have a GlassFish Server that uses secure admin access, we can now test the server by deploying a sample WAR file.

First, we need to download a sample WAR file on domain1’s document root directory:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
cd /opt/glassfish5/glassfish/domains/domain1/docroot/ 
sudo wget https://github.com/dmatej/Glassfish/raw/master/v2/www/downloads/quickstart/hello.war

To deploy, run the following command. It will ask for admin credentials:

asadmin deploy hello.war

After successfully deploying, navigate to:
http://<server-ip-address>:8080/hello

You should see the following message:

Hi, my name is Duke. What’s yours?

And that’s it! You now have a working GlassFish server on your Ubuntu 18.04 VPS.

See related: How to configure GlassFish cluster with automatic load balancing


Of course, you don’t have to install GlassFish on Ubuntu 18.04 if you have use one of our GlassFish Hosting plans. If you do, you can simply ask our support team to install GlassFish on Ubuntu 18.04 for you. They are available 24/7 and will be able to help you with the installation.

PS. If you enjoyed reading this blog post on how to install GlassFish on Ubuntu 18.04, feel free to share it on social networks by using the shortcuts below, or simply leave a comment. Thank you.

2 thoughts on “How to Install GlassFish on Ubuntu 18.04”

  1. I’ve installed Glassfish on many servers – and always forget how to do it.
    Nice simple instructions – worked well

    Reply

Leave a Comment