How to Install GlassFish Java Server with Nginx as a Reverse Proxy on Debian 11

GlassFish is an open-source application server used for deploying Java applications. It supports different Java-based technologies including, JPA, JavaServer Faces, JMS, RMI, as well as many other Java-based technologies. It provides a web-based as well as a command-line interface for managing Java applications and their components. GlassFish allows you to create portable and scalable applications that can be easily integrated with legacy technologies.

In this tutorial, I will show you how to install the Glassfish server with Nginx as a reverse proxy on Debian 11.

Prerequisites

  • A server running Debian 11.
  • A valid domain name pointed with your server IP.
  • A root password is configured on the server.

Install Java

Glassfish is a java-based application software so Java must be installed on your server. If not installed you can install it by running the following command:

apt-get install default-jdk unzip -y

Once the Java is installed, you can verify the Java installation using the following command:

java --version

You will get the following output:

openjdk 11.0.13 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-post-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 11.0.13+8-post-Debian-1deb11u1, mixed mode, sharing)

Once you are finished, you can proceed to the next step.

Download Glassfish

First, you will need to download the latest version of Glassfish from the Eclipse website. You can download it using the wget command as shown below:

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

Once the download is completed, extract the downloaded file to the /opt directory:

unzip glassfish-6.1.0.zip -d /opt/

Once you are finished, you can proceed to the next step.

Create a Systemd Service File for Glassfish

Next, you will need to create a systemd service file for managing the Glassfish service. You can create it using the following command:

nano /usr/lib/systemd/system/glassfish.service

Add the following lines:

[Unit]
Description = GlassFish Server v6.1.0
After = syslog.target network.target

[Service]
User = root
ExecStart = /usr/bin/java -jar /opt/glassfish6/glassfish/lib/client/appserver-cli.jar start-domain
ExecStop = /usr/bin/java -jar /opt/glassfish6/glassfish/lib/client/appserver-cli.jar stop-domain
ExecReload = /usr/bin/java -jar /opt/glassfish6/glassfish/lib/client/appserver-cli.jar restart-domain
Type = forking

[Install]
WantedBy = multi-user.target

Save and close the file when you are finished. Then, reload the systemd daemon to apply the changes.

systemctl daemon-reload

Next, start the Glassfish service and enable it to start at system reboot:

systemctl start glassfish
systemctl enable glassfish

You can also verify the Glassfish service using the following command:

systemctl status glassfish

You will get the following output:

? glassfish.service - GlassFish Server v6.1.0
     Loaded: loaded (/lib/systemd/system/glassfish.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-11-07 04:56:16 UTC; 8s ago
    Process: 6018 ExecStart=/usr/bin/java -jar /opt/glassfish6/glassfish/lib/client/appserver-cli.jar start-domain (code=exited, status=0/SUC>
   Main PID: 6037 (java)
      Tasks: 91 (limit: 4679)
     Memory: 343.0M
        CPU: 13.299s
     CGroup: /system.slice/glassfish.service
             ??6037 /usr/lib/jvm/java-11-openjdk-amd64/bin/java -cp /opt/glassfish6/glassfish/modules/glassfish.jar -XX:+UnlockDiagnosticVMOp>

Nov 07 04:56:10 debian11 systemd[1]: Starting GlassFish Server v6.1.0...
Nov 07 04:56:16 debian11 java[6018]: Waiting for domain1 to start .....
Nov 07 04:56:16 debian11 java[6018]: Successfully started the domain : domain1
Nov 07 04:56:16 debian11 java[6018]: domain  Location: /opt/glassfish6/glassfish/domains/domain1
Nov 07 04:56:16 debian11 java[6018]: Log File: /opt/glassfish6/glassfish/domains/domain1/logs/server.log
Nov 07 04:56:16 debian11 java[6018]: Admin Port: 4848
Nov 07 04:56:16 debian11 java[6018]: Command start-domain executed successfully.
Nov 07 04:56:16 debian11 systemd[1]: Started GlassFish Server v6.1.0.

Once you are finished, you can proceed to the next step.

Set Glassfish Admin Password

By default, Glassfish is accessible without any password. So it is a good idea to secure it by setting up an admin password. You can do it by running the following command:

/opt/glassfish6/bin/asadmin --port 4848 change-admin-password

You will be asked to provide the admin username as shown below:

Enter admin user name [default: admin]>admin

Provide the default admin username and press the Enter key. You will be asked to provide the existing admin password:

Enter the admin password> 

Just press the Enter key. You will be asked to set a new admin password as shown below:

Enter the new admin password> Enter the new admin password again>

Set your secured password and hit Enter. You will get the following output:

Command change-admin-password executed successfully.

Next, it is also recommended to enable the HTTPS on Glassfish. You can do it by running the following command:

/opt/glassfish6/bin/asadmin --port 4848 enable-secure-admin

You will be asked to provide your admin username and password to enable the HTTPS:

Enter admin user name>  admin
Enter admin password for user "admin"> 
You must restart all running servers for the change in secure admin to take effect.
Command enable-secure-admin executed successfully.

Finally, restart the Glassfish service to apply the changes:

systemctl restart glassfish

Once you are finished, you can proceed to the next step.

Access GlassFish Web Interface

At this point, Glassfish is installed and running. By default, the Glassfish web interface listens on port 8080 while the admin interface listens on port 4848. First, access the Glassfish web interface using the URL http://your-server-ip:8080. You should see the following page:

GlassFish server is running

Next, access the Glassfish admin interface using the URL https://your-server-ip:4848. You will be redirected to the Glassfish login page:

GlassFish Login

Provide your admin username, password and click on the Login button. You should see the Glassfish admin interface on the following page:

GlassFish Dashboard

Click on the server in the left pane, you should see the following screen:

General server information

From here, you can restart, stop and view the Glassfish logs.

Click on the Nodes in the left pane, you should see the Glassfish node information in the following screen:

Nodes

Configure Nginx as a Reverse Proxy for Glassfish

It is a good idea to configure the Nginx as a reverse proxy to access the Glassfish web interface. So you can access your application without using port 8080.

To do so, first install the Nginx package with the following command:

apt-get install nginx -y

Once installed, create an Nginx virtual host configuration file using the command below:

nano /etc/nginx/conf.d/glassfish.conf

Add the following lines:

upstream glassfish {
  server 127.0.0.1:8080 weight=100 max_fails=5 fail_timeout=5;
}

server {
  listen          80;
  server_name     glassfish.example.com;

  location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://glassfish/hello/;
  }
}

Save and close the file then verify the Nginx for any syntax error using the following command:

nginx -t

If everything is fine, you will get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, restart the Nginx service to apply the changes:

systemctl restart nginx

You can also check the status of the Nginx with the following command:

systemctl status nginx

You should get the following output:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-11-07 05:02:58 UTC; 22s ago
       Docs: man:nginx(8)
    Process: 6790 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 6791 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 6873 (nginx)
      Tasks: 3 (limit: 4679)
     Memory: 5.1M
        CPU: 53ms
     CGroup: /system.slice/nginx.service
             ??6873 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??6875 nginx: worker process
             ??6876 nginx: worker process

Nov 07 05:02:58 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 07 05:02:58 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.

At this point, Nginx is installed and configured to serve the Glassfish web interface. You can now access the Glassfish web interface using the URL http://glassfish.example.com.

Conclusion

Congratulations! you have successfully installed the Glassfish server with Nginx as a reverse proxy on Debian 11. You can now start deploying your Java-based application on the Glassfish server. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

0 Comment(s)

Add comment