Step by Step guide to install Apache Tomcat tar ball on Amazon Linux 2
Published: 2023-12-13 06:38:43

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k5dlhcxnho60ay4u331n.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rrpojhc1ac0ntsakq02u.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3pqssnocie69m0heuxf4.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqu16jad01r6ju0hwb4l.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u85m5sxg3wd77ee734x.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zt08i4bpnyfz272abapw.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ul8zpj5s0ulfa2kf0ung.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0zlw0oi9zemuh7jrlw7.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93hiwnh2tmy9jcm63igx.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihx4bslt0fvn5mhqwu0q.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8w8i14iqvl9rlv5yqzz.png)
You should see a green message that says “Successfully initiated launch of instance”.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1cekqqgdebijnhs2pqw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwrv3ojwmpte32l3o7fh.png)

## 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
```
## 3.0 Install Java
```
- yum install java -y
- java -version
```
**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
```
Step 2: Extract file
```
- tar -xvf apache-tomcat-8.5.91.tar.gz
```
Step3:   Move a file 
```
- mv apache-tomcat-8.5.91 /usr/share/tomcat8  
```
## 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
```
****check tomcat directory 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4592gf2kimfzpnowgyi.png)
Step 4: Check tomcat8 service
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9a107a5tlsyyexvxt4yc.png)

## 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
```
check tomcat8 service and start a tomcat8 service

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8hmtcvjycqqua114cvf.png)

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 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
````
**Check tomcat8 status**
systemctl status tomcat8.service
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tf5bmo5qkps9sz1icpu.png)
Enjoy!!!!

## 6.0 Troubleshot option

1. Getting below error

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q52yu5ck8awn9fpyf7u9.png)
```
[root@ip-172-31-26-137 bin]# systemctl status -l  tomcat8.service
```