새소식

운영체제/Linux

[Amazon Linux] Tomcat 및 Java 설치 방법

  • -

Tomcat은 Java로 작성된 JSP를 실행할 수 있는 서버로 OpenJDK 설치가 반드시 필요합니다. 본 포스팅에서는 Amazon Linux에서 Tomcat과 Java를 설치하는 방법을 알아보겠습니다.


설치 환경

설치 환경은 다음과 같습니다.

  • AWS AMI : al2023-ami-2023.0.20230329.0-kernel-6.1-x86_64
  • Tomcat version 9.0.74
  • Java version 11.0.18

 

Java 설치 및 설정

우선 설치 가능한 Java 패키지가 어떤 것들이 있는지 확인해보겠습니다.

$ sudo yum list java*
Last metadata expiration check: 1 day, 7:55:39 ago on Tue Apr 18 23:00:31 2023.
Installed Packages
java-11-amazon-corretto.x86_64                                                            1:11.0.18+10-1.amzn2023                                                                    @amazonlinux
java-11-amazon-corretto-headless.x86_64                                                   1:11.0.18+10-1.amzn2023                                                                    @amazonlinux
javapackages-filesystem.noarch                                                            6.0.0-7.amzn2023.0.5                                                                       @amazonlinux
Available Packages
java-1.8.0-amazon-corretto.x86_64                                                         1:1.8.0_362.b08-1.amzn2023                                                                 amazonlinux
java-1.8.0-amazon-corretto-devel.x86_64                                                   1:1.8.0_362.b08-1.amzn2023                                                                 amazonlinux
java-11-amazon-corretto-devel.x86_64                                                      1:11.0.18+10-1.amzn2023                                                                    amazonlinux
java-11-amazon-corretto-javadoc.x86_64                                                    1:11.0.18+10-1.amzn2023                                                                    amazonlinux
java-11-amazon-corretto-jmods.x86_64                                                      1:11.0.18+10-1.amzn2023                                                                    amazonlinux
java-17-amazon-corretto.x86_64                                                            1:17.0.6+10-1.amzn2023.1                                                                   amazonlinux
java-17-amazon-corretto-devel.x86_64                                                      1:17.0.6+10-1.amzn2023.1                                                                   amazonlinux
java-17-amazon-corretto-headless.x86_64                                                   1:17.0.6+10-1.amzn2023.1                                                                   amazonlinux
java-17-amazon-corretto-javadoc.x86_64                                                    1:17.0.6+10-1.amzn2023.1                                                                   amazonlinux
java-17-amazon-corretto-jmods.x86_64                                                      1:17.0.6+10-1.amzn2023.1                                                                   amazonlinux
java_cup.noarch                                                                           1:0.11b-21.amzn2023.0.3                                                                    amazonlinux
java_cup-javadoc.noarch                                                                   1:0.11b-21.amzn2023.0.3                                                                    amazonlinux
java_cup-manual.noarch                                                                    1:0.11b-21.amzn2023.0.3                                                                    amazonlinux
javacc.noarch                                                                             7.0.4-11.amzn2023.0.1                                                                      amazonlinux
javacc-demo.noarch                                                                        7.0.4-11.amzn2023.0.1                                                                      amazonlinux
javacc-javadoc.noarch                                                                     7.0.4-11.amzn2023.0.1                                                                      amazonlinux
javacc-manual.noarch                                                                      7.0.4-11.amzn2023.0.1                                                                      amazonlinux
javacc-maven-plugin.noarch                                                                2.6-35.amzn2023.0.1                                                                        amazonlinux
javacc-maven-plugin-javadoc.noarch                                                        2.6-35.amzn2023.0.1                                                                        amazonlinux
javapackages-bootstrap.noarch                                                             1.5.0^20220105.git9f283b7-3.amzn2023.0.2                                                   amazonlinux
javapackages-generators.noarch                                                            6.0.0-7.amzn2023.0.5                                                                       amazonlinux
javapackages-local.noarch                                                                 6.0.0-7.amzn2023.0.5                                                                       amazonlinux
javapackages-tools.noarch                                                                 6.0.0-7.amzn2023.0.5                                                                       amazonlinux
javaparser.noarch                                                                         3.22.0-3.amzn2023.0.1                                                                      amazonlinux
javaparser-javadoc.noarch                                                                 3.22.0-3.amzn2023.0.1                                                                      amazonlinux
javassist.noarch                                                                          3.28.0-4.amzn2023.0.1                                                                      amazonlinux
javassist-javadoc.noarch                                                                  3.28.0-4.amzn2023.0.1                                                                      amazonlinux

여기서는 java 11버전을 설치하였습니다. devel이 붙은 패키지는 Java Compiler인 javac입니다.

$ sudo yum update
$ sudo yum install java-11-amazon-corretto.x86_64
$ sudo yum install java-11-amazon-corretto-devel.x86_64

java와 javac가 잘 설치되었는지 버전을 확인해보겠습니다.

$ java -version
openjdk version "11.0.18" 2023-01-17 LTS
OpenJDK Runtime Environment Corretto-11.0.18.10.1 (build 11.0.18+10-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.18.10.1 (build 11.0.18+10-LTS, mixed mode)

$ javac -version
javac 11.0.18

환경 변수를 추가하기 위해 /etc/profile 파일을 열어 다음 세 가지의 환경 변수를 마지막 줄에 추가하겠습니다.

  • JAVA_HOME : java가 실행되는 경로
  • PATH : JAVA_HOME/bin 경로
  • CLASSPATH : 클래스를 찾기 위한 경로
$ which java
/usr/bin/java
$ readlink -f /usr/bin/java
/usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java

$ sudo vim /etc/profile
export JAVA_HOME=/usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
$ source /etc/profile
$ echo $JAVA_HOME
/usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java

여기까지 문제없이 왔다면 Java 설치와 설정이 된 것 입니다.

다음으로 Tomcat 설치 및 설정 부분을 이어나가봅시다.

 

Tomcat 설치 및 설정

보안적인 측면에서 WAS를 기동하는 계정은 root가 아닌 별도의 계정을 사용하는 것이 좋습니다. 따라서 Tomcat을 실행할 그룹과 계정을 생성하겠습니다. tomcat 계정을 생성할 때 해당 계정의 로그인은 필요없기 때문에 nologin 옵션을 주었습니다.

$ sudo groupadd tomcat
$ sudo useradd -s /bin/nologin -g tomcat tomcat
useradd: Warning: missing or non-executable shell '/bin/nologin'

ec2-user 계정에서 tomcat 디렉토리의 내용에 접근하기 위해서는 다음 명령어로 ec2-user 계정을 tomcat 그룹에 추가해주면 되겟습니다.

$ sudo usermod -aG tomcat ec2-user
$ groups
ec2-user adm wheel systemd-journal

 

여기서는 Tomcat 9버전을 설치 할 것입니다. Tomcat 공식 홈페이지에서 설치에 필요한 tar.gz 파이너리 파일 주소를 복사하여 다운로드하고, 압축을 풀어 위에서 생성한 tomcat 계정의 홈디렉토리로 이동시켜주겠습니다.

$ wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.74/bin/apache-tomcat-9.0.74.tar.gz
--2023-04-20 07:10:47--  https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.74/bin/apache-tomcat-9.0.74.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
Connecting to dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11642299 (11M) [application/x-gzip]
Saving to: ‘apache-tomcat-9.0.74.tar.gz’

apache-tomcat-9.0.74.tar.gz                      100%[=======================================================================================================>]  11.10M  --.-KB/s    in 0.06s

2023-04-20 07:10:47 (174 MB/s) - ‘apache-tomcat-9.0.74.tar.gz’ saved [11642299/11642299]

$ tar xvfz apache-tomcat-9.0.74.tar.gz
$ sudo mv apache-tomcat-9.0.74 /home/tomcat

그리고 tomcat 홈디렉토리 하위의 모든 파일들에 대한 소유자와 그룹을 tomcat으로 일괄적으로 변경하겠습니다.

$ sudo chgrp -R tomcat /home/tomcat/
$ sudo chown -R tomcat /home/tomcat/

다음으로 tomcat.service 파일을 새로 생성하여 다음과 같이 내용을 추가합시다.

$ sudo vim /etc/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat 9.0 Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/home/tomcat/apache-tomcat-9.0.74/temp/tomcat.pid
Environment=CATALINA_HOME=/home/tomcat/apache-tomcat-9.0.74
Environment=CATALINA_BASE=/home/tomcat/apache-tomcat-9.0.74
Environment='CATALINA_OPTS=-Xms256M -Xmx256M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/home/tomcat/apache-tomcat-9.0.74/bin/startup.sh
ExecStop=/home/tomcat/apache-tomcat-9.0.74/bin/shutdown.sh
#ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

위의 내용으로 서비스 파일을 생성하였으면 데몬 리로드를 하여 서비스를 등록하겠습니다. 그리고 부팅될 때 Tomcat 서비스가 자동으로 Active될 수 있도록 enable하고, Tomcat 서비스를 시작합니다.

$ sudo systemctl daemon-reload
$ sudo systemctl enable tomcat.service
$ sudo systemctl start tomcat.service

Tomcat 서비스가 정상적으로 시작되었다면 기본적으로 8080포트를 사용하기 때문에 localhost의 해당 포트로 curl 명령을 수행해봅시다.

$ curl localhost:8080
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Apache Tomcat/9.0.74</title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
        <div id="wrapper">
            <div id="navigation" class="curved container">
                <span id="nav-home"><a href="https://tomcat.apache.org/">Home</a></span>
                <span id="nav-hosts"><a href="/docs/">Documentation</a></span>
                <span id="nav-config"><a href="/docs/config/">Configuration</a></span>
                <span id="nav-examples"><a href="/examples/">Examples</a></span>
                <span id="nav-wiki"><a href="https://cwiki.apache.org/confluence/display/TOMCAT/">Wiki</a></span>
                <span id="nav-lists"><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></span>
                <span id="nav-help"><a href="https://tomcat.apache.org/findhelp.html">Find Help</a></span>
                <br class="separator" />
            </div>
            <div id="asf-box">
                <h1>Apache Tomcat/9.0.74</h1>
            </div>
            <div id="upper" class="curved container">
                <div id="congrats" class="curved container">
                    <h2>If you're seeing this, you've successfully installed Tomcat. Congratulations!</h2>
                </div>
                <div id="notice">
                    <img id="tomcat-logo" src="tomcat.svg" alt="[tomcat logo]" />
                    <div id="tasks">
                        <h3>Recommended Reading:</h3>
                        <h4><a href="/docs/security-howto.html">Security Considerations How-To</a></h4>
                        <h4><a href="/docs/manager-howto.html">Manager Application How-To</a></h4>
                        <h4><a href="/docs/cluster-howto.html">Clustering/Session Replication How-To</a></h4>
                    </div>
                </div>
                <div id="actions">
                    <div class="button">
                        <a class="container shadow" href="/manager/status"><span>Server Status</span></a>
                    </div>
                    <div class="button">
                        <a class="container shadow" href="/manager/html"><span>Manager App</span></a>
                    </div>
                    <div class="button">
                        <a class="container shadow" href="/host-manager/html"><span>Host Manager</span></a>
                    </div>
                </div>
                <br class="separator" />
            </div>
            <div id="middle" class="curved container">
                <h3>Developer Quick Start</h3>
                <div class="col25">
                    <div class="container">
                        <p><a href="/docs/setup.html">Tomcat Setup</a></p>
                        <p><a href="/docs/appdev/">First Web Application</a></p>
                    </div>
                </div>
                <div class="col25">
                    <div class="container">
                        <p><a href="/docs/realm-howto.html">Realms &amp; AAA</a></p>
                        <p><a href="/docs/jndi-datasource-examples-howto.html">JDBC DataSources</a></p>
                    </div>
                </div>
                <div class="col25">
                    <div class="container">
                        <p><a href="/examples/">Examples</a></p>
                    </div>
                </div>
                <div class="col25">
                    <div class="container">
                        <p><a href="https://cwiki.apache.org/confluence/display/TOMCAT/Specifications">Servlet Specifications</a></p>
                        <p><a href="https://cwiki.apache.org/confluence/display/TOMCAT/Tomcat+Versions">Tomcat Versions</a></p>
                    </div>
                </div>
                <br class="separator" />
            </div>
            <div id="lower">
                <div id="low-manage" class="">
                    <div class="curved container">
                        <h3>Managing Tomcat</h3>
                        <p>For security, access to the <a href="/manager/html">manager webapp</a> is restricted.
                        Users are defined in:</p>
                        <pre>$CATALINA_HOME/conf/tomcat-users.xml</pre>
                        <p>In Tomcat 9.0 access to the manager application is split between
                           different users. &nbsp; <a href="/docs/manager-howto.html">Read more...</a></p>
                        <br />
                        <h4><a href="/docs/RELEASE-NOTES.txt">Release Notes</a></h4>
                        <h4><a href="/docs/changelog.html">Changelog</a></h4>
                        <h4><a href="https://tomcat.apache.org/migration.html">Migration Guide</a></h4>
                        <h4><a href="https://tomcat.apache.org/security.html">Security Notices</a></h4>
                    </div>
                </div>
                <div id="low-docs" class="">
                    <div class="curved container">
                        <h3>Documentation</h3>
                        <h4><a href="/docs/">Tomcat 9.0 Documentation</a></h4>
                        <h4><a href="/docs/config/">Tomcat 9.0 Configuration</a></h4>
                        <h4><a href="https://cwiki.apache.org/confluence/display/TOMCAT/">Tomcat Wiki</a></h4>
                        <p>Find additional important configuration information in:</p>
                        <pre>$CATALINA_HOME/RUNNING.txt</pre>
                        <p>Developers may be interested in:</p>
                        <ul>
                            <li><a href="https://tomcat.apache.org/bugreport.html">Tomcat 9.0 Bug Database</a></li>
                            <li><a href="/docs/api/index.html">Tomcat 9.0 JavaDocs</a></li>
                            <li><a href="https://github.com/apache/tomcat/tree/9.0.x">Tomcat 9.0 Git Repository at GitHub</a></li>
                        </ul>
                    </div>
                </div>
                <div id="low-help" class="">
                    <div class="curved container">
                        <h3>Getting Help</h3>
                        <h4><a href="https://tomcat.apache.org/faq/">FAQ</a> and <a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></h4>
                        <p>The following mailing lists are available:</p>
                        <ul>
                            <li id="list-announce"><strong><a href="https://tomcat.apache.org/lists.html#tomcat-announce">tomcat-announce</a><br />
                                Important announcements, releases, security vulnerability notifications. (Low volume).</strong>
                            </li>
                            <li><a href="https://tomcat.apache.org/lists.html#tomcat-users">tomcat-users</a><br />
                                User support and discussion
                            </li>
                            <li><a href="https://tomcat.apache.org/lists.html#taglibs-user">taglibs-user</a><br />
                                User support and discussion for <a href="https://tomcat.apache.org/taglibs/">Apache Taglibs</a>
                            </li>
                            <li><a href="https://tomcat.apache.org/lists.html#tomcat-dev">tomcat-dev</a><br />
                                Development mailing list, including commit messages
                            </li>
                        </ul>
                    </div>
                </div>
                <br class="separator" />
            </div>
            <div id="footer" class="curved container">
                <div class="col20">
                    <div class="container">
                        <h4>Other Downloads</h4>
                        <ul>
                            <li><a href="https://tomcat.apache.org/download-connectors.cgi">Tomcat Connectors</a></li>
                            <li><a href="https://tomcat.apache.org/download-native.cgi">Tomcat Native</a></li>
                            <li><a href="https://tomcat.apache.org/taglibs/">Taglibs</a></li>
                            <li><a href="/docs/deployer-howto.html">Deployer</a></li>
                        </ul>
                    </div>
                </div>
                <div class="col20">
                    <div class="container">
                        <h4>Other Documentation</h4>
                        <ul>
                            <li><a href="https://tomcat.apache.org/connectors-doc/">Tomcat Connectors</a></li>
                            <li><a href="https://tomcat.apache.org/connectors-doc/">mod_jk Documentation</a></li>
                            <li><a href="https://tomcat.apache.org/native-doc/">Tomcat Native</a></li>
                            <li><a href="/docs/deployer-howto.html">Deployer</a></li>
                        </ul>
                    </div>
                </div>
                <div class="col20">
                    <div class="container">
                        <h4>Get Involved</h4>
                        <ul>
                            <li><a href="https://tomcat.apache.org/getinvolved.html">Overview</a></li>
                            <li><a href="https://tomcat.apache.org/source.html">Source Repositories</a></li>
                            <li><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></li>
                            <li><a href="https://cwiki.apache.org/confluence/display/TOMCAT/">Wiki</a></li>
                        </ul>
                    </div>
                </div>
                <div class="col20">
                    <div class="container">
                        <h4>Miscellaneous</h4>
                        <ul>
                            <li><a href="https://tomcat.apache.org/contact.html">Contact</a></li>
                            <li><a href="https://tomcat.apache.org/legal.html">Legal</a></li>
                            <li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
                            <li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li>
                        </ul>
                    </div>
                </div>
                <div class="col20">
                    <div class="container">
                        <h4>Apache Software Foundation</h4>
                        <ul>
                            <li><a href="https://tomcat.apache.org/whoweare.html">Who We Are</a></li>
                            <li><a href="https://tomcat.apache.org/heritage.html">Heritage</a></li>
                            <li><a href="https://www.apache.org">Apache Home</a></li>
                            <li><a href="https://tomcat.apache.org/resources.html">Resources</a></li>
                        </ul>
                    </div>
                </div>
                <br class="separator" />
            </div>
            <p class="copyright">Copyright &copy;1999-2023 Apache Software Foundation.  All Rights Reserved</p>
        </div>
    </body>

</html>

위와 같은 출력이 보여지면 Tomcat 페이지가 정상적으로 동작하고 있는 것 입니다!😎

 

접속이 안될 경우

기본적으로 Public IP를 부여하거나 VPN 연결이 된 상황이 아니라면 외부에서 http://INSTANCE_IP:8080/ 으로 접속이 되지는 않을 것입니다. 접속이 되어야 하는 상황임에도 페이지가 나오지 않는다면 Security Group에서 8080 포트를 열어주었는지 확인해봅시다. 

[그림 1] Security Group 8080 포트 Any로 Open

 

혹은 netstat 명령어를 통해 8080 포트가 열려있는 상태인지 확인해봅시다.

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.