X
    Categories: Tutorials

How to install WildFly with Nginx as a reverse proxy on Ubuntu 16.04

In this tutorial we are going to provide you with step by step instructions on how to install WildFly with Nginx as a reverse proxy on an Ubuntu 16.04 VPS.
WildFly is a flexible and lightweight open source application server authored by JBoss, now developed by Red Hat.
At the moment of writing this article, WildFly 10 is the latest release in a series of JBoss open-source application server offerings.

Let’s start with the installation. Make sure your server OS packages are fully up-to-date:

apt-get update 
apt-get upgrade

Stop and remove Apache, then install nginx using the following commands:

service apache2 stop
apt-get remove apache2
apt-get autoremove
apt-get install nginx

Edit the default nginx configuration file:

vi /etc/nginx/sites-enabled/default

Delete the following line:

listen [::]:80 default_server;

Enable nginx service to start on boot and start it:

systemctl enable nginx
systemctl start nginx.service

In order to run WildFly, Java has to be installed on the server so run the following command to install the Java Development Kit package (JDK):

apt-get install default-jdk

To find out what version of Java has been installed, run:

java -version

The output should be something like this:

openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~16.04.1-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

Create a WildFly user:

groupadd -r wildfly
useradd -r -g wildfly -d /opt/wildfly -s /sbin/nologin wildfly

Download the latest version of WildFly available at ‘http://wildfly.org/downloads’ to a directory on the server and extract it using the following commands:

cd /opt
wget http://download.jboss.org/wildfly/10.0.0.Final/wildfly-10.0.0.Final.tar.gz
tar -xvzf wildfly-10.0.0.Final.tar.gz
mv wildfly-10.0.0.Final wildfly

Create a wildfly configuration file:

vi /etc/default/wildfly

Add the following lines to it:

WILDFLY_USER="wildfly"
STARTUP_WAIT=180
SHUTDOWN_WAIT=30
WILDFLY_CONFIG=standalone.xml
WILDFLY_MODE=standalone
WILDFLY_BIND=0.0.0.0

so it will start WildFly 10 using the default web profile configuration in ‘standalone’ mode.
Create a WildFly startup script, named ‘launch.sh’:

vi /opt/wildfly/bin/launch.sh
#!/bin/sh

if [ "x$WILDFLY_HOME" = "x" ]; then
    WILDFLY_HOME=/opt/wildfly
fi

if [ "x$1" = "xdomain" ]; then
    echo 'Starting Wildfly in domain mode.'
    $WILDFLY_HOME/bin/domain.sh -c $2 -b $3
else
    echo 'Starting Wildfly in standalone mode.'
    $WILDFLY_HOME/bin/standalone.sh -c $2 -b $3
fi

Make the startup script executable:

chmod 755 /opt/wildfly/bin/launch.sh

WildFly can be started using the launch.sh script we created in the /opt/wildfly/bin directory or even better, we will create a systemd init file for that purpose:

vi /etc/systemd/system/wildfly.service
[Unit]
Description=The WildFly Application Server
After=syslog.target network.target
Before=nginx.service

[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
EnvironmentFile=/etc/default/wildfly
User=wildfly
LimitNOFILE=102642
PIDFile=/var/run/wildfly/wildfly.pid
ExecStart=/opt/wildfly/bin/launch.sh $WILDFLY_MODE $WILDFLY_CONFIG $WILDFLY_BIND
StandardOutput=null

[Install]
WantedBy=multi-user.target

Set the proper ownership of files and directories located to the /opt/wildfly directory:

chown wildfly:wildfly -R /opt/wildfly/

Start the WildFly service and enable WildFly to start on boot:

systemctl daemon-reload
systemctl start wildfly
systemctl enable wildfly

Create a new nginx block so you can access the WildFly management interface using nginx as a reverse proxy:

vi /etc/nginx/sites-available/wildfly

Add the following lines:

upstream wildfly {
    server 127.0.0.1:8080;
}

server {
    listen      80;
    server_name your-domain.com;

    access_log  /var/log/nginx/wildfly.access.log;
    error_log   /var/log/nginx/wildfly.error.log;

    proxy_buffers 16 64k;
    proxy_buffer_size 128k;

location / {
        proxy_pass  http://wildfly;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;

        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto https;
    }

}

Do not forget to replace ‘your-domain.com’ with your actual domain name.

ln -s /etc/nginx/sites-available/wildfly /etc/nginx/sites-enabled/

Restart nginx for the changes to take effect:

systemctl restart nginx.service

In order to access the WildFly management console, you need to add a new user. Execute the add-user.sh script within the bin directory of the WildFly installation and enter the requested information:

/opt/wildfly/bin/add-user.sh

That is it. The WildFly installation is now complete.

To access the WildFly management console, open http://your-domain.com using the newly created user login credentials.

Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install WildFly with Nginx as a reverse proxy for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.