AL2 ApacheのHTTP ServerとTomcatでWEBサーバ構築

Amazon Linux 2にApache HTTP ServerとApache Tomcatをインストールして、WEBサーバを構築します。

システム環境

Amazon Linux 2 Kernel 5.10の環境で構築しました。

$ cat /etc/system-release
Amazon Linux release 2 (Karoo)
$ uname -srv
Linux 5.10.109-104.500.amzn2.x86_64 #1 SMP Wed Apr 13 20:31:43 UTC 2022

Java11のインストール

Java11をインストールします。

$ sudo yum install java-11-amazon-corretto-headless.x86_64
   : (割愛)
Running transaction
  Installing : fontpackages-filesystem-1.44-8.amzn2.noarch                      1/10
  Installing : libxslt-1.1.28-6.amzn2.x86_64                                    2/10
  Installing : python-lxml-3.2.1-4.amzn2.0.3.x86_64                             3/10
  Installing : python-javapackages-3.4.1-11.amzn2.noarch                        4/10
  Installing : javapackages-tools-3.4.1-11.amzn2.noarch                         5/10
  Installing : dejavu-fonts-common-2.33-6.amzn2.noarch                          6/10
  Installing : dejavu-sans-fonts-2.33-6.amzn2.noarch                            7/10
  Installing : fontconfig-2.13.0-4.3.amzn2.x86_64                               8/10
  Installing : log4j-cve-2021-44228-hotpatch-1.1-16.amzn2.noarch                9/10
Created symlink from /etc/systemd/system/multi-user.target.wants/log4j-cve-2021-44228-hotpatch.service
 to /usr/lib/systemd/system/log4j-cve-2021-44228-hotpatch.service.
  Installing : 1:java-11-amazon-corretto-headless-11.0.15+9-1.amzn2.x86_64    10/10
   : (割愛)
Installed:
  java-11-amazon-corretto-headless.x86_64 1:11.0.15+9-1.amzn2

Dependency Installed:
  dejavu-fonts-common.noarch 0:2.33-6.amzn2                            dejavu-sans-fonts.noarch 0:2.33-6.amzn2
  fontconfig.x86_64 0:2.13.0-4.3.amzn2                                 fontpackages-filesystem.noarch 0:1.44-8.amzn2
  javapackages-tools.noarch 0:3.4.1-11.amzn2                           libxslt.x86_64 0:1.1.28-6.amzn2
  log4j-cve-2021-44228-hotpatch.noarch 0:1.1-16.amzn2                  python-javapackages.noarch 0:3.4.1-11.amzn2
  python-lxml.x86_64 0:3.2.1-4.amzn2.0.3

Complete!

インストールが正常であるかを兼ねて、バージョンを確認します。

$ java --version
openjdk 11.0.15 2022-04-19 LTS
OpenJDK Runtime Environment Corretto-11.0.15.9.1 (build 11.0.15+9-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.15.9.1 (build 11.0.15+9-LTS, mixed mode)

Apache HTTP Serverのインストール

HTTP Serverをインストールします。

$ sudo yum install httpd.x86_64
   : (割愛)
Running transaction
  Installing : apr-1.7.0-9.amzn2.x86_64                                        1/9
  Installing : apr-util-bdb-1.6.1-5.amzn2.0.2.x86_64                           2/9
  Installing : apr-util-1.6.1-5.amzn2.0.2.x86_64                               3/9
  Installing : httpd-tools-2.4.53-1.amzn2.x86_64                               4/9
  Installing : generic-logos-httpd-18.0.0-4.amzn2.noarch                       5/9
  Installing : mailcap-2.1.41-2.amzn2.noarch                                   6/9
  Installing : httpd-filesystem-2.4.53-1.amzn2.noarch                          7/9
  Installing : mod_http2-1.15.19-1.amzn2.0.1.x86_64                            8/9
  Installing : httpd-2.4.53-1.amzn2.x86_64                                     9/9
   : (割愛)
Installed:
  httpd.x86_64 0:2.4.53-1.amzn2

Dependency Installed:
  apr.x86_64 0:1.7.0-9.amzn2                    apr-util.x86_64 0:1.6.1-5.amzn2.0.2
  apr-util-bdb.x86_64 0:1.6.1-5.amzn2.0.2       generic-logos-httpd.noarch 0:18.0.0-4.amzn2
  httpd-filesystem.noarch 0:2.4.53-1.amzn2      httpd-tools.x86_64 0:2.4.53-1.amzn2
  mailcap.noarch 0:2.1.41-2.amzn2               mod_http2.x86_64 0:1.15.19-1.amzn2.0.1

Complete!

インストールが正常であるかを兼ねて、バージョンを確認します。

$ httpd -v
Server version: Apache/2.4.53 ()
Server built:   Apr 12 2022 12:00:44

systemdによる起動を行い、自動起動設定をします。

$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd.service(8)
$ sudo systemctl start httpd
$ sudo systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service
 to /usr/lib/systemd/system/httpd.service.
$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2022-05-07 01:26:19 UTC; 1min 39s ago
     Docs: man:httpd.service(8)
 Main PID: 3654 (httpd)
   Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├3654 /usr/sbin/httpd -DFOREGROUND
           ├3661 /usr/sbin/httpd -DFOREGROUND
           ├3662 /usr/sbin/httpd -DFOREGROUND
           ├3663 /usr/sbin/httpd -DFOREGROUND
           ├3664 /usr/sbin/httpd -DFOREGROUND
           └3665 /usr/sbin/httpd -DFOREGROUND
   : (割愛)
Hint: Some lines were ellipsized, use -l to show in full.

AWSの「パブリック IPv4 DNS」で画面が表示されるかを確認します。

URL ⇒ http://ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com/

systemdによる停止を行います。

$ sudo systemctl stop httpd
$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Sat 2022-05-07 01:30:12 UTC; 10s ago
     Docs: man:httpd.service(8)
  Process: 3654 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
 Main PID: 3654 (code=exited, status=0/SUCCESS)
   Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
   : (割愛)
Hint: Some lines were ellipsized, use -l to show in full.

Apache Tomcatのインストール

CoreリポジトリーにあるTomcatは、古めですので、Extrasリポジトリーからインストールします。

$ yum list | grep tomcat
tomcat.noarch                          7.0.76-10.amzn2.0.2            amzn2-core
tomcat-admin-webapps.noarch            7.0.76-10.amzn2.0.2            amzn2-core
tomcat-docs-webapp.noarch              7.0.76-10.amzn2.0.2            amzn2-core
tomcat-el-2.2-api.noarch               7.0.76-10.amzn2.0.2            amzn2-core
tomcat-javadoc.noarch                  7.0.76-10.amzn2.0.2            amzn2-core
tomcat-jsp-2.2-api.noarch              7.0.76-10.amzn2.0.2            amzn2-core
tomcat-jsvc.noarch                     7.0.76-10.amzn2.0.2            amzn2-core
tomcat-lib.noarch                      7.0.76-10.amzn2.0.2            amzn2-core
tomcat-native.i686                     1.2.30-1.amzn2                 amzn2-core
tomcat-native.x86_64                   1.2.30-1.amzn2                 amzn2-core
tomcat-servlet-3.0-api.noarch          7.0.76-10.amzn2.0.2            amzn2-core
tomcat-webapps.noarch                  7.0.76-10.amzn2.0.2            amzn2-core
tomcatjss.noarch                       7.2.5-1.amzn2.0.1              amzn2-core
$ amazon-linux-extras list | grep tomcat9
 52  tomcat9                  available    [ =stable ]
$ sudo amazon-linux-extras install tomcat9
    : (割愛)
Running transaction
  Installing : libICE-1.0.9-9.amzn2.0.2.x86_64                                1/25
  Installing : 1:java-17-amazon-corretto-headless-17.0.3+6-1.amzn2.1.x86_64   2/25
  Installing : tomcat-el-3.0-api-9.0.58-1.amzn2.0.1.noarch                    3/25
  Installing : tomcat-servlet-4.0-api-9.0.58-1.amzn2.0.1.noarch               4/25
  Installing : tomcat-jsp-2.3-api-9.0.58-1.amzn2.0.1.noarch                   5/25
  Installing : libSM-1.2.2-2.amzn2.0.2.x86_64                                 6/25
  Installing : alsa-lib-1.1.4.1-2.amzn2.x86_64                                7/25
  Installing : libXau-1.0.8-2.1.amzn2.0.2.x86_64                              8/25
  Installing : libxcb-1.12-1.amzn2.0.2.x86_64                                 9/25
  Installing : libX11-common-1.6.7-3.amzn2.0.2.noarch                        10/25
  Installing : libX11-1.6.7-3.amzn2.0.2.x86_64                               11/25
  Installing : libXext-1.3.3-3.amzn2.0.2.x86_64                              12/25
  Installing : libXi-1.7.9-1.amzn2.0.2.x86_64                                13/25
  Installing : libXrender-0.9.10-1.amzn2.0.2.x86_64                          14/25
  Installing : libXrandr-1.5.1-2.amzn2.0.3.x86_64                            15/25
  Installing : libXtst-1.2.3-1.amzn2.0.2.x86_64                              16/25
  Installing : libXinerama-1.1.3-2.1.amzn2.0.2.x86_64                        17/25
  Installing : libXt-1.1.5-3.amzn2.0.2.x86_64                                18/25
  Installing : dejavu-sans-mono-fonts-2.33-6.amzn2.noarch                    19/25
  Installing : dejavu-serif-fonts-2.33-6.amzn2.noarch                        20/25
  Installing : 1:java-17-amazon-corretto-17.0.3+6-1.amzn2.1.x86_64           21/25
  Installing : 1:ecj-4.5.2-3.amzn2.0.2.x86_64                                22/25
  Installing : tomcat-lib-9.0.58-1.amzn2.0.1.noarch                          23/25
  Installing : apache-commons-daemon-1.0.13-7.amzn2.x86_64                   24/25
  Installing : tomcat-9.0.58-1.amzn2.0.1.noarch                              25/25
   : (割愛)
Installed:
  tomcat.noarch 0:9.0.58-1.amzn2.0.1

Dependency Installed:
  alsa-lib.x86_64 0:1.1.4.1-2.amzn2                               apache-commons-daemon.x86_64 0:1.0.13-7.amzn2
  dejavu-sans-mono-fonts.noarch 0:2.33-6.amzn2                    dejavu-serif-fonts.noarch 0:2.33-6.amzn2
  ecj.x86_64 1:4.5.2-3.amzn2.0.2                                  java-17-amazon-corretto.x86_64 1:17.0.3+6-1.amzn2.1
  java-17-amazon-corretto-headless.x86_64 1:17.0.3+6-1.amzn2.1    libICE.x86_64 0:1.0.9-9.amzn2.0.2
  libSM.x86_64 0:1.2.2-2.amzn2.0.2                                libX11.x86_64 0:1.6.7-3.amzn2.0.2
  libX11-common.noarch 0:1.6.7-3.amzn2.0.2                        libXau.x86_64 0:1.0.8-2.1.amzn2.0.2
  libXext.x86_64 0:1.3.3-3.amzn2.0.2                              libXi.x86_64 0:1.7.9-1.amzn2.0.2
  libXinerama.x86_64 0:1.1.3-2.1.amzn2.0.2                        libXrandr.x86_64 0:1.5.1-2.amzn2.0.3
  libXrender.x86_64 0:0.9.10-1.amzn2.0.2                          libXt.x86_64 0:1.1.5-3.amzn2.0.2
  libXtst.x86_64 0:1.2.3-1.amzn2.0.2                              libxcb.x86_64 0:1.12-1.amzn2.0.2
  tomcat-el-3.0-api.noarch 0:9.0.58-1.amzn2.0.1                   tomcat-jsp-2.3-api.noarch 0:9.0.58-1.amzn2.0.1
  tomcat-lib.noarch 0:9.0.58-1.amzn2.0.1                          tomcat-servlet-4.0-api.noarch 0:9.0.58-1.amzn2.0.1

Complete!

下記を追加でインストールします。

$ sudo yum install tomcat-admin-webapps.noarch tomcat-webapps.noarch tomcat-docs-webapp.noarch
   : (割愛)
Running transaction
  Installing : xml-commons-apis-1.4.01-16.amzn2.noarch                               1/8
  Installing : xml-commons-resolver-1.2-15.amzn2.noarch                              2/8
  Installing : xalan-j2-2.7.1-23.1.amzn2.noarch                                      3/8
  Installing : xerces-j2-2.11.0-17.amzn2.noarch                                      4/8
  Installing : jakarta-taglibs-standard-1.1.2-14.1.amzn2.noarch                      5/8
  Installing : tomcat-webapps-9.0.58-1.amzn2.0.1.noarch                              6/8
  Installing : tomcat-docs-webapp-9.0.58-1.amzn2.0.1.noarch                          7/8
  Installing : tomcat-admin-webapps-9.0.58-1.amzn2.0.1.noarch                        7/8
   : (割愛)
Installed:
  tomcat-admin-webapps.noarch 0:9.0.58-1.amzn2.0.1    tomcat-docs-webapp.noarch 0:9.0.58-1.amzn2.0.1
  tomcat-webapps.noarch 0:9.0.58-1.amzn2.0.1

Dependency Installed:
  jakarta-taglibs-standard.noarch 0:1.1.2-14.1.amzn2  xalan-j2.noarch 0:2.7.1-23.1.amzn2
  xerces-j2.noarch 0:2.11.0-17.amzn2                  xml-commons-apis.noarch 0:1.4.01-16.amzn2
  xml-commons-resolver.noarch 0:1.2-15.amzn2

Complete!

インストールが正常であるかを兼ねて、バージョンを確認します。

$ tomcat version
Server version: Apache Tomcat/9.0.58
Server built:   Feb 28 2022 18:33:55 UTC
Server number:  9.0.58.0
OS Name:        Linux
OS Version:     5.10.109-104.500.amzn2.x86_64
Architecture:   amd64
JVM Version:    17.0.3+6-LTS
JVM Vendor:     Amazon.com Inc.

systemdによる起動を行い、自動起動設定をします。

$ systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
$ sudo systemctl start tomcat
$ sudo systemctl enable tomcat
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service
 to /usr/lib/systemd/system/tomcat.service.
$ systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2022-05-07 13:15:55 UTC; 2min 28s ago
 Main PID: 2835 (java)
   CGroup: /system.slice/tomcat.service
           └2835 /usr/lib/jvm/jre/bin/java -Djavax.sql.DataSource.Factory=org.apache.commons.dbcp.Basi...
   : (割愛)
Hint: Some lines were ellipsized, use -l to show in full.

AWSの「パブリック IPv4 DNS」で画面が表示されるかを確認します。

URL ⇒ http://ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com:8080/

systemdによる停止を行います。

$ sudo systemctl stop tomcat
$ systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Sat 2022-05-07 13:19:31 UTC; 2s ago
  Process: 4892 ExecStart=/usr/libexec/tomcat/server start (code=exited, status=143)
 Main PID: 4892 (code=exited, status=143)  ← 戻り値が143で返ってくる
   : (割愛)
Hint: Some lines were ellipsized, use -l to show in full.
$ cat /usr/lib/systemd/system/tomcat.service | grep SuccessExitStatus
SuccessExitStatus=143  ← 戻り値が143で正常終了

Java17からJava11に変更

$ java --version
openjdk 17.0.3 2022-04-19 LTS
OpenJDK Runtime Environment Corretto-17.0.3.6.1 (build 17.0.3+6-LTS)
OpenJDK 64-Bit Server VM Corretto-17.0.3.6.1 (build 17.0.3+6-LTS, mixed mode, sharing)
$ sudo alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java
*+ 2           /usr/lib/jvm/java-17-amazon-corretto.x86_64/bin/java

Enter to keep the current selection[+], or type selection number: 1
$ java --version
openjdk 11.0.15 2022-04-19 LTS
OpenJDK Runtime Environment Corretto-11.0.15.9.1 (build 11.0.15+9-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.15.9.1 (build 11.0.15+9-LTS, mixed mode)

HTTP ServerとTomcatの連携

$ cat /etc/httpd/conf.modules.d/*.conf | grep mod_proxy
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_express_module modules/mod_proxy_express.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_hcheck_module modules/mod_proxy_hcheck.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
LoadModule proxy_uwsgi_module modules/mod_proxy_uwsgi.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
LoadModule proxy_http2_module modules/mod_proxy_http2.so
$ cat /etc/httpd/conf/httpd.conf | grep conf.modules.d
Include conf.modules.d/*.conf
$ cd /etc/httpd/conf.d
$ sudo vi proxy-ajp.conf

# 全てのリクエストをポート8009にプロキシーする
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/

$ cd /etc/tomcat
$ sudo cp -p server.xml BK-server.xml.original
$ sudo vi server.xml

    <!-- コメントアウト化 -->
    <!--
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->

    <!-- 下記を追加 -->
    <Connector protocol="AJP/1.3"
               address="0.0.0.0"
               port="8009"
               redirectPort="8443"
               secretRequired="false" />

HTTP serverとTomcatの再起動

$ sudo systemctl stop httpd
$ sudo systemctl stop tomcat
$ sudo systemctl start tomcat
$ sudo systemctl start httpd
タイトルとURLをコピーしました