ぱと隊長日誌

ブログ運用もエンジニアとしての生き方も模索中

Apache Tomcat の環境変数設定ガイド

この記事では、Apache Tomcat環境変数の設定方法について、特に JVM パラメータ(例:Java ヒープサイズの指定)を通して解説します。

環境変数

Apache Tomcat にはいくつかの環境変数があります。以下に代表的なものを示します。

環境変数 説明
CATALINA_HOME Tomcat バイナリのルートディレクトリのパス。
JRE_HOME JRE のインストールパス。
CATALINA_OPTS Tomcat 起動時に使用する java コマンドの追加オプション。

Tomcat環境変数を設定する前に、まずは公式ドキュメント (RUNNING.txt) の参照をお勧めします。

Tomcat 10.1 の場合、以下のURLから確認できます。
https://tomcat.apache.org/tomcat-10.1-doc/RUNNING.txt

環境変数の設定例

JVM のパラメータ設定(例:Java ヒープサイズの指定)を通して、環境変数の設定方法を説明します。公式ドキュメントから該当部分を引用し、続けてポイントを解説します。ドキュメントには Windows / Linux の両方の例が示されていますが、この記事では Linux を前提に説明します。

(1) CATALINA_HOME と CATALINA_BASE の設定

(3.1) Set CATALINA_HOME (required) and CATALINA_BASE (optional)

The CATALINA_HOME environment variable should be set to the location of the root directory of the "binary" distribution of Tomcat.

The Tomcat startup scripts have some logic to set this variable automatically if it is absent, based on the location of the startup script in *nix and on the current directory in Windows. That logic might not work in all circumstances, so setting the variable explicitly is recommended.

The CATALINA_BASE environment variable specifies location of the root directory of the "active configuration" of Tomcat. It is optional. It defaults to be equal to CATALINA_HOME.

CATALINA_HOME, CATALINA_BASE を明示的に設定していなければ、Tomcat の起動スクリプトが自動的に設定を試みます。ただし、ロジックが正しく動作しない場合を考慮し、明示的に設定することが推奨されています。

CATALINA_BASE を明示的に設定する例が他の公式ドキュメントに記載されています。この例では起動スクリプト実行前に設定しています。

How to Use CATALINA_BASE

The CATALINA_BASE property is an environment variable. You can set it before you execute the Tomcat start script, for example:

On Unix: CATALINA_BASE=/tmp/tomcat_base1 bin/catalina.sh start

Apache Tomcat 10 (10.1.30) - Introduction

(2) CATALINA_OPTS と JVM パラメータの設定

(3.3) Other variables (optional)

One frequently used variable is CATALINA_OPTS. It allows specification of additional options for the java command that starts Tomcat.

A similar variable is JAVA_OPTS. It is used less frequently. It allows specification of options that are used both to start and to stop Tomcat as well as for other commands.

Note: Do not use JAVA_OPTS to specify memory limits. You do not need much memory for a small process that is used to stop Tomcat. Those settings belong to CATALINA_OPTS.

JVM パラメータの指定は CATALINA_OPTS を利用するのが適切です。

(3) setenv スクリプトの利用

(3.4) Using the "setenv" script (optional, recommended)

Apart from CATALINA_HOME and CATALINA_BASE, all environment variables can be specified in the "setenv" script. The script is placed either into CATALINA_BASE/bin or into CATALINA_HOME/bin directory and is named setenv.bat (on Windows) or setenv.sh (on *nix). The file has to be readable.

By default the setenv script file is absent. If the script file is present both in CATALINA_BASE and in CATALINA_HOME, the one in CATALINA_BASE is preferred.

For example, to configure the JRE_HOME and CATALINA_PID variables you can create the following script file:

On *nix, $CATALINA_BASE/bin/setenv.sh:

JRE_HOME=/usr/java/latest
CATALINA_PID="/run/tomcat.pid"

Tomcat では、環境変数の設定を setenv スクリプトに記述することも可能です。Linux であれば、CATALINA_BASE/bin, CATALINA_HOME/bin のいずれかに setenv.sh ファイルを作成し、そのファイル内に環境変数を設定します。

例えば、ヒープサイズの設定であれば、setenv.sh に以下のような記述を行います。

CATALINA_OPTS="-Xms128m -Xmx1024m"

まとめ

Tomcat環境変数の設定は、正確な動作を保証するために重要です。CATALINA_HOME や CATALINA_BASE は明示的に設定するのが望ましく、JVM のパラメータは CATALINA_OPTS を使って指定します。また、setenv スクリプトを活用することで、柔軟な設定が可能です。今回紹介したガイドに従い、公式ドキュメントに基づく設定を行うことで、確実な動作を期待できます。