[Java] Install Java, Apache, Tomcat 9 on EC2 (Amazon Linux 2)

3 minute read

Overview

Make sure you have installed Java, Apache (WEB server), and Tomcat (servlet container) on AmazonLinux2.

  • Java program running on the Web server is a servlet
  • The software that runs the servlet is the servlet container, this time it is Tomcat
  • WEB server software that responds to requests is Apache
  • Tomcat works to work without Apache. It also functions as simple WEB server software.

https://wa3.i-3-i.info/word12843.html

Install Java

Install java-1.8.0-openjdk-devel. For the difference between the presence and absence of devel, refer to here. For the execution environment only, java-1.8.0-openjdk is fine, but those with -devel can use development commands such as javac. Basically-install devel.

install


$yum install -y java-1.8.0-openjdk-devel

version check


$ java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

Apache installation

I referred to here.

version check


$ httpd -v
Server version: Apache/2.4.43 ()
Server built: May 8 2020 17:02:41

Settings to start Apache when EC2 starts


$ systemctl enable httpd.service
$ systemctl status httpd.service
● httpd.service-The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-09-02 14:17:37 UTC; 16h ago
     Docs: man:httpd.service(8)
~ Omitted ~

If there is a character of enabled, the automatic start setting is completed.

When you access the EC2 endpoint as shown below, the Apache test page is displayed. http://ec2****.ap-northeast-1.compute.amazonaws.com

Tomcat installation

When I installed Apache, I also referred to the linked links that helped me, but it didn’t work. It worked based on here. I wonder why. .. .. Note that the 8080 port is allowed in the inbound rule of the EC2 security group. Required to access the Tomcat test page. Security Group: Server.png

The download source URL is the link address of tar.gz of official site.

Download

tomcat

 to the ho directory
$ cd ~
$ wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.37/bin/apache-tomcat-9.0.37.tar.gz

decompress


$ tar -xvzf apache-tomcat-9.0.37.tar.gz

The unzipped directory is moved to /opt/tomcat-9 according to the reference site & rename.


$ sudo mv ~/apache-tomcat-9.0.37 /opt/tomcat-9

Create a tomcat group and user and set it to the user group in the tomcat-9 directory. The -s/bin/false option seems to prevent the tomcat user from logging in as a normal user.

User and group settings


$sudo groupadd tomcat
$sudo useradd -r tomcat -s /bin/false -g tomcat tomcat
$sudo chgrp -R tomcat /opt/tomcat-9
$sudo chown -R tomcat /opt/tomcat-9

Create a configuration file (unit file) for starting the service of

tomcat


$sudo vim /etc/systemd/system/tomcat.service

Paste the following. When comparing the reference site and my environment, the JAVA_HOME path was slightly different. CATALINA_HOME and BASE look like the directory where Tomcat is installed. It seems that there are various meanings in the unit file when you see ↓, but I will study the details in detail. https://tex2e.github.io/blog/linux/create-my-systemd-service

Unit file


[Unit]
Description=Apache Tomcat
After=network.target
[Service]
Type=forking
Environment=CATALINA_PID=/opt/tomcat-9/temp/tomcat9.pid
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
Environment=CATALINA_HOME=/opt/tomcat-9
Environment=CATALINA_BASE=/opt/tomcat-9
Environment="CATALINA_OPTS=-Xms512m -Xmx1G"
Environment="JAVA_OPTS=-Dfile.encoding=UTF-8 -Djava.awt.headless=true"
ExecStart=/opt/tomcat-9/bin/startup.sh
ExecStop=/opt/tomcat-9/bin/shutdown.sh
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target

Start automatically


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

Status check


$ systemctl status tomcat.service
● tomcat.service-Apache Tomcat
   Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-09-02 14:17:37 UTC; 18h ago
~ Omitted ~

If you add :8080 to the EC2 endpoint, the Tomcat page will be displayed. http://ec2****.ap-northeast-1.compute.amazonaws.com:8080

The service can be started and stopped manually.

$sudo service tomcat start
$sudo service tomcat stop

Comparing the above steps with Sites that took care of installing Apache, there is a subtle difference in the contents of the unit file and the procedure for creating symbolic links. It was different, but it worked. The procedure to create a symbolic link can handle cases such as changing the version of tomcat.

reference

Thank you very much. https://qiita.com/hiren/items/2a4f1b55c99ebfb3fd08#apache https://medium.com/@ean.vafaei/how-to-install-tomcat-on-aws-ec2-instance-c10738a653f2