2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Tomcatをデーモンで起動する

Last updated at Posted at 2018-01-20

Tomcat をインストールしても、デフォルトではデーモンで起動してくれないので、デーモンで起動するようにした際の備忘録。

環境

Software Version
OS CentOS7.3
Tomcat 7.0.82

前提

・JDKは、wgetでダウンロードできないので、以下ディレクトリへ配備済み。

/usr/java/jdk1.8.0_152

tomcat ユーザを作成する。

groupadd tomcat
useradd -M tomcat -g tomcat

gcc をインストールする。

yum install gcc
=======================================================================================================================================
 Package                         アーキテクチャー        バージョン                           リポジトリー                        容量
=======================================================================================================================================
インストール中:
 gcc                             x86_64                  4.8.5-16.el7_4.1                     ol7_x86_64_latest                   16 M
依存性関連でのインストールをします:
 cpp                             x86_64                  4.8.5-16.el7_4.1                     ol7_x86_64_latest                  5.9 M
 glibc-devel                     x86_64                  2.17-196.el7_4.2                     ol7_x86_64_latest                  1.1 M
 glibc-headers                   x86_64                  2.17-196.el7_4.2                     ol7_x86_64_latest                  675 k
 kernel-headers                  x86_64                  3.10.0-693.11.1.el7                  ol7_x86_64_latest                  6.0 M
 libmpc                          x86_64                  1.0.1-3.el7                          ol7_x86_64_latest                   49 k
 mpfr                            x86_64                  3.1.1-4.el7                          ol7_x86_64_latest                  198 k
依存性関連での更新をします:
 glibc                           i686                    2.17-196.el7_4.2                     ol7_x86_64_latest                  4.2 M
 glibc                           x86_64                  2.17-196.el7_4.2                     ol7_x86_64_latest                  3.6 M
 glibc-common                    x86_64                  2.17-196.el7_4.2                     ol7_x86_64_latest                   11 M
 libgcc                          i686                    4.8.5-16.el7_4.1                     ol7_x86_64_latest                  105 k
 libgcc                          x86_64                  4.8.5-16.el7_4.1                     ol7_x86_64_latest                   98 k
 libgomp                         x86_64                  4.8.5-16.el7_4.1                     ol7_x86_64_latest                  153 k

トランザクションの要約
=======================================================================================================================================
インストール  1 パッケージ (+6 個の依存関係のパッケージ)
更新                       ( 6 個の依存関係のパッケージ)

Tomcat をダウンロードする。

適当なディレクトリへダウンロードする。

wget http://ftp.jaist.ac.jp/pub/apache/tomcat/tomcat-7/v7.0.82/bin/apache-tomcat-7.0.82.tar.gz

ダウンロードしたTomcatを解凍する。

tar -xvzf ./apache-tomcat-7.0.82.tar.gz

解凍したTomcat を配備する。

今回は、/opt/の配下に配備した。

mv ./apache-tomcat-7.0.82/ /opt/.

tomcat-daemon-native を解凍する。

cd /opt/apache-tomcat-7.0.82/bin/
tar -xvzf commons-daemon-native.tar.gz

解凍したtomcat-daemon-native ディレクトリへ移動する。

cd ./commons-daemon-1.0.15-native-src/unix/

tomcat-daemon-native をconfigure する。

export CFLAGS=-m64
export LDFLAGS=-m64
./configure --with-java=/usr/java/jdk1.8.0_152/

*** Current host ***
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking cached host system type... ok
*** C-Language compilation tools ***
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking for ranlib... ranlib
checking for strip... strip
*** Host support ***
checking C flags dependant on host system type... ok
*** Java compilation tools ***
checking JAVA_HOME... /usr/java/jdk1.8.0_152/
checking for JDK os include directory...  linux
gcc flags added
checking how to run the C preprocessor... gcc -E
checking for egrep... grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking sys/capability.h usability... no
checking sys/capability.h presence... no
checking for sys/capability.h... no
configure: WARNING: cannot find headers for libcap
*** Writing output files ***
configure: creating ./config.status
config.status: creating Makefile
config.status: creating Makedefs
config.status: creating native/Makefile
*** All done ***
Now you can issue "make"

エラーが無いことを確認し、make する。

make

(cd native; make  all)
make[1]: ディレクトリ `/opt/apache-tomcat-7.0.82/bin/commons-daemon-1.0.15-native-src/unix/native' に入ります
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c jsvc-unix.c -o jsvc-unix.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c arguments.c -o arguments.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c debug.c -o debug.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c dso-dlfcn.c -o dso-dlfcn.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c dso-dyld.c -o dso-dyld.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c help.c -o help.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c home.c -o home.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c java.c -o java.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c location.c -o location.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c replace.c -o replace.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c locks.c -o locks.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes   -I/usr/java/jdk1.8.0_152//include -I/usr/java/jdk1.8.0_152//include/linux -c signals.c -o signals.o
ar cr libservice.a arguments.o debug.o dso-dlfcn.o dso-dyld.o help.o home.o java.o location.o replace.o locks.o signals.o
ranlib libservice.a
gcc   jsvc-unix.o libservice.a -ldl -lpthread -o ../jsvc
make[1]: ディレクトリ `/opt/apache-tomcat-7.0.82/bin/commons-daemon-1.0.15-native-src/unix/native' から出ます

tomcat-daemon-native (jsvc) が作成されていることを確認する。

ls- altr jsvc
-rwxr-xr-x 1 root  root  170552 12月 14 15:17 jsvc

作成したtomcat-daemon-native (jsvc) を、tomcat インストールディレクトリ配下の/bin ディレクトリへ移動する。

mv ./jsvc ../../.

Tomcat を配備したディレクトリのオーナーを tomcat へ変更する。

chown -R tomcat. /opt/apache-tomcat-7.0.82/

tomcat 起動スクリプトを編集する。

vi /etc/systemd/system/tomcat.service

[Unit]
 Description=Apache Tomcat 7
 After=network.target

[Service]
 Type=forking
 User=tomcat
 Group=tomcat
 EnvironmentFile=/etc/sysconfig/tomcat

 ExecStart=/opt/apache-tomcat-7.0.82/bin/jsvc \
            -Djava.util.logging.config.file=${LOGGING_PROPERTIES} \
            -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager \
            -Dcatalina.home=${CATALINA_HOME} \
            -Dcatalina.base=${CATALINA_BASE} \
            -Djava.io.tmpdir=${CATALINA_TMPDIR} \
            -java-home ${JAVA_HOME} \
            -pidfile ${CATALINA_PID} \
            ${CATALINA_OPTS} \
            -cp ${CLASS_PATH} \
            org.apache.catalina.startup.Bootstrap

 ExecStop=/opt/apache-tomcat-7.0.82/bin/jsvc \
            -pidfile ${CATALINA_PID} \
            -stop \
            org.apache.catalina.startup.Bootstrap

[Install]
 WantedBy=multi-user.target

tomcat 起動スクリプト用環境変数ファイルを作成する。

vi /etc/sysconfig/tomcat

JAVA_HOME="/usr/java/jdk1.8.0_152"
JRE_HOME="/usr/java/jdk1.8.0_152"

CATALINA_HOME="/opt/apache-tomcat-7.0.82"
CATALINA_BASE="/opt/apache-tomcat-7.0.82"
CATALINA_TMPDIR="/opt/apache-tomcat-7.0.82/temp"
CATALINA_PID="/opt/apache-tomcat-7.0.82/tomcat.pid"
LOGGING_PROPERTIES="/opt/apache-tomcat-7.0.82/conf/logging.properties"

CATALINA_OPTS="-XX:+AggressiveHeap -XX:+AggressiveOpts -Dorg.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false -XX:+PrintGCDetails -XX:PermSize=128m -XX:MaxPermSize=256m"

CLASS_PATH="/usr/java/jdk1.8.0_152/lib/tools.jar:/opt/apache-tomcat-7.0.82/bin/commons-daemon.jar:/opt/apache-tomcat-7.0.82/bin/tomcat-juli.jar:/opt/apache-tomcat-7.0.82/bin/bootstrap.jar"

DAEMON をリロードする。

systemctl daemon-reload

tomcat を起動する。

systemctl start tomcat.service

tomcat の状態を確認する。

systemctl status tomcat.service

● tomcat.service - Apache Tomcat 7
   Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: active (running) since 月 2017-12-18 14:02:40 JST; 1 months 3 days ago
 Main PID: 25287 (jsvc)
   CGroup: /system.slice/tomcat.service
           tq25287 jsvc.exec -Djava.util.logging.config.file=/opt/apache-tomcat-7.0.82/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dcatalina.home=/opt/apache-tomcat-7.0.82 -Dcatalina.base...
           mq25288 jsvc.exec -Djava.util.logging.config.file=/opt/apache-tomcat-7.0.82/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dcatalina.home=/opt/apache-tomcat-7.0.82 -Dcatalina.base...

tomcat の自動起動を有効にする。

systemctl enable tomcat.service

tomcat の自動起動が有効になっているか確認する。

systemctl list-unit-files tomcat.service
UNIT FILE      STATE
tomcat.service enabled

できた

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?