これは、なにをしたくて書いたもの?
WildFlyにsystemd用の設定ファイルが含まれているのに気づきまして、ちょっと試してみようかなと。
WildFlyのサービス起動用スクリプト
WildFlyのdocs/contrib/scriptsに、以下の3種類の起動スクリプトのサンプルが含まれています。
- init.d(System V)
- systemd
- Windowsサービス
GitHub上だと、こちらですね。
説明はREADME.mdにあります。
各ディレクトリ内のスクリプトや設定ファイルのうち、READMEが付属しているのはsystemdだけのようですね。
今回はこの中のうちで、systemd用のものを使ってみます。
環境
今回の環境は、こちらです。Ubuntu Linux 20.04 LTSです。
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.2 LTS Release: 20.04 Codename: focal $ uname -srvmpio Linux 5.4.0-80-generic #90-Ubuntu SMP Fri Jul 9 22:49:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Javaのバージョンは、こちら。
$ java --version openjdk 11.0.11 2021-04-20 OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04) OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)
WildFlyは24.0.0.Finalを使います。
$ curl -OL https://download.jboss.org/wildfly/24.0.0.Final/wildfly-24.0.0.Final.zip $ unzip wildfly-24.0.0.Final.zip
WildFlyをsystemdのサービスユニットとして登録する
まずは、docs/contrib/scriptsディレクトリ内にあるREADME.mdに目を通してみます。
wildfly-24.0.0.Final/docs/contrib/scripts/README.md
User contributed init scripts ============================= ____ ____ _ _______ ____ _____ _____ ____ _____ ______ _ |_ _| |_ _|/ \ |_ __ \ |_ \|_ _||_ _||_ \|_ _|.' ___ | | | \ \ /\ / / / _ \ | |__) | | \ | | | | | \ | | / .' \_| | | \ \/ \/ / / ___ \ | __ / | |\ \| | | | | |\ \| | | | ____ | | \ /\ /_/ / \ \_ _| | \ \_ _| |_\ |_ _| |_ _| |_\ |_\ `.___] | |_| \/ \/|____| |____||____| |___||_____|\____||_____||_____|\____|`._____.' (_) In this folder you can find user contributed scripts & services for running WildFly as a service on various operating systems. This scripts are user contributions and are here as example and/or reference. init.d ------ System V init.d scripts. + `wildfly-init-redhat.sh` for RHEL/CentOS 6.* and below + `wildfly-init-debian.sh` for Debian based distributions that do not use Systemd Both scripts use `wildfly.conf` as reference configuration file and expect that file is present in proper location. Selected script should be copied and renamed to proper directory before usage. systemd ------- Systemd scripts for Linux distributions that use systemd. Use this script for RHEL 7 and above. See systemd/README on how to use it. service ------- Windows service files, to enable installing wildfly as service on Windows. See service.bat on how to use it.
サンプルであることがすごく強調されています。
This scripts are user contributions and are here as example and/or reference.
WildFlyのドキュメントにも、このあたりのスクリプト群の説明ってないんですよね。利用は自己責任というか、
中身をちゃんと見て使いましょうという感じですね。
systemdのREADMEには、インストール方法が書いてあります。
wildfly-24.0.0.Final/docs/contrib/scripts/systemd/README
= How to configure WildFly as a systemd service
== Create a wildfly user
# groupadd -r wildfly
# useradd -r -g wildfly -d /opt/wildfly -s /sbin/nologin wildfly
== Install WildFly
# tar xvzf wildfly-10.0.0.Final.tar.gz -C /opt
# ln -s /opt/wildfly-10.0.0.Final /opt/wildfly
# chown -R wildfly:wildfly /opt/wildfly
== Configure systemd
# mkdir /etc/wildfly
# cp wildfly.conf /etc/wildfly/
# cp wildfly.service /etc/systemd/system/
# cp launch.sh /opt/wildfly/bin/
# chmod +x /opt/wildfly/bin/launch.sh
== Start and enable
# systemctl start wildfly.service
# systemctl enable wildfly.service
ざっくり、WildFly用のグループとユーザーの作成、WildFlyを/opt/wildflyに配置、systemdの設定、起動といった
流れですね。
systemd用のディレクトリに含まれているファイルは、こんな感じです。
$ ll wildfly-24.0.0.Final/docs/contrib/scripts/systemd 合計 24 drwxr-xr-x 2 xxxxx xxxxx 4096 6月 16 16:47 ./ drwxr-xr-x 5 xxxxx xxxxx 4096 6月 16 16:47 ../ -rw-r--r-- 1 xxxxx xxxxx 645 6月 16 16:47 README -rwxr-xr-x 1 xxxxx xxxxx 217 6月 16 16:47 launch.sh* -rw-r--r-- 1 xxxxx xxxxx 165 6月 16 16:47 wildfly.conf -rw-r--r-- 1 xxxxx xxxxx 409 6月 16 16:47 wildfly.service
なんとなく、読み替えながら設定していってみます。
ユーザーの作成。
$ sudo adduser --shell /usr/sbin/nologin --disabled-password --no-create-home --gecos '' wildfly
確認。
$ sudo -u wildfly id uid=1001(wildfly) gid=1001(wildfly) groups=1001(wildfly)
ダウンロードしておいたWildFlyを/optディレクトリに移動して、/opt/wildflyとなるようにシンボリックリンクを作成。
$ sudo mv wildfly-24.0.0.Final /opt $ sudo ln -s /opt/wildfly-24.0.0.Final /opt/wildfly $ sudo chown -R wildfly:wildfly /opt/wildfly /opt/wildfly-24.0.0.Final
systemdで使う、環境変数の定義ファイルを/etc/wildflyにコピー。
$ sudo mkdir /etc/wildfly $ sudo cp /opt/wildfly/docs/contrib/scripts/systemd/wildfly.conf /etc/wildfly
systemdのユニット定義ファイルをコピー。
$ 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/launch
あとは、systemdのサービスとして有効化して、起動。
$ sudo systemctl enable wildfly $ sudo systemctl start wildfly
確認。
$ curl -i localhost:8080
HTTP/1.1 200 OK
Connection: keep-alive
Last-Modified: Wed, 16 Jun 2021 07:47:30 GMT
Content-Length: 1504
Content-Type: text/html
Accept-Ranges: bytes
Date: Sat, 24 Jul 2021 12:58:46 GMT
<!DOCTYPE html>
<html>
<head>
<!-- proper charset -->
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
<title>Welcome to WildFly</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="StyleSheet" href="wildfly.css" type="text/css">
</head>
<body>
<div class="wrapper">
<div class="content">
<div class="logo">
<img src="wildfly_logo.png" alt="WildFly" border="0" />
</div>
<h1>Welcome to WildFly</h1>
<h3>Your WildFly instance is running.</h3>
<p><a href="https://docs.wildfly.org">Documentation</a> | <a href="https://github.com/wildfly/quickstart">Quickstarts</a> | <a href="/console">Administration
Console</a> </p>
<p><a href="https://wildfly.org">WildFly Project</a> |
<a href="https://community.jboss.org/en/wildfly">User Forum</a> |
<a href="https://issues.jboss.org/browse/WFLY">Report an issue</a></p>
<p class="logos"><a href="https://www.jboss.org"><img src="jbosscommunity_logo_hori_white.png" alt="JBoss and JBoss Community" width=
"195" height="37" border="0"></a></p>
<p class="note">To replace this page simply deploy your own war with / as its context path.<br />
To disable it, remove the "welcome-content" handler for location / in the undertow subsystem.</p>
</div>
</div>
</body>
</html>
OKですね。
ステータスを確認して
$ sudo systemctl status wildfly
● wildfly.service - The WildFly Application Server
Loaded: loaded (/etc/systemd/system/wildfly.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-07-24 21:57:57 JST; 1min 12s ago
Main PID: 13141 (launch.sh)
Tasks: 52 (limit: 2278)
Memory: 295.2M
CGroup: /system.slice/wildfly.service
├─13141 /bin/bash /opt/wildfly/bin/launch.sh standalone standalone.xml 0.0.0.0
├─13142 /bin/sh /opt/wildfly/bin/standalone.sh -c standalone.xml -b 0.0.0.0
└─13249 java -D[Standalone] -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jbos>
7月 24 21:57:57 ubuntu2004.localdomain systemd[1]: Started The WildFly Application Server.
停止。
$ sudo systemctl stop wildfly
確認できました。
もう少し、ファイルの中身を見てみる
これで終わってもなんなので、もう少しファイルの中身を見てみましょう。
まずは、ユニット定義ファイル。
/etc/systemd/system/wildfly.service
[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=/var/run/wildfly/wildfly.pid ExecStart=/opt/wildfly/bin/launch.sh $WILDFLY_MODE $WILDFLY_CONFIG $WILDFLY_BIND StandardOutput=null [Install] WantedBy=multi-user.target
/opt/wildfly/bin/launch.shを呼び出して起動するようになっています。
ExecStart=/opt/wildfly/bin/launch.sh $WILDFLY_MODE $WILDFLY_CONFIG $WILDFLY_BIND
/etc/wildfly/wildfly.confファイルは、環境変数の定義ファイルとして扱われます。
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1 EnvironmentFile=-/etc/wildfly/wildfly.conf
LAUNCH_JBOSS_IN_BACKGROUNDというのは、起動スクリプトに送ったシグナルをWildFlyのJavaプロセスにも
渡すためのものですね。
ところでこのユニットの後に起動するべきサービスとしてhttpdが書かれているのですが、Ubuntu Linuxの場合は
これはapache2にしないといけない気がしますね。
Before=httpd.service
環境変数の定義を見てみましょう。
/etc/wildfly/wildfly.conf
# The configuration you want to run WILDFLY_CONFIG=standalone.xml # The mode you want to run WILDFLY_MODE=standalone # The address to bind to WILDFLY_BIND=0.0.0.0
これは、ユニット定義ファイルのExecStartを見ると関係がわかりますね。
ExecStart=/opt/wildfly/bin/launch.sh $WILDFLY_MODE $WILDFLY_CONFIG $WILDFLY_BIND
launch.shを見ると、WILDFLY_MODEをdomainにするかどうかでドメインモードで起動するのかスタンドアロンモードで
起動するのかが変わるようです。
/opt/wildfly/bin/launch.sh
#!/bin/bash
if [ "x$WILDFLY_HOME" = "x" ]; then
WILDFLY_HOME="/opt/wildfly"
fi
if [[ "$1" == "domain" ]]; then
$WILDFLY_HOME/bin/domain.sh -c $2 -b $3
else
$WILDFLY_HOME/bin/standalone.sh -c $2 -b $3
fi
少し設定を変えてみる
例として、環境変数に設定を追加してみましょう。JBOSS_JAVA_SIZINGを使って、ヒープサイズをデフォルト値から
変更してみます。
/etc/wildfly/wildfly.conf
# The configuration you want to run WILDFLY_CONFIG=standalone.xml # The mode you want to run WILDFLY_MODE=standalone # The address to bind to WILDFLY_BIND=0.0.0.0 JBOSS_JAVA_SIZING='-Xms1G -Xmx1G -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m'
WildFlyを起動。
$ sudo systemctl start wildfly
確認。
$ sudo systemctl status wildfly
● wildfly.service - The WildFly Application Server
Loaded: loaded (/etc/systemd/system/wildfly.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-07-24 22:45:16 JST; 2min 22s ago
Main PID: 13614 (launch.sh)
Tasks: 52 (limit: 2278)
Memory: 309.9M
CGroup: /system.slice/wildfly.service
├─13614 /bin/bash /opt/wildfly/bin/launch.sh standalone standalone.xml 0.0.0.0
├─13624 /bin/sh /opt/wildfly/bin/standalone.sh -c standalone.xml -b 0.0.0.0
└─13718 java -D[Standalone] -server -Xms1G -Xmx1G -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.b>
7月 24 22:45:16 ubuntu2004.localdomain systemd[1]: Started The WildFly Application Server.
反映されていることが確認できました。
ソースコード上の変遷
今回、WildFly 24.0.0.Finalで確認したわけですが。
このスクリプト、いつからあるんでしょう?
WildFly 10.0.0.Finalの頃には、こちらにあったようです。
さらに、WildFly 9.0.0.Finalの時にはdocsの配下でもなかったようです。
WildFly 8.0.0.Finalの時にも含まれていたようですが、こちらは探すのは諦めました…。
そもそもJBoss ASの頃からあったみたいですね。
まとめ
WildFlyをsystemdのサービスユニットとして使うスクリプトを、ちょっと試してみました。
あくまでサンプルという位置づけのようなので、使う時には中身をちゃんと確認しないと、ですね。
まあ、実際に使うなら似たようなものは作ると思うので、そのベースとして。