Skip to main content

WildFly (JBoss Community Version) - Setup on Debian 12, Deployment of a Java WebApp, Nginx Reverse Proxy

1040 words·
WildFly JBoss Debian Java Nginx

Prerequisites
#

In this tutorial I’m using a Debian 12 server with the following IP “192.168.30.61”.

Install Java
#

# Install OpenJDK (Open-source Java Platform Standard Edition)
sudo apt update && sudo apt install default-jdk -y

# Verify installation
java -version

Install WildFly
#

# Find the link for the latest WildFly Distribution
https://www.wildfly.org/downloads/
# Install unzip
sudo apt install unzip -y

# Download WildFly
cd /tmp && wget https://github.com/wildfly/wildfly/releases/download/32.0.0.Final/wildfly-32.0.0.Final.zip

# Unzip the package
sudo unzip wildfly-*.zip -d /opt

# Rename the folder
sudo mv /opt/wildfly-32.0.0.Final /opt/wildfly

WildFly Configuration
#

WildFly System User and Group
#

# Create a system user and group for WildFly
sudo addgroup --system wildfly &&
sudo adduser --system --ingroup wildfly --shell /sbin/nologin wildfly

# Change ownership
sudo chown -R wildfly:wildfly /opt/wildfly

Note: “Not creating `/nonexistent’” means no home directory was created for the user.

Copy Configuration Files
#

# Copy the service unit and configuration files
sudo mkdir -p /etc/wildfly &&
sudo cp /opt/wildfly/docs/contrib/scripts/systemd/wildfly.conf /etc/wildfly/ &&
sudo cp /opt/wildfly/docs/contrib/scripts/systemd/wildfly.service /etc/systemd/system/ &&
sudo cp /opt/wildfly/docs/contrib/scripts/systemd/launch.sh /opt/wildfly/bin/ &&
sudo chmod +x /opt/wildfly/bin/*.sh

wildfly.conf
#

# Edit wildfly.conf
sudo vi /etc/wildfly/wildfly.conf

Change the IP to 0.0.0.0 to allow connections from any IP address, or define a specific network interface:

# The address to bind to
WILDFLY_BIND=0.0.0.0
WILDFLY_CONSOLE_BIND=0.0.0.0

standalone.xml
#

Per default the management interface is bound to 127.0.0.1, change it to 0.0.0.0 to allow connections from any IP address, or define a specific network interface.

# Open the configuration file for the standalone profile
sudo vi /opt/wildfly/standalone/configuration/standalone.xml
<interfaces>
    <interface name="management">
        <inet-address value="${jboss.bind.address.management:0.0.0.0}"/>
    </interface>
    <interface name="public">
        <inet-address value="${jboss.bind.address:0.0.0.0}"/>
    </interface>
  • Management Interface: Dedicated to managing the WildFly server itself through the management console and the management API. It’s used to configure, monitor, and manage server resources and deployments.

  • Public Interface: This interface is used to access deployed applications.


Optional: Verify the listening ports (After the rest of the installation)

# Install net-tools package
sudo apt install net-tools -y

# Verify WildFly is listening on the correct interface
sudo netstat -tulpn | grep 9990

Systemd Service-Unit
#

The following service unit starts a standalone server:
https://docs.wildfly.org/32/Getting_Started_Guide.html#standalone-server-configurations

# Open the service unit
sudo vi /etc/systemd/system/wildfly.service

# Modify the following line:
ExecStart=/opt/wildfly-32.0.0.Final/bin/standalone.sh $WILDFLY_MODE $WILDFLY_CONFIG $WILDFLY_BIND

# To:
ExecStart=/opt/wildfly/bin/launch.sh $WILDFLY_MODE $WILDFLY_CONFIG $WILDFLY_BIND $WILDFLY_CONSOLE_BIND -Djboss.bind.address.management=0.0.0.0

The service unit should look like this:

[Unit]
Description=The WildFly Application Server
After=syslog.target network.target
Before=httpd.service

[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
EnvironmentFile=-/etc/wildfly/wildfly.conf
User=wildfly
LimitNOFILE=102642
PIDFile=/run/wildfly/wildfly.pid
ExecStart=/opt/wildfly/bin/launch.sh $WILDFLY_MODE $WILDFLY_CONFIG $WILDFLY_BIND $WILDFLY_CONSOLE_BIND -Djboss.bind.address.management=0.0.0.0
StandardOutput=null

[Install]
WantedBy=multi-user.target

Start and Enable Service
#

# Reload systemd daemon / reload configuration files
sudo systemctl daemon-reload

# Start service and enable after boot
sudo systemctl enable wildfly && sudo systemctl start wildfly

# Check status
sudo systemctl status wildfly

WildFly
#

Create WildFly Management User
#

# Create a WildFly management user (For the admin console)
sudo /opt/wildfly/bin/add-user.sh

# What type of user do you wish to add?
# a) Management User (mgmt-users.properties)
# b) Application User (application-users.properties)
a

# Username : 
# Password :
# Re-enter Password :
# What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[  ]: 
[Enter]

Note: It’s not necessary to define a group for the management user.

Access Admin Console
#

# Open the admin console in a browser
http://192.168.30.61:9990

# Login with the Management User created earlier

Example Application
#

Prerequisite: Install Java
#

# Find latest .deb package
https://www.oracle.com/java/technologies/downloads/

# Download package
cd /tmp && wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.deb

# Install JDK (Java Development Kit)
sudo dpkg -i jdk-22_linux-x64_bin.deb
# Verify Java installation / check version
java -version

# Verify Java Compiler installation / check version
javac -version

# List Java installation path
which java

# List path of Java executable
ls -l $(which java)

Example Webapplication
#

Project Folder
#

# Create a project folder
mkdir -p ~/example-web-project/{META-INF,WEB-INF} && cd ~/example-web-project

Java Server Pages File
#

# Create the JavaServerPages file
vi index.jsp
<!doctype html>
<html>
<head>
    <title>JSP Test</title>
    <%!
        String title = "Hello, World";
    %>
</head>
<body>
    <h2><%= title %></h2>
    <p>
        <%= new java.util.Date() %>
    </p>
    <p>
        You are from <%= request.getRemoteAddr() %>
    </p>
    <div id="congrats"></div>
</body>
</html>

Deployment Descriptor
#

# Create the deployment descriptor
vi WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <display-name>Hello World</display-name>
</web-app>

Package the Application
#

A WAR (Web Archive) file is simply a ZIP file with the compiled classes and web.xml structured in a specific way.

# Create the WAR File
jar -cvf HelloWorld-WebApp.war *

Application Deployment
#

Permissions
#

Copy the HelloWorld-WebApp.war file into a directory on the WildFly server, where the wildfly user has permissions to use it.

# Create deployment directory
sudo mkdir -p /var/wildfly/deployments/

# Set ownership nad permissions
sudo chown wildfly:wildfly /var/wildfly/deployments &&
sudo chmod 750 /var/wildfly/deployments

# Change file owner
sudo chown wildfly:wildfly /var/wildfly/deployments/HelloWorld-WebApp.war

Deploy / Undeploy App from CLI
#

# Deploy the WAR file to WildFly
sudo -u wildfly /opt/wildfly-32.0.0.Final/bin/jboss-cli.sh --connect --command="deploy /var/wildfly/deployments/HelloWorld-WebApp.war"
# Undeploy the WAR file
sudo -u wildfly /opt/wildfly-32.0.0.Final/bin/jboss-cli.sh --connect --command="undeploy HelloWorld-WebApp.war"

Deploy / Undeploy App Manually
#

# Copy the WAR file into the standalone/deployments directory
sudo cp /tmp/HelloWorld-WebApp.war /opt/wildfly-32.0.0.Final/standalone/deployments/
# Undeploy the app
sudo rm /opt/wildfly-32.0.0.Final/standalone/deployments/HelloWorld-WebApp.war

Deployment Logs
#

# List the deployment logs
tail /opt/wildfly-32.0.0.Final/standalone/log/server.log

Access the Deployment
#

# Open the URL in a browser
http://192.168.30.71:8080/HelloWorld-WebApp/

Nginx Reverse Proxy
#

Certbot
#

# Install Let's Encrypt Certbot
sudo apt install certbot -y

# Create certificate
sudo certbot certonly --standalone -d wildfly.jklug.work

# Certificate path
/etc/letsencrypt/live/wildfly.jklug.work/

Note: Since I have tested this setup in my homelab, I have just used a Let’s Encrypt wildcard certificate.

Nginx Reverse Proxy
#

# Install Nginx
sudo apt install nginx -y

# Copy default website configuration
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/wildfly.jklug.work

# Edit the new website configuration
sudo vi /etc/nginx/sites-available/wildfly.jklug.work
server {
    listen 443 ssl;
    server_name wildfly.jklug.work;

    ssl_certificate /etc/letsencrypt/live/wildfly.jklug.work/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/wildfly.jklug.work/privkey.pem;

    location / {
        proxy_pass http://localhost:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    add_header Front-End-Https on;
    add_header Cache-Control no-cache;
    }
}
# Enable the new website configuration
sudo ln -s /etc/nginx/sites-available/wildfly.jklug.work /etc/nginx/sites-enabled/

# Disable the default configuration
sudo rm /etc/nginx/sites-enabled/default

# Test the website configuration
sudo nginx -t

# Restart Nginx
sudo systemctl restart nginx

Test the Deployment with TLS Encryption
#

# Open the URL in a browser
https://wildfly.jklug.work/HelloWorld-WebApp/

Links #

# Official Documentation
https://docs.wildfly.org/

# Download WildFly Distribution latest version
https://www.wildfly.org/downloads/