Install WildFly Java Application Server with Nginx Reverse Proxy on Rocky Linux 8
WildFly is an open-source and cross-platform application server developed by RedHat. It is written in Java and used for developing Java applications. It is simple, flexible, lightweight and based on pluggable subsystems that can be added or removed as per your requirements. It comes with a simple and user-friendly dashboard and aims to provide users with a fast and stable Java runtime environment.
In this tutorial, we will show you how to install Wildfly with Nginx as a reverse proxy on Rocky Linux 8. The same steps will work on AlmaLinux 8 and CentOS 8 as well.
Prerequisites
- A server running Rocky Linux 8 / AlmaLinux 8 or centOS 8.
- A root password is configured the server.
Install Java
Wildfly is a Java-based application so Java must be installed in your server. If not installed you can install it with the following command:
dnf install java-11-openjdk-devel -y
After installing Java, you can verify the installed version of Java with the following command:
java --version
You should get the following output:
openjdk 11.0.8 2020-07-14 LTS OpenJDK Runtime Environment 18.9 (build 11.0.8+10-LTS) OpenJDK 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode, sharing)
Install Wildfly
Before installing Wildfly, you will need to create a separate user and group to run Wildfly. You can create them with the following command:
groupadd --system wildfly
useradd -s /sbin/nologin --system -d /opt/wildfly -g wildfly wildfly
Next, you will need to download the latest version of Wildfly from their official website. At the time of writing this tutorial, the latest available version of Wildfly is 20.0.1. You can download it with the following command:
wget https://download.jboss.org/wildfly/20.0.1.Final/wildfly-20.0.1.Final.tar.gz
Once the download is completed, extract the downloaded file with the following command:
tar -xvzf /root/wildfly-20.0.1.Final.tar.gz
Next, copy the extracted directory to the /opt with the following command:
mv wildfly-20.0.1.Final /opt/wildfly
Next, create a directory to store Wildfly configuration files:
mkdir /etc/wildfly
Next, copy all necessary files to the desired location with the following command:
cp /opt/wildfly/docs/contrib/scripts/systemd/wildfly.conf /etc/wildfly/
cp /opt/wildfly/docs/contrib/scripts/systemd/wildfly.service /etc/systemd/system/
cp /opt/wildfly/docs/contrib/scripts/systemd/launch.sh /opt/wildfly/bin/
Next, give proper ownership and permissions to the wildfly directory and files with the following command:
chmod +x /opt/wildfly/bin/launch.sh
chown -R wildfly:wildfly /opt/wildfly
chmod -R +x /opt/wildfly/
Next, reload the systemd daemon with the following command:
systemctl daemon-reload
Next, start the Wildfly service and enable it to start at boot with the following command:
systemctl start wildfly
systemctl enable wildfly
You can now verify the Wildfly service status with the following command:
systemctl status wildfly
You should get the following output:
? wildfly.service - The WildFly Application Server Loaded: loaded (/etc/systemd/system/wildfly.service; disabled; vendor preset: disabled) Active: active (running) since Sun 2020-09-13 05:57:22 EDT; 16s ago Main PID: 31834 (launch.sh) Tasks: 123 (limit: 12527) Memory: 304.1M CGroup: /system.slice/wildfly.service ??31834 /bin/bash /opt/wildfly/bin/launch.sh standalone standalone.xml 0.0.0.0 ??31835 /bin/sh /opt/wildfly/bin/standalone.sh -c standalone.xml -b 0.0.0.0 ??31925 java -D[Standalone] -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=tru> Sep 13 05:57:22 centos8 systemd[1]: Started The WildFly Application Server.
At this point Wildfly is installed and started. By default, wildfly application server is listening on port 8080. You can check it with the following command:
ss -tunelp | grep 8080
You should get the following output:
tcp LISTEN 0 128 0.0.0.0:8080 0.0.0.0:* users:(("java",pid=31925,fd=478)) uid:989 ino:59014 sk:9
Wildfly admin console is listening on port 9990. You can check it with the following command:
ss -tunelp | grep 9990
You should get the following output:
tcp LISTEN 0 50 127.0.0.1:9990 0.0.0.0:* users:(("java",pid=31925,fd=138)) uid:989 ino:59017 sk:7
Add Wildfly Admin User
In order to access WildFly administration console, you will need to create an administrative user for Wildfly. You can create it with the following command:
/opt/wildfly/bin/add-user.sh
You will be asked to choose the type of user you want to add as shown below:
What type of user do you wish to add? a) Management User (mgmt-users.properties) b) Application User (application-users.properties) (a): a
Type a for management user and hit Enter. You should see the following output:
Enter the details of the new user to add. Using realm 'ManagementRealm' as discovered from the existing property files. Username : wildflyadmin
Provide your desired username and hit Enter. You should see the following output:
Password recommendations are listed below. To modify these restrictions edit the add-user.properties configuration file. - The password should be different from the username - The password should not be one of the following restricted values {root, admin, administrator} - The password should contain at least 8 characters, 1 alphabetic character(s), 1 digit(s), 1 non-alphanumeric symbol(s) Password : Re-enter Password :
Provide your password and hit Enter. You should see the following output:
What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[ ]: About to add user 'wildflyadmin' for realm 'ManagementRealm' Is this correct yes/no? yes Added user 'wildflyadmin' to file '/opt/wildfly/standalone/configuration/mgmt-users.properties' Added user 'wildflyadmin' to file '/opt/wildfly/domain/configuration/mgmt-users.properties' Added user 'wildflyadmin' with groups to file '/opt/wildfly/standalone/configuration/mgmt-groups.properties' Added user 'wildflyadmin' with groups to file '/opt/wildfly/domain/configuration/mgmt-groups.properties' Is this new user going to be used for one AS process to connect to another AS process? e.g. for a slave host controller connecting to the master or for a Remoting connection for server to server EJB calls. yes/no? yes To represent the user add the following to the server-identities definition
At this point, your Wildfly management user is created. You can proceed to the next step.
Configure Firewall and SELinux
By default, SELinux is enabled in Rocky Linux 8. So you will need to configure SELinux for Wildfly. You can do it with the following command:
semanage fcontext -a -t bin_t "/opt/wildfly/bin(/.*)?"
restorecon -Rv /opt/wildfly/bin/
setsebool -P httpd_can_network_connect 1
Next, you will need to allow port 8080, 9990 and 80 through the firewall. You can do it with the following command:
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --permanent --add-port=9990/tcp
firewall-cmd --permanent --add-port=80/tcp
Next, reload the firewalld to apply the changes.
firewall-cmd --reload
Once you are finished, you can proceed to the next step.
Access Wildfly Admin Console
By default, Wildfly admin console is accessible only from the localhost. You will need to edit /opt/wildfly/bin/launch.sh and make some changes for external access.
nano /opt/wildfly/bin/launch.sh
Find the following line:
$WILDFLY_HOME/bin/standalone.sh -c $2 -b $3
And, replace it with the following line:
$WILDFLY_HOME/bin/standalone.sh -c $2 -b $3 -bmanagement=0.0.0.0
Save and close the file then restart the Wildfly service to apply the changes.
systemctl restart wildfly
Now, open your web browser and access the Wildfly admin console using the URL http://your-server-ip:9990. You will be asked to provide username and password as shown below:
Provide your Wildfly admin username, password and click on the Sign in button. Once login, you should see the Wildfly admin dashboard in the following screen:
Configure Nginx as a Reverse Proxy for Wildfly
By default, Wildfly application is accessible on port 8080. So it is recommended way to configure Nginx as a reverse proxy so it can be accessed using the port 80.
First, install the Nginx web server with the following command;
dnf install nginx -y
Once installed, create a new Wildfly configuration file with the following command:
nano /etc/nginx/conf.d/wildfly.conf
Add the following lines:
upstream wildfly { server 127.0.0.1:8080 weight=100 max_fails=5 fail_timeout=5; } server { listen 80; server_name your-server-ip; 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://wildfly/; } }
Save and close the file then check the Nginx for any configuration error with the following command:
nginx -t
If everything is fine you should 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, start the Nginx and enable it to start at system reboot with the following command:
systemctl start nginx
systemctl enable nginx
You can also check the status of the Nginx service with the following command:
systemctl status nginx
You should get the following output:
? nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Drop-In: /usr/lib/systemd/system/nginx.service.d ??php-fpm.conf Active: active (running) since Sun 2020-09-13 06:03:37 EDT; 5s ago Process: 1775 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 1773 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 1771 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 1776 (nginx) Tasks: 3 (limit: 12527) Memory: 5.4M CGroup: /system.slice/nginx.service ??1776 nginx: master process /usr/sbin/nginx ??1777 nginx: worker process ??1778 nginx: worker process Sep 13 06:03:37 centos8 systemd[1]: Stopped The nginx HTTP and reverse proxy server. Sep 13 06:03:37 centos8 systemd[1]: Starting The nginx HTTP and reverse proxy server... Sep 13 06:03:37 centos8 nginx[1773]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Sep 13 06:03:37 centos8 nginx[1773]: nginx: configuration file /etc/nginx/nginx.conf test is successful Sep 13 06:03:37 centos8 systemd[1]: Started The nginx HTTP and reverse proxy server.
At this point, Nginx is installed and configured to serve your Wildfly application. You can now access the Wildfly application using the URL http://your-server-ip. You should see the following screen:
Conclusion
In this guide, we learned how to install Wildfly and enable remote access on Rocky Linux 8. We also configured Nginx as a reverse proxy to access the Wildfly through the port 80. Now, you can start building the Java application and manage it from the Widlfly admin console.