DEV Community

Ashish Gajjar for AWS Community Builders

Posted on • Edited on

Step by Step guide to install Apache Tomcat tar ball on Amazon Linux 2

Prerequisite :
1.0 Creating an EC2 Instance
2.0 Connecting to EC2 Instance
3.0 Install Java
4.0 Download and Install Tomcat
5.0 Add startup Script for Tomcat Application
6.0 Troubleshot option

1.0 Creating an EC2 Instance

First, let's login into our AWS account. Once login, we will land on the Management Console page, we can see all the AWS services.
Login to AWS Management Console –
https://console.aws.amazon.com/console/home
Image description
Once we click on EC2 you will be redirected to the EC2 Dashboard. It shows the information related to all the EC2 resources for a specific Region in our account.
There is a button Launch Instance to create a new instance in the selected region on the dashboard's bottom section.
Image description

How to launch an EC2 Instance

Let’s give our web server a name. I’ll go with “webserver” to keep it short and sweet. You can name it whatever you want.

Image description

After clicking the Launch button, we need to select the Amazon Machine Image (AMI) it includes the operating system and applications required to launch an instance. Here we will select the Amazon Linux 2023 AMI as shown in the below snapshot.

Image description

The next step is to choose the type of instance we need. AWS provides many types of instances based on different use cases with various CPU combinations, memory, storage, and networking capacity. We will here select the t2.micro instance, which is a free tier eligible instance.

Image description

Moving on, we’ll create a new key pair so that we can SSH into the instance.

Image description
Your key pair name can be whatever you want to name it. Key pair type is RSA, and Private Key File Format is .PEM. Once you click “Create key pair”, the pem file should automatically download to your computer. Be sure to select the Key Pair from the drop down box.

Image description

Image description

SSH Access
This type of access is required to connect to the EC2 instance and run commands / install software's, etc
Port = 22, 8080
Allow from anywhere

Image description

There was no need to make changes to configure storage or advanced details so let's move along.

Image description

Now it’s time to officially launch the instance. Proceed by clicking the orange button to the right of the screen that says “Launch Instance”.

Image description
You should see a green message that says “Successfully initiated launch of instance”.

Image description
Image description

2.0 Connecting to EC2 Instance

In order to SSH into our instance we must connect to it. On the left side of the screen, click instances. From there, select the instance we just launched followed by “Connect” in the top right of the screen.

ssh -i "sunday.pem" ec2-user@ec2-54-242-255-156.compute-1.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

3.0 Install Java

- yum install java -y
- java -version
Enter fullscreen mode Exit fullscreen mode

4.0 Download and Install Tomcat

Step 1: Download file

wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.91/bin/apache-tomcat-8.5.91.tar.gz
Enter fullscreen mode Exit fullscreen mode

Step 2: Extract file

- tar -xvf apache-tomcat-8.5.91.tar.gz
Enter fullscreen mode Exit fullscreen mode

Step3: Move a file

- mv apache-tomcat-8.5.91 /usr/share/tomcat8  
Enter fullscreen mode Exit fullscreen mode

Step 4: link a file

# Enter a below command
- cd /usr/share/tomcat8
# conf file
- mv conf /etc/tomcat8
- ln -s /etc/tomcat8 conf
# Lib file 
- mv lib /usr/share/java/tomcat8
- ln -s /usr/share/java/tomcat8 lib
# temp file
- mkdir /var/cache/tomcat8 
- mv temp /var/cache/tomcat8/temp
- ln -s /var/cache/tomcat8/temp temp
# Webapp file 
- mkdir /var/lib/tomcat8
- mv webapps /var/lib/tomcat8/
- ln -s /var/lib/tomcat8/webapps webapps
# Work file
- mv work /var/cache/tomcat8/work 
- ln -s /var/cache/tomcat8/work work
#logs :
- mv logs /var/log/tomcat8
- ln -s /var/log/tomcat8 logs
Enter fullscreen mode Exit fullscreen mode

****check tomcat directory
Image description
Step 4: Check tomcat8 service
Image description

5.0 Add startup Script for Tomcat Application

Service is not available then we will Create a tomcat8 service manually

[root@ashish tomcat8]#  vim /usr/lib/systemd/system/tomcat8.service
# create new
[Unit]
Description=Apache Tomcat 8
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/share/tomcat8/bin/startup.sh
ExecStop=/usr/share/tomcat8/bin/shutdown.sh
RemainAfterExit=yes
User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

check tomcat8 service and start a tomcat8 service

Image description

Oh! No Still tomcat8 is not running. we will troubleshoot the issue

Manual configure all files and directories permission.

# first create a tomcat user
useradd -r -m -U -d /usr/share/tomcat8 -s /bin/false tomcat
#Setup a permission users and group on /var/log/tomcat8/
chown tomcat:tomcat /var/log/tomcat8/
chmod g+s /var/log/tomcat8/
chmod g+w /usr/share/tomcat8/
chown root:tomcat /usr/share/tomcat8/
chmod -R 755 /usr/share/tomcat8/webapps/ROOT/www
chmod a+rx /etc/tomcat8
chgrp tomcat /etc/tomcat8
chown tomcat:tomcat /etc/tomcat8/*
chown root:tomcat /usr/share/tomcat8/bin/*
chmod a+rx /usr/share/tomcat8/bin/
chmod a+r /usr/share/java/tomcat8/*
chown tomcat:tomcat /var/cache/tomcat8/work
chown tomcat:tomcat /var/lib/tomcat8/webapps/*
chown tomcat:tomcat /var/cache/tomcat8/temp
systemctl start tomcat8.service
Enter fullscreen mode Exit fullscreen mode


`
Check tomcat8 status
systemctl status tomcat8.service
Image description
Enjoy!!!!

6.0 Troubleshot option

  1. Getting below error

Image description

[root@ip-172-31-26-137 bin]# systemctl status -l tomcat8.service

Top comments (0)