LoginSignup
3
4

More than 3 years have passed since last update.

CentOS7にTomcat環境を構築するまで

Last updated at Posted at 2019-11-06

環境

CentOS 7
Java 11(Oracle JDK 11の最新)
Apache Tomcat 9 (最新)

Apache

まずはApacheをインストールしてみる。

# yum install httpd mod_ssl

Apacheの設定変更

/etc/httpd/conf/httpd.confを良き様に変更
今回は↓のような感じにしてみました。

# diff /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org 
316c316
< #AddDefaultCharset UTF-8
---
> AddDefaultCharset UTF-8
354,359d353
< 
< TraceEnable Off
< ServerTokens Prod
< Timeout 60
< KeepAlive On
< ServerSignature Off

welcom.conf無効化

welcome.confはリネームしてバーチャルホスト用の設定ファイルを別途作る。

mv -vi /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.org

www.example.com.conf

<VirtualHost *:80>
    ServerName tomcat-example
    DocumentRoot /www/tomcat/tomcat-example/html

    CustomLog /www/tomcat/tomcat-example/logs/tomcat-example-access_log combined
    ErrorLog  /www/tomcat/tomcat-example/logs/tomcat-example-error_log

<Directory "/www/tomcat/tomcat-example/html">
Require all granted
Options FollowSymLinks
DirectoryIndex index.html index.htm index.php
AllowOverride All
</Directory>


  <IfModule mod_proxy.c>
        ProxyPass / ajp://localhost:18009/
        ProxyPassReverse / ajp://localhost:18009/
  </IfModule>

</VirtualHost>

JDK

jdkの展開

サーバにアップした jdk-11.0.4_linux-x64_bin.tar.gz を /usr/java以下に展開する
jdk-11.0.4_linux-x64_bin.tar.gzのバイナリファイルはOracleサイトから取得しました。

# mkdir /usr/java
# cd /usr/local/src/
# ls -ltr
# tar zxvf jdk-11.0.4_linux-x64_bin.tar.gz 
# mv -vi jdk-11.0.4 /usr/java

Tomcat

tomcat9.0.24の展開

サーバにアップしたtomcat9.0.24を /www/tomcat/以下に展開する

# cd /usr/local/src/
# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.24/bin/apache-tomcat-9.0.24.tar.gz
# tar zxvf apache-tomcat-9.0.24.tar.gz 
# cp -ar apache-tomcat-9.0.24 /www/tomcat/tomcat-example/

展開できていることを確認後不要ファイルは削除

# rm apache-tomcat-9.0.24.tar.gz

シンボリックリンクを張る

ln -s apache-tomcat-9.0.24 tomcat

tomcatユーザ作成

tomcatユーザを作成し、tomcatのディレクトリの所有者をtomcatユーザに設定。

# useradd -M -s /sbin/nologin -d /home/tomcat tomcat
# chown -R tomcat.tomcat /www/tomcat/tomcat-example/apache-tomcat-9.0.24

tomcat管理画面アクセス制限

/www/tomcat/tomcat-example/apache-tomcat-9.0.24/webapps/manager/META-INF/context.xml

/www/tomcat/tomcat-example/apache-tomcat-9.0.24/webapps/host-manager/META-INF/context.xml
に管理画面へのアクセス制限の設定があるので、IPアドレスを追加します。

例として、192.168.1.100を追加したい場合は以下のように変更。

/www/tomcat/tomcat-example/apache-tomcat-9.0.24/webapps/manager/META-INF/context.xml

# diff /www/tomcat/tomcat-example/tomcat/webapps/manager/META-INF/context.xml /www/tomcat/tomcat-example/tomcat/webapps/manager/META-INF/context.xml.org 
20c20
<          allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192\.168\.1\.100+" />
---
>          allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

/www/tomcat/tomcat-example/apache-tomcat-9.0.24/webapps/host-manager/META-INF/context.xml

# diff /www/tomcat/tomcat-example/tomcat/webapps/host-manager/META-INF/context.xml /www/tomcat/tomcat-example/tomcat/webapps/host-manager/META-INF/context.xml.org 
20c20
<          allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192\.168\.1\.100+" />
---
>          allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

管理画面へアクセスするユーザーの設定

/www/tomcat/tomcat-example/apache-tomcat-9.0.24/conf/tomcat-users.xml
に設定を追記してあげる。

# diff /www/tomcat/tomcat-example/apache-tomcat-9.0.24/conf/tomcat-users.xml /www/tomcat/tomcat-example/apache-tomcat-9.0.24/conf/tomcat-users.xml.org 
44,50d43
<   <role rolename="manager-gui"/>
<   <role rolename="manager-status"/>
<   <role rolename="manager-script"/>
<   <role rolename="manager-jmx"/>
<   <role rolename="admin-gui"/>
<   <role rolename="admin-script"/>
<   <user username="admin" password="password" roles="manager-gui,manager-status,manager-script,manager-jmx,admin-gui,admin-script"/>

username=とpassword=の箇所は任意です。

tomcatポート変更

必要に応じてtomcatのポートを変更します。
今回はapacheの設定にてAJPのコネクターポートを18009にしているので、合わせます。

# diff /www/tomcat/tomcat-example/tomcat/conf/server.xml /www/tomcat/tomcat-example/tomcat/conf/server.xml.org
22c22
< <Server port="18005" shutdown="SHUTDOWN">
---
> <Server port="8005" shutdown="SHUTDOWN">
67c67
<          Define a non-SSL/TLS HTTP/1.1 Connector on port 18080
---
>          Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
69c69
<     <Connector port="18080" protocol="HTTP/1.1"
---
>     <Connector port="8080" protocol="HTTP/1.1"
71c71
<                redirectPort="18443" />
---
>                redirectPort="8443" />
75c75
<                port="18080" protocol="HTTP/1.1"
---
>                port="8080" protocol="HTTP/1.1"
77c77
<                redirectPort="18443" />
---
>                redirectPort="8443" />
79c79
<     <!-- Define a SSL/TLS HTTP/1.1 Connector on port 18443
---
>     <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
88c88
<     <Connector port="18443" protocol="org.apache.coyote.http11.Http11NioProtocol"
---
>     <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
96c96
<     <!-- Define a SSL/TLS HTTP/1.1 Connector on port 18443 with HTTP/2
---
>     <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
103c103
<     <Connector port="18443" protocol="org.apache.coyote.http11.Http11AprProtocol"
---
>     <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
115,116c115,116
<     <!-- Define an AJP 1.3 Connector on port 18009 -->
<     <Connector port="18009" protocol="AJP/1.3" redirectPort="18443" />
---
>     <!-- Define an AJP 1.3 Connector on port 8009 -->
>     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

tomcatの起動スクリプトを設置

環境変数ファイルを作成します。

# vi /www/tomcat/tomcat-example/apache-tomcat-9.0.24/bin/setenv.sh

# cat /www/tomcat/tomcat-example/tomcat/bin/setenv.sh
#JAVA_PATH
JAVA_HOME=/usr/java/jdk-11.0.4/
PATH=$PATH:$JAVA_HOME/bin
#Tomcat_PATH
CATALINA_HOME=/www/tomcat/tomcat-example/tomcat/
#Tomcat Memory Allocation
CATALINA_OPTS="-Xms512m -Xmx2048m -XX:PermSize=256m -XX:MaxPermSize=512m -verbose:gc -Dorg.apache.el.parser.SKIP_IDENTIFIER_CHECK=true"

メモリは適宜調整してください。

続いて起動スクリプトの作成

# vi /etc/systemd/system/tomcat.service

# cat /etc/systemd/system/tomcat.service 
[Unit]
Description=Apache Tomcat 9
After=network.target

[Service]
User=tomcat
Group=tomcat
Type=oneshot
PIDFile=/www/tomcat/tomcat-example/tomcat/tomcat.pid
RemainAfterExit=yes

ExecStart=/www/tomcat/tomcat-example/tomcat/bin/startup.sh
ExecStop=/www/tomcat/tomcat-example/tomcat/bin/shutdown.sh
ExecReStart=/www/tomcat/tomcat-example/tomcat/bin/shutdown.sh;/www/tomcat/tomcat-example/tomcat/bin/startup.sh

[Install]
WantedBy=multi-user.target

自動起動を有効にして、tomcatを起動します。

# systemctl enable tomcat
# systemctl start tomcat
# systemctl status tomcat.service

一旦、apache、tomcatの設定反映しておきます。

# systemctl restart httpd
# systemctl enable tomcat
# systemctl start tomcat

プロセスを確認

ps ax |grep httpd
ps ax |grep tomcat-kmew

tomcat ログ回転設定

ここを参考にさせていただきました。

# cat /etc/logrotate.d/tomcat9 

/www/tomcat/*/tomcat/logs/catalina.out {
    copytruncate
    daily
    rotate 150
    compress
    missingok
    create 0644 tomcat tomcat
}

catalina.out以外はtomcatに任せて削除のみcrontabに登録
crontabの中身

#delete old tomcat logs
3 3 * * 3 root /usr/bin/find /www/tomcat/*/tomcat/logs -mtime +150 -type f -print | xargs rm -f

150日保存としていますが、好きなようにしてください。

3
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
3
4