本教程适用于以下操作系统版本

  • Debian 12 (Bookworm)
  • Debian 11 (Bullseye)
  • Debian 10 (Buster)

本页内容

  • 前提条件
  • 安装 Java JDK
  • 安装 Wildfly
  • 启用 Wildfly 管理控制台
  • 创建 Wildfly 管理员用户
  • 安装并配置 Nginx 作为反向代理
  • 访问 Wildfly Web UI
  • 总结

Wildfly 是一款用 Java 编写、由 RedHat 开发的应用服务器。它是一款简单、轻量且功能强大的服务器,带有 CLI 和管理控制台。它是开源的、跨平台的,基于可插拔子系统,可根据需要添加或删除。它提供了运行 Java Web 应用程序所需的所有功能。

在本教程中,我将向您展示如何在 Debian 11 上安装 Wildfly 并以 Nginx 作为反向代理。

前提条件

  • 一台运行 Debian 11 的服务器。
  • 一个指向您服务器 IP 的有效域名。
  • 您的服务器上配置了 root 密码。

安装 Java JDK

Wildfly 是一个基于 Java 的应用程序,因此必须在您的服务器上安装 Java。如果尚未安装,您可以通过运行以下命令来安装:

apt-get install default-jdk -y

Java 安装完成后,您可以使用以下命令进行验证:

java --version

您应该在以下输出中看到 Java 版本:

openjdk 11.0.12 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-post-Debian-2deb10u1)
OpenJDK 64-Bit Server VM (build 11.0.12+7-post-Debian-2deb10u1, mixed mode, sharing)

安装 Wildfly

首先,使用以下命令创建一个用于运行 Wildfly 应用程序的用户和组:

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

接下来,使用 wget 命令下载最新版本的 Wildfly:

wget https://github.com/wildfly/wildfly/releases/download/25.0.1.Final/wildfly-25.0.1.Final.zip

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

unzip wildfly-25.0.1.Final.zip

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

mv wildfly-25.0.1.Final /opt/wildfly

接下来,更改 /opt/wildfly 目录的所有权:

chown -RH wildfly:wildfly /opt/wildfly

接下来,使用以下命令在 /etc 中创建 Wildfly 配置目录:

mkdir -p /etc/wildfly

接下来,将所有必需的文件从 Wildfly 目录复制到 /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/

接下来,为所有 shell 脚本文件设置执行权限:

chmod +x /opt/wildfly/bin/*.sh

接下来,使用以下命令重新加载 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: enabled)
   Active: active (running) since Fri 2021-12-10 14:55:07 UTC; 6s ago
 Main PID: 20928 (launch.sh)
    Tasks: 59 (limit: 4701)
   Memory: 171.6M
   CGroup: /system.slice/wildfly.service
           ??20928 /bin/bash /opt/wildfly/bin/launch.sh standalone standalone.xml 0.0.0.0
           ??20929 /bin/sh /opt/wildfly/bin/standalone.sh -c standalone.xml -b 0.0.0.0
           ??21022 java -D[Standalone] -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=tru

Dec 10 14:55:07 debian11 systemd[1]: Started The WildFly Application Server.

启用 Wildfly 管理控制台

默认情况下,Wildfly 管理控制台是禁用的。建议启用它来管理 Wildfly 应用程序。

您可以通过编辑 Wildfly 配置文件来启用它:

nano /etc/wildfly/wildfly.conf

更改以下行:

WILDFLY_BIND=127.0.0.1
WILDFLY_CONSOLE_BIND=127.0.0.1

保存并关闭文件,然后编辑 Wildfly 启动脚本并启用管理控制台。

nano /opt/wildfly/bin/launch.sh

按如下所示修改文件:

if [[ "$1" == "domain" ]]; then
    $WILDFLY_HOME/bin/domain.sh -c $2 -b $3 -bmanagement $4
else
    $WILDFLY_HOME/bin/standalone.sh -c $2 -b $3 -bmanagement $4
fi

保存并关闭文件,然后编辑 Wildfly systemd 文件:

nano /etc/systemd/system/wildfly.service

更改以下行:

ExecStart=/opt/wildfly/bin/launch.sh $WILDFLY_MODE $WILDFLY_CONFIG $WILDFLY_BIND $WILDFLY_CONSOLE_BIND

保存并关闭文件,然后重新加载 systemd 守护进程以应用配置更改:

systemctl daemon-reload

接下来,重启 Wildfly 服务以应用更改:

systemctl restart wildfly

您还可以使用以下命令检查 Wildfly 的状态:

systemctl status wildfly

您将得到以下输出:

? wildfly.service - The WildFly Application Server
   Loaded: loaded (/etc/systemd/system/wildfly.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2021-12-10 14:57:22 UTC; 3s ago
 Main PID: 21182 (launch.sh)
    Tasks: 64 (limit: 4701)
   Memory: 205.6M
   CGroup: /system.slice/wildfly.service
           ??21182 /bin/bash /opt/wildfly/bin/launch.sh standalone standalone.xml 127.0.0.1 127.0.0.1
           ??21183 /bin/sh /opt/wildfly/bin/standalone.sh -c standalone.xml -b 127.0.0.1 -bmanagement 127.0.0.1
           ??21282 java -D[Standalone] -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=tru

Dec 10 14:57:22 debian11 systemd[1]: Started The WildFly Application Server.

此时,Wildfly 已启动并监听端口 8080 和 9990。您可以使用以下命令检查它们:

ss -plnt

您将得到以下输出:

State        Recv-Q       Send-Q             Local Address:Port             Peer Address:Port                                                 
LISTEN       0            128                      0.0.0.0:22                    0.0.0.0:*           users:(("sshd",pid=678,fd=3))            
LISTEN       0            128                    127.0.0.1:8443                  0.0.0.0:*           users:(("java",pid=21282,fd=485))        
LISTEN       0            50                     127.0.0.1:9990                  0.0.0.0:*           users:(("java",pid=21282,fd=487))        
LISTEN       0            80                     127.0.0.1:3306                  0.0.0.0:*           users:(("mysqld",pid=12461,fd=21))       
LISTEN       0            128                    127.0.0.1:8080                  0.0.0.0:*           users:(("java",pid=21282,fd=476))        
LISTEN       0            128                         [::]:22                       [::]:*           users:(("sshd",pid=678,fd=4))            

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

创建 Wildfly 管理员用户

接下来,您还需要创建一个管理员用户来访问 Wildfly 管理控制台。您可以通过运行以下命令来创建它:

sh /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 : hitesh
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 键。系统将要求您向 realm 添加用户:

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 'hitesh' for realm 'ManagementRealm'
Is this correct yes/no? yes

键入 yes 并按 Enter 键继续。用户添加完成后,您将得到以下输出:

Added user 'hitesh' to file '/opt/wildfly/standalone/configuration/mgmt-users.properties'
Added user 'hitesh' to file '/opt/wildfly/domain/configuration/mgmt-users.properties'
Added user 'hitesh' with groups  to file '/opt/wildfly/standalone/configuration/mgmt-groups.properties'
Added user 'hitesh' 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 Jakarta Enterprise Beans calls.
yes/no? yes
To represent the user add the following to the server-identities definition 

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

安装并配置 Nginx 作为反向代理

接下来,您需要将 Nginx 配置为反向代理,以便通过端口 80 访问 Wildfly。

首先,使用以下命令安装 Nginx 软件包:

apt-get install nginx -y

Nginx 安装完成后,使用以下命令创建一个 Nginx 代理文件:

nano /etc/nginx/conf.d/proxy_headers.conf

添加以下行:

proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
add_header Front-End-Https on;
add_header Cache-Control no-cache;

保存并关闭文件,然后为 Wildfly 创建一个 Nginx 虚拟主机配置文件:

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

添加以下行:

server {
  listen          80;
  server_name     wildfly.yourdomain.com;

  location / {
    include conf.d/proxy_headers.conf;
    proxy_pass http://127.0.0.1:8080;
  }

  location /management {
    include conf.d/proxy_headers.conf;
    proxy_pass http://127.0.0.1:9990/management;
  }

  location /console {
    include conf.d/proxy_headers.conf;
    proxy_pass http://127.0.0.1:9990/console;
  }

  location /logout {
    include conf.d/proxy_headers.conf;
    proxy_pass http://127.0.0.1:9990/logout;
  }

  location /error {
    include conf.d/proxy_headers.conf;
    proxy_pass http://127.0.0.1:9990;
  }

}

保存并关闭文件,然后使用以下命令检查 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 restart nginx

您还可以使用以下命令检查 Nginx 状态:

systemctl status nginx

您将得到以下输出:

? 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 Fri 2021-12-10 15:12:26 UTC; 9s ago
     Docs: man:nginx(8)
  Process: 22115 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 22116 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 22117 (nginx)
    Tasks: 3 (limit: 4701)
   Memory: 3.8M
   CGroup: /system.slice/nginx.service
           ??22117 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ??22118 nginx: worker process
           ??22119 nginx: worker process

Dec 10 15:12:25 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Dec 10 15:12:26 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.

此时,Nginx 已安装并配置为服务于 Wildfly。现在您可以继续下一步。

访问 Wildfly Web UI

现在,打开您的 Web 浏览器并使用 URL http://wildfly.yourdomain.com 访问 Wildfly 应用程序页面。您应该在以下屏幕上看到 Wildfly 默认页面:

如何在 Debian 11 上安装和配置 WildFly(JBoss)应用服务器

要访问 Wildfly 管理控制台,请在 Web 浏览器中输入 URL http://wildfly.yourdomain.com/console/ 。系统将要求您提供管理员用户名和密码,如下所示:

如何在 Debian 11 上安装和配置 WildFly(JBoss)应用服务器

提供您的管理员用户名和密码,然后单击 Sign in 按钮。登录后,您应该看到 Wildfly 管理仪表板:

如何在 Debian 11 上安装和配置 WildFly(JBoss)应用服务器

总结

恭喜!您已在 Debian 11 上成功安装 Wildfly 并以 Nginx 作为反向代理。现在您可以使用 Wildfly 在 Internet 上托管您的 Java 应用程序。如果您有任何问题,请随时向我提问。