How to install GlassFish Java Server using Nginx as a reverse proxy on Debian 11

How to install GlassFish Java Server using Nginx as a reverse proxy on Debian 11

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

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

prerequisites

  • A server running Debian 11.
  • A valid domain name pointing to your server IP.
  • The 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 it is not installed, you can install it by running the following command:

apt-get install default-jdk unzip -y

After installing Java, 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 completed, you can proceed to the next step.

Download glass fish

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

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

After the download is complete, unzip the downloaded file to the /opt directory:

unzip glassfish-6.1.0.zip -d /opt/

Once completed, you can proceed to the next step.

Create a Systemd service file for Glassfish

Next, you need to create a systemd service file to manage the Glassfish service. You can create it with 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 completed file. Then, reload the systemd daemon to apply the changes.

systemctl daemon-reload

Next, start the Glassfish service and enable it to start when the system restarts:

systemctl start glassfishsystemctl enable glassfish

You can also verify the Glassfish service with 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 completed, you can proceed to the next step.

Set the Glassfish administrator password

By default, you can access Glassfish without any password.Therefore, it is a good idea to protect it by setting one 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 admin The user name is as shown below:

Enter admin user name [default: admin]>admin

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

Enter the admin password> 

Just press Enter key.You will be asked to set up a new admin The password is as follows:

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

Set your security password and click Enter. You will get the following output:

Command change-admin-password executed successfully.

Next, it is also recommended to enable 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 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 completed, you can proceed to the next step.

Access the GlassFish web interface

At this point, Glassfish has been installed and running.By default, the Glassfish web interface listens on the port 8080 and admin Interface listening port 4848. First, use the URL to access the Glassfish Web interface https://your-server-ip:8080. You should see the following page:

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

Log in

Provide your admin Username, password, and click Log in Button.You should see glass fish admin The interface is as follows:

GlassFish dashboard

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

General server information

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

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

node

Configure Nginx as a reverse proxy for Glassfish

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

To do this, first install the Nginx package using the following command:

apt-get install nginx -y

After installation, use the following command to create the Nginx virtual host configuration file:

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 https://glassfish/hello/;
  }
}

Save and close The file then uses the following command to verify if there are any syntax errors in Nginx:

nginx -t

If everything is ok, 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 use the following command to check the status of Nginx:

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 has been installed and configured to serve the Glassfish Web interface.You can now use the URL to access the Glassfish web interface https://glass fish.example.com.

in conclusion

Congratulations! You have successfully installed the Glassfish server with Nginx as a reverse proxy on Debian 11. You can now start deploying Java-based applications on the Glassfish server. If you have any questions, please feel free to ask me.

Source