本页内容

  • 前提条件
  • 安装 Java
  • 安装 Wildfly
  • 添加 Wildfly 管理员用户
  • 配置防火墙和 SELinux
  • 访问 Wildfly 管理控制台
  • 将 Nginx 配置为 Wildfly 的反向代理
  • 结论

WildFly 是由 RedHat 开发的开源跨平台应用服务器。它使用 Java 编写,用于开发 Java 应用程序。它简单、灵活、轻量,并基于可插拔子系统,可根据您的需求添加或移除。它配有简洁且用户友好的仪表板,旨在为用户提供快速稳定的 Java 运行时环境。

在本教程中,我们将向您展示如何在 Rocky Linux 8 上安装 Wildfly 并以 Nginx 作为反向代理。相同的步骤同样适用于 AlmaLinux 8 和 CentOS 8。

前提条件

  • 一台运行 Rocky Linux 8 / AlmaLinux 8 或 CentOS 8 的服务器。
  • 服务器上已配置 root 密码。

安装 Java

Wildfly 是基于 Java 的应用程序,因此您的服务器上必须安装 Java。若尚未安装,可使用以下命令进行安装:

dnf install java-11-openjdk-devel -y

安装 Java 后,您可以使用以下命令验证已安装的 Java 版本:

java --version

您应该会看到以下输出:

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)

安装 Wildfly

在安装 Wildfly 之前,您需要创建一个单独的用户和组来运行 Wildfly。可使用以下命令创建:

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

接下来,您需要从官方网站下载最新版本的 Wildfly。在撰写本教程时,Wildfly 的最新可用版本为 20.0.1。可使用以下命令下载:

wget https://download.jboss.org/wildfly/20.0.1.Final/wildfly-20.0.1.Final.tar.gz

下载完成后,使用以下命令解压已下载的文件:

tar -xvzf /root/wildfly-20.0.1.Final.tar.gz

接下来,使用以下命令将解压后的目录复制到 /opt:

mv wildfly-20.0.1.Final /opt/wildfly

接下来,创建一个目录用于存放 Wildfly 配置文件:

mkdir /etc/wildfly

接下来,使用以下命令将所有必需文件复制到目标位置:

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/

接下来,使用以下命令为 wildfly 目录及文件设置正确的所有者和权限:

chmod +x /opt/wildfly/bin/launch.sh
chown -R wildfly:wildfly /opt/wildfly
chmod -R +x /opt/wildfly/

接下来,使用以下命令重新加载 systemd 守护进程:

systemctl daemon-reload

接下来,使用以下命令启动 Wildfly 服务并设置为开机自启:

systemctl start wildfly
systemctl enable wildfly

现在,您可以使用以下命令验证 Wildfly 服务状态:

systemctl status wildfly

您应该会看到以下输出:

? 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.

此时 Wildfly 已安装并启动。默认情况下,wildfly 应用服务器监听 8080 端口。可使用以下命令检查:

ss -tunelp | grep 8080

您应该会看到以下输出:

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 管理控制台监听 9990 端口。可使用以下命令检查:

ss -tunelp | grep 9990

您应该会看到以下输出:

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

添加 Wildfly 管理员用户

为了访问 WildFly 管理控制台,您需要为 Wildfly 创建一个管理用户。可使用以下命令创建:

/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): a           

输入 a 选择管理用户并按 Enter。您应该会看到以下输出:

Enter the details of the new user to add.
Using realm 'ManagementRealm' as discovered from the existing property files.
Username : wildflyadmin

输入您想要的用户名并按 Enter。您应该会看到以下输出:

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 : 

输入您的密码并按 Enter。您应该会看到以下输出:

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 

此时,您的 Wildfly 管理用户已创建。您可以继续下一步。

配置防火墙和 SELinux

默认情况下,Rocky Linux 8 启用了 SELinux。因此您需要为 Wildfly 配置 SELinux。可使用以下命令完成:

semanage fcontext -a -t bin_t "/opt/wildfly/bin(/.*)?"
restorecon -Rv /opt/wildfly/bin/
setsebool -P httpd_can_network_connect 1

接下来,您需要允许 8080、9990 和 80 端口通过防火墙。可使用以下命令完成:

firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --permanent --add-port=9990/tcp
firewall-cmd --permanent --add-port=80/tcp

接下来,重新加载 firewalld 以应用更改。

firewall-cmd --reload

完成后,您可以继续下一步。

访问 Wildfly 管理控制台

默认情况下,Wildfly 管理控制台只能从本地主机访问。您需要编辑 /opt/wildfly/bin/launch.sh 并进行一些修改以实现外部访问。

nano /opt/wildfly/bin/launch.sh

找到以下这一行:

    $WILDFLY_HOME/bin/standalone.sh -c $2 -b $3

然后,将其替换为以下这一行:

    $WILDFLY_HOME/bin/standalone.sh -c $2 -b $3 -bmanagement=0.0.0.0

保存并关闭文件,然后重启 Wildfly 服务以应用更改。

systemctl restart wildfly

现在,打开您的 Web 浏览器,通过 URL http://your-server-ip:9990 访问 Wildfly 管理控制台。系统将要求您提供用户名和密码,如下所示:

在 Rocky Linux 8 上安装 WildFly 并配置 Nginx 反向代理完整教程

输入您的 Wildfly 管理员用户名、密码并点击 Sign in 按钮。登录后,您应该会在以下界面看到 Wildfly 管理仪表板:

在 Rocky Linux 8 上安装 WildFly 并配置 Nginx 反向代理完整教程

将 Nginx 配置为 Wildfly 的反向代理

默认情况下,Wildfly 应用可通过 8080 端口访问。因此推荐的做法是将 Nginx 配置为反向代理,以便通过 80 端口访问它。

首先,使用以下命令安装 Nginx Web 服务器:

dnf install nginx -y

安装完成后,使用以下命令创建一个新的 Wildfly 配置文件:

nano /etc/nginx/conf.d/wildfly.conf

添加以下行:

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/;
  }
}

保存并关闭文件,然后使用以下命令检查 Nginx 是否存在配置错误:

nginx -t

如果一切正常,您应该会看到以下输出:

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

接下来,使用以下命令启动 Nginx 并设置为系统重启时自启:

systemctl start nginx
systemctl enable nginx

您也可以使用以下命令检查 Nginx 服务状态:

systemctl status nginx

您应该会看到以下输出:

? 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.

此时,Nginx 已安装并配置好为您的 Wildfly 应用提供服务。现在您可以通过 URL http://your-server-ip 访问 Wildfly 应用。您应该会看到以下界面:

在 Rocky Linux 8 上安装 WildFly 并配置 Nginx 反向代理完整教程

结论

在本指南中,我们学习了如何在 Rocky Linux 8 上安装 Wildfly 并启用远程访问。我们还配置了 Nginx 作为反向代理,以便通过 80 端口访问 Wildfly。现在,您可以开始构建 Java 应用程序,并从 Widlfly 管理控制台进行管理。