준호씨의 블로그

tomcat - 환경변수 세팅은 bin/setenv.sh 에서 본문

개발이야기

tomcat - 환경변수 세팅은 bin/setenv.sh 에서

준호씨 2022. 3. 24. 00:33
반응형

tomcat을 실행할 때 환경변수를 적용하는 방법에는 여러 가지 방법이 있습니다. 결론부터 말하면 bin/setenv.sh 파일을 만들어서 거기다가 환경변수를 넣어주면 됩니다.

환경변수

잠깐 환경변수에 대해 짚고 넘어가 보면요. 환경변수는 jvm이나 tomcat, 애플리케이션의 설정을 적용하는 데 사용할 수 있습니다. 예를 들어 다음과 같은 설정은 JVM Max Heap Size를 4G로 설정하고 JMX설정을 활성화합니다.

export CATALINA_OPTS="-Xmx4G \
                  -Dcom.sun.management.jmxremote \
                  -Dcom.sun.management.jmxremote.port=9090 \
                  -Dcom.sun.management.jmxremote.authenticate=false \
                  -Dcom.sun.management.jmxremote.ssl=false"

~/.bashrc, ~/.zshrc?

그냥 ~/.bashrc, ~/.zshrc 같은 파일에 미리 세팅을 해 두는 방법도 있겠지만 tomcat을 실행하는 상황에 따라 해당 설정이 적용되지 않을 수도 있기에 특별한 경우가 아니라면 사용하지 않는 것이 좋겠습니다.

별도 스크립트? tomcatctl?

별도 스크립트 작성 방법도 있습니다. tomcatctl 같은 스크립트를 만들고 거기에 필요한 환경변수들을 지정하는 것입니다. startup.sh나 catalina.sh를 실행하기 전에 tomcatctl에서 각종 설정을 입력할 수 있습니다. tomcatctl을 레거시 시스템을 운영할 때 자주 사용하던 방식인데 tomcatctl start/stop/restart 등의 명령어를 만들어 두고 하나의 스크립트로 tomcat을 제어할 때 사용하던 방식입니다.

bin/startup.sh?

startup.sh에 환경변수를 미리 기입해 둘 수도 있습니다. 보통 tomcat을 설치하고 사용하면 startup.sh를 이용해서 구동을 할 건데요. tomcat을 그냥 깔고 사용하다 보면 startup.sh에 넣는 게 만만해 보입니다.

bin/catalina.sh?

catalina.sh에 환경변수를 넣는 방법도 있습니다. startup.sh이 catalina.sh를 실행하기도 하고 startup.sh를 안 쓰고 catalina.sh를 사용하는 사람들은 여기에 넣는 것을 선호할 수도 있겠습니다.

bin/setenv.sh

하지만 catalina.sh파일을 열어 보면 다음과 같은 설명이 적혀있습니다. 변수들을 catalina.sh에 작성하지 말고 setenv.sh에 작성하라고 합니다.

# Environment Variable Prerequisites
#
#   Do not set the variables in this script. Instead put them into a script
#   setenv.sh in CATALINA_BASE/bin to keep your customizations separate.
#
#   CATALINA_HOME   May point at your Catalina "build" directory.
#
#   CATALINA_BASE   (Optional) Base directory for resolving dynamic portions
#                   of a Catalina installation.  If not present, resolves to
#                   the same directory that CATALINA_HOME points to.
#
#   CATALINA_OUT    (Optional) Full path to a file where stdout and stderr
#                   will be redirected.
#                   Default is $CATALINA_BASE/logs/catalina.out
#
#   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
#                   "run" or "debug" command is executed.
#                   Include here and not in JAVA_OPTS all options, that should
#                   only be used by Tomcat itself, not by the stop process,
#                   the version command etc.
#                   Examples are heap size, GC logging, JMX ports etc.
#
#   CATALINA_TMPDIR (Optional) Directory path location of temporary directory
#                   the JVM should use (java.io.tmpdir).  Defaults to
#                   $CATALINA_BASE/temp.
#
#   JAVA_HOME       Must point at your Java Development Kit installation.
#                   Required to run the with the "debug" argument.
#
#   JRE_HOME        Must point at your Java Runtime installation.
#                   Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME
#                   are both set, JRE_HOME is used.
#
#   JAVA_OPTS       (Optional) Java runtime options used when any command
#                   is executed.
#                   Include here and not in CATALINA_OPTS all options, that
#                   should be used by Tomcat and also by the stop process,
#                   the version command etc.
#                   Most options should go into CATALINA_OPTS.
...

여러 방법들로 환경변수들을 작성할 수 있지만 정석은 setenv.sh를 이용하는 것이었습니다.

catalina.sh를 보다 보면 setenv.sh를 찾아서 실행시키는 곳이 있습니다.

if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
  . "$CATALINA_BASE/bin/setenv.sh"
elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
  . "$CATALINA_HOME/bin/setenv.sh"
fi

다만 좀 의아한 것은 setenv.sh파일이 존재하고 있었다면 그 파일을 수정할 생각을 하기 좋은데 기본적으로 setenv.sh파일이 없기에 파일을 따로 만들어 주어야 한다는 점입니다.

아무튼 tomcat 환경변수를 어디다 설정하느냐에 대한 결론은 setenv.sh입니다. 특별한 이유가 없다면 tomcat 환경변수 설정은 setenv.sh에서 하면 됩니다.

반응형
Comments