How to setup Glassfish server for Java web app deployment

How to setup Glassfish server for Java web app deployment

Recently I had to locally run a JavaEE web app so I can contribute to the project. However, it was my first time trying to run a Java web app locally, or even remotely. I know it was easy to deploy it using NetBeans because it has been developed with NetBeans and Glassfish. However, I wanted to run it without NetBeans because I don’t have NetBeans setup on my computer and I NetBeans currently doesn’t have the native M1 support.

After some googling and reading though hours of different tutorials, StackOverflow answers, troubleshooting various problems I was able to setup the web app on my computer.

Now that I got it working, I decided to write this post combining everything so that someone who wants to deploy a Java web app on Glassfish remotely or locally can get some idea on how to do it.

I’ll be writing on how to deploy it on a remote server (a DigitalOcean Droplet) but you can follow the same steps for deploying it on your localhost.

Setup a LAMP server

If your web app is using MySql, it’s easy to get started with a LAMP server (Linux, Apache, MySql, PHP).

When you create a new droplet on DigitalOcean you can easily select a LAMP server from their market place. It install the current stable Ubuntu LTS version, and automatically open up port 80 for web traffic and install MySql and setup a user and you can manipulate the database with it.

You can get more details about the LAMP server on DigitalOcean here – https://marketplace.digitalocean.com/apps/lamp

I don’t think Apache and PHP is necessary, and you can use a different database management system instead of MySql. But it’s easy to set up a LAMP server, on DigitalOcean on a computer, but it’s not a must. Simply having Ubuntu or any other Linux distro would be enough.

Install Java, JDK

I ran into a small problem at this point. There are different “branches” of GlassFish available out there. If you search on Google you will find the following page as its top result. https://javaee.github.io/glassfish/. However, this branch hasn’t been updated since 2019, and you can download GlassFish version 5 from the following website.

The latest update of GlassFish are available on Eclipse foundation website – https://projects.eclipse.org/projects/ee4j.glassfish/downloads

On the Eclipse foundation website you will be able to download the newest version of GlassFish which is GlassFish 6.2 at the time of writing.

GlassFish 5 only supports JDK 1.8, Java 8 and will not work on the latest Java version at the time of writing which is Java 11.

GlassFish 6 on the other hand will support JDK 1.11, Java 11.

Depending on the version of the GlassFish that you are going to use, install Java 8 or Java 11. You can do it with the following commands.

sudo apt update
sudo apt install openjdk-11-jdk
java -version

This should show your Java version as follows.

openjdk version "11.0.7".....

If you are using GlassFish 5, install Java version 8.

sudo apt update
sudo apt install openjdk-8-jdk
java -version

And your Java version should be like this

openjdk version "1.8.0_191"....

Install Glassfish

Now it’s time to install Glassfish, depending on the version of Java that you installed, install the appropriate Glassfish version.

If you are installing Glassfish 5

wget http://download.oracle.com/glassfish/5.0.1/release/glassfish-5.0.1-web.zip

If you are installing Glassfish 6

wget https://download.eclipse.org/ee4j/glassfish/web-6.2.0.zip

Unzip your Glassfish download. I suggest you unzip it to the /opt/ folder

sudo mv web-6.2.0.zip /opt/
cd /opt/
sudo unzip web-6.2.0.zip

If unzip ins not install you can install unzip on Ubuntu like this, if it’s not already installed,

sudo apt update
sudo apt install unzip

Setting up Glassfish

Create a new service file

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

Enter the following details to your Glassfish.service file and make sure to use the path where you extracted the Glassfish zip file. The below file assumes that you have used Glassfish version 5 and you have extracted Glassfish zip file to /opt/ folder.

If you are using Glassfish 6 change glassfish5 to glassfish6

[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 the file and reload the system services.

sudo systemctl daemon-reload

Enable Glassfish at boot

sudo systemctl enable glassfish

To start Glassfish

sudo systemctl start glassfish

To stop Glassfish

sudo systemctl stop glassfish

Restart Glassfish

sudo systemctl restart glassfish

After starting Glassfish server you will be able to access though the following address

http://<your-digital-ocean-ip>:8080
http://localhost:8080

You can also access Glassfish admin console by visiting the port 4848

http://<your-digital-ocean-ip>:4848
http://localhost:4848

However, you won’t be able to login to the admin console unless you are running it on your localhost. If you are running it on a DigitalOcean Droplet, you won’t be able to access the admin console without enabling secure admin.

If you are accessing admin console the default username is admin. Leave the password field blank.

Open ports 8080 and 4848

If you are running a DigitalOcean Droplet, usually your 8080 and 4848 ports are not open. You can open them by running the following commands.

ufw allow 8080
uff allow 4848

Glassfish enable-secure-admin

You can enable secure-admin by running the following command.

/opt/glassfish5/bin/asadmin enable-secure-admin

However, I came across this problem where after enabling secure admin I was unable to run any command though the asadmin admin command. I got a security error. This was probably because some certificate key mismatch, and I was unable to find a solution for it.

However, if you are using this as a development server, which in my case I’m using it for development purposes, or if you are using it on your localhost, then you don’t have to enable secure admin.

Add Glassfish to your path

You can add Glassfish to your path by using the following command. It will make it possible for you to run Glassfish from (asadmin) anywhere.

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

If you did everything correctly, you should be able to see the Glassfish welcome page by visiting your remote server IP/localhost on port 8080.

This is a basic introduction on how to setup a Glassfish server to deploy a Java web app. Since this post is relatively long, I will write another post on how you can deploy a Java web app on your newly setup Glassfish server.