Step-by-Step Guide to Install Java 17, Maven, and Apache Tomcat on AWS Ubuntu Server

Step-by-Step Guide to Install Java 17, Maven, and Apache Tomcat on AWS Ubuntu Server

Deploying a Maven Project on Apache Tomcat Server with Practical Insights

Introduction

In this guide, I will walk you through the installation and configuration of Java 17, Maven, and Apache Tomcat Server on an AWS Ubuntu instance. Additionally, I will demonstrate how to deploy a simple Maven project to Tomcat and access it via the server's public IP. This practical tutorial will cover:

  • Prerequisites

  • Step-by-step installation

  • Configuring and starting Tomcat

  • Deploying a sample Maven project

  • Accessing the deployed project


Prerequisites

Before starting, ensure you have the following:

  1. A Linux-based OS (Ubuntu) with sudo privileges.

  2. Basic knowledge of Linux command-line operations.

  3. AWS account with an EC2 instance ready (public IP for access).

For this guide, I used an AWS Ubuntu machine with the following public IP: 3.6.160.124


AWS Instance Creation

Before proceeding to installation steps, you must create an AWS Ubuntu instance. Below are the steps to create the instance:

  1. Log in to AWS Management Console.

  2. Navigate to EC2 > Launch Instance.

  3. Choose an Ubuntu 20.04 LTS AMI.

  4. Select an instance type (e.g., t2.micro for free tier).

  5. Configure networking and security group:

    • Allow SSH (port 22) and HTTP (port 8080 for Tomcat).
  6. Launch the instance and note down the public IP.

Screenshot Reference:

  • AWS Instance Creation

Once the instance is ready, you can SSH into the machine to proceed with the following steps.


Step 1: Set Up Java 17 and Maven

Install Java 17

First, update your system and install OpenJDK 17:

sudo apt update
sudo apt install openjdk-17-jdk -y

Verify the Java installation:

java -version

Install Maven

Maven is required to build and package our project. Install it using:

sudo apt install maven -y

Verify Maven installation:

mvn -version

Step 2: Download and Configure Apache Tomcat

2.1 Download and Extract Tomcat

Switch to the root user and navigate to the /opt directory:

sudo su
cd /opt

Download Apache Tomcat 9.0.65 tarball:

sudo wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz

Extract the tarball:

sudo tar -xvf apache-tomcat-9.0.65.tar.gz

2.2 Configure Tomcat Users

Navigate to the Tomcat configuration directory:

cd /opt/apache-tomcat-9.0.65/conf

Edit the tomcat-users.xml file:

sudo vi tomcat-users.xml

Add the following line before the closing </tomcat-users> tag to set up an admin user:

<user username="admin" password="admin1234" roles="admin-gui,manager-gui,manager-script"/>

Save the file and exit.


2.3 Create Start and Stop Scripts

To simplify starting and stopping the Tomcat server, create symbolic links:

sudo ln -s /opt/apache-tomcat-9.0.65/bin/startup.sh /usr/bin/startTomcat
sudo ln -s /opt/apache-tomcat-9.0.65/bin/shutdown.sh /usr/bin/stopTomcat

You can now use startTomcat and stopTomcat commands from any directory.


2.4 Modify Access Restrictions

Tomcat restricts access to the Manager and Host Manager web apps by default. To allow external access:

For Manager App

Edit the context.xml file:

sudo vi /opt/apache-tomcat-9.0.65/webapps/manager/META-INF/context.xml

Comment out the RemoteAddrValve configuration:

<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.�d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->

For Host Manager App

Repeat the same steps for the Host Manager app:

sudo vi /opt/apache-tomcat-9.0.65/webapps/host-manager/META-INF/context.xml

Comment out the RemoteAddrValve block.


2.5 Start and Stop Tomcat

Start the Tomcat server:

sudo startTomcat

Stop the Tomcat server:

sudo stopTomcat

Access the Tomcat UI using the public IP and port 8080:

http://3.6.160.124:8080

Step 3: Deploy a Maven Project to Tomcat

3.1 Clone the Sample Project

Navigate to your home directory and clone a sample Maven project:

cd ~
git clone https://github.com/kri-sh27/maven-tomact-sample.git

Change into the project directory:

cd maven-tomact-sample

3.2 Build the Project

Use Maven to package the project:

mvn package

This will generate a hello-world.war file inside the target folder.


3.3 Deploy the WAR File to Tomcat

Copy the generated .war file to the Tomcat webapps directory:

sudo cp target/hello-world.war /opt/apache-tomcat-9.0.65/webapps/

Restart the Tomcat server:

sudo stopTomcat
sudo startTomcat

3.4 Access the Deployed Application

Once the WAR file is deployed, access the application using the following URL:

http://3.6.160.124:8080/hello-world

You should now see the application UI displayed.


Screenshots of the Process

Here are some captured screenshots during the process:

  1. AWS Instance Creation:

    Give name to Instance and select AMI As ubuntu.

    Then select AMI ubuntu Server 24.04 (free tier eligible)

    and instance type is t2.micro

    Now create Key pair

    We need port 22 and 8080 open so add Security Group and allow this port in inbound rule.

    Click on Launch

    Here instance is up and Running

  2. Tomcat Server UI:

    This the UI of tomcat server.

  3. Deployed Application:

    This is the sample application UI.


Outcomes

  • Successfully installed and configured Java 17, Maven, and Apache Tomcat Server.

  • Created start and stop scripts for easier server management.

  • Deployed a Maven project on Tomcat and accessed it via a public IP.

  • Learned about modifying Tomcat access restrictions to enable remote management.


Conclusion

By following this step-by-step guide, you can set up Java, Maven, and Apache Tomcat on any Ubuntu-based server, deploy your web applications, and make them accessible externally. Whether you are working on AWS or a local environment, these steps provide a comprehensive understanding of the process.


Feel free to share your thoughts or ask questions in the comments below! If you enjoyed this tutorial, follow me for more DevOps and software deployment guides.