In this tutorial, we will learn how to create an EC2 server on the AWS Cloud and setup Java and Tomcat on that server.

setting up apache tomcat using ubuntu 16 0 4 mobisoft infotech

Please note that in this tutorial, we are going to do some basic server setup (which is already been explained over last published tutorial, please read How To Launch An AWS EC2 Server And Set Up Ubuntu 16.04 On It ).

Firstly, we would be creating a server by logging into an EC2 account.

We need to go to the EC2 dashboard to launch instance. Now, we are going to do step by step of basic server setup:

EC2 dashboard to launch instance mobisoft infotech

Step 1: Select Ubuntu 16.04 (64 bit) on your EC2 dashboard;

select ubuntu 64 bit mobisoft infotech

Step 2: Use t2.small as Java generally requires more RAM;

select an instance type mobisoft infotech

Step 3: Enable Protection against accidental termination, other than that, keep other settings as it is;

configure instance details mobisoft infotech

Step 4: For storage, use 25 GB, General purpose SSD;

add-storage-mobisoft-infotech

Step 5: Tag Instance: Enter value as “Javasetup”

Step 6: Configure Security Group, In this, you can either use/select the existing security group or can create a new security group. We will create a new one and will give it a name and a description:

    • Security Group Name: JavaSetup

 

    • Description: Security Group created for Java setup

 

Type: SSH
Protocol: TCP
Port range: 22 (for now)
Source: Anywhere

configure security group mobisoft infotech

Step 7: Review Instance Launch and if it looks fine then proceed.

review instance launch mobisoft infotech

Now, select ‘Launch’. When you do that, a dialog box would pop up giving you an option of selecting an existing key pair or creating a new key pair.

Here, we are going to use an existing key pair. It’s OK, if you might want to create a new set of key pair.

select a key pair mobisoft infotech

For this instance, we require Elastic IP address. So, while it launches I will provision for it.

launch instance mobisoft infotech

Check if your instance is working. Now, you need to associate the Elastic IP with our Java setup server.

allocate address mobisoft infotech

associate the address mobisoft infotech

Go to >>

Instances>Elastic IP Address

Now, review if Elastic IP Address is present.

elastic ip address check mobisoft infotech

Now, I used to do the SSH login to the server.

Post that, we will run through the basic server setup. (You can refer to the previous tutorial on basic server setup)

We’ll have to wait for a couple of minutes then it will restart. When the system is back on, proceed through the further process of setting up of the JDKs and the Tomcat servers:

From now on, our preferred JDK would be Open JDK because most of the beanstalk installation as well as Heroku prefers Open JDK. Therefore, we need to add an apt-get source from where JDK is installed.

To add apt-get source, please run this command:

$ sudo -E add-apt-repository ppa:openjdk-r/ppa

When the source is added, update by running this command:

$ sudo apt-get update

We’ll learn, now, how to install multiple versions of JDKs and how to switch between them. The first one is Open JDK 7, for its setup run this command:

$ sudo apt-get install openjdk-7-jdk

Now, how to check if the above setup is done or not?

Run this command on the directory:

>>

$java -version

When you run this command, you’ll get this as a result:

#-> java version "1.7.0_95"
#-> OpenJDK Runtime Environment (IcedTea 2.6.4) (7u95-2.6.4-3)
#-> OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode)

Similarly, now we want to check JavaC version. For that, we need to run this command:

$javac -version

javac version mobisoft infotech

It’s again 1.7.0_95

Following the same process, we would now install Open JDK 8 and run this command:

$sudo apt-get install openjdk-8-jdk

After this, we will check the Java version:

$java -version

When you run this command, you’ll get this as a result:

# openjdk version "1.8.0_91"
# OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~16.04.1-b14)
# OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

Now, we will check Javac version and run this command:

$javac -version

It’s also 1.8.0_91

# javac 1.8.0_91

Now, let’s try to switch back to JDK-7 for that command is:

$update-java-alternatives –list

This shows which JDKs are installed and what’s the value of JAVA_HOME for each of those.

We would now want to switch to this:

java-1.7.0-openjdk-amd64

So, we will do that by running this command:

$sudo update-java-alternatives --set java-1.7.0-openjdk-amd64

no alternatives for mozilla plugins mobisoft infotech

Unfortunately, it fails because there’s some problem with Mozilla-javaplugin.

there is some problem with mozilla plugins mobisoft infotech

To fix this issue, we need to install that very plugin and for that we would require a ppa-source. You can add it by running this command:

$sudo add-apt-repository ppa:maarten-fonville/ppa

Now, we need to update the apt-get sources again!

For that, we want to run this command:

$sudo apt-get update

Now, these are the commands for:

# For JDK 7 plugin

sudo apt-get install icedtea-web

# ForJDK 8 plugin

sudo apt-get install icedtea-8-plugin

After this, you can check Java version and Javac version. Here, are the required commands and the expected results:

$ java -version
#-> java version "1.7.0_95"
#-> OpenJDK Runtime Environment (IcedTea 2.6.4) (7u95-2.6.4-3)
#-> OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode)
$ javac -version

#-> javac 1.7.0_95

Sometimes you need to run “sudo update-alternatives –config javac” to change javac. But right now, it is not needed.

Next, we would now add Oracle’s JDKs. For that, you want to add one ppa, i.e:

$ sudo add-apt-repository ppa:webupd8team/java

Now, let’s update the apt-get sources:

$ sudo apt-get update

Next, we would be installing Oracle Java6, Java7 and Java8 one by one:

Here are the following commands you need to run:

$ sudo apt-get install oracle-java6-installer
$ sudo apt-get install oracle-java7-installer
$ sudo apt-get install oracle-java8-installer

Please note that you would not typically install all of these on the server. You would typically install Open JDK 8. In this tutorial, you’re taught to do so because if you require any specific JDK for your project you would be able to do it.

Now, we will check the update Java alternatives by running this command:

$ update-java-alternatives --list

You can see that all the JDK versions are now available to use on this system.

all java versions are now available mobisoft infotech

#-> java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
#-> java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
#-> java-6-oracle 1082 /usr/lib/jvm/java-6-oracle
#-> java-7-oracle 1083 /usr/lib/jvm/java-7-oracle
#-> java-8-oracle 1084 /usr/lib/jvm/java-8-oracle

We would like to use Open JDK 8, so to set that up we need to run this command:

$ sudo update-java-alternatives --set java-1.8.0-openjdk-amd64

Now, let’s check the java and javac versions. To see, if it is correct we need to run the following commands:

$java -version

Expected result would be:

#-> openjdk version "1.8.0_91"
#-> OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~16.04.1-b14)
#-> OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)
$javac -version

Expected result would be:

#-> javac 1.8.0_101

Both java and javac versions are correct!

We are ready to install Tomcat. I have used this tutorial to install Tomcat

## https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-ubuntu-16-04

I have made some minor changes, for this tutorial, to suit our requirements. Let’s create this directory in our home folder (~ denotes home folder), run the following commands:

$ mkdir ~/software
$ cd ~
$ pwd

Home folder would be /home/ubuntu for this user

Let’s now download the Tomcat using the curl command

$ curl -O http://mirror.fibergrid.in/apache/tomcat/tomcat-8/v8.5.6/bin/apache-tomcat-8.5.6.tar.gz

Please note that you should get the latest download link from this (https://tomcat.apache.org/download-80.cgi) page.

Once it is downloaded, let’s go back to the home directory again and create the opt directory by running the commands one by one:

$ mkdir opt
$ cd opt

And then to extract the Tomcat setup run this command:

$ tar xvzf ~/software/apache-tomcat-8.5.6.tar.gz -C .

After this, we will create a symlink so that we can refer to apache-tomcat-8.5.6 with just Tomcat:

$ ln -s ~/opt/apache-tomcat-8.5.6/ tomcat
$ ls -la

symlink to refer to tomcat mobisoft infotech

Post this, we are going to create the startup script for Tomcat. With Ubuntu 16.04, the way to start script has changed. The preferred way to do it now is via systemd service. Before this, we used to create our script in /etc/init.d

This option is still available but as said before, the preferred way to do it is with systemd. Therefore, we would be using the same way of creating a startup script. For this, we basically need to create this file.

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

For this tutorial, I have already created this file. Note that, I have setup the following:

[Unit] Description=Apache Tomcat Web Application Container
After=network.target

[Service] Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
Environment=CATALINA_PID=/home/ubuntu/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/home/ubuntu/opt/tomcat
Environment=CATALINA_BASE=/home/ubuntu/opt/tomcat
Environment=’CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC’
Environment=’JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom’

ExecStart=/home/ubuntu/opt/tomcat/bin/startup.sh
ExecStop=/home/ubuntu/opt/tomcat/bin/shutdown.sh

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

[Install] WantedBy=multi-user.target

You might need to make some changes on this, on the basis of your project’s requirements.

Now, let’s reload the systemctl daemon by running this command:

$ sudo systemctl daemon-reload

Here, we can start Tomcat:

$ sudo systemctl start tomcat

It is up and running. To check, run either

$ ps aux | grep tomcat

Or via

$ sudo systemctl status tomcat

check if tomcat has started mobisoft infotech

So, now Tomcat has started!

You may now want to check if Tomcat is running and if it is actually accessible but as you know, Tomcat runs on port 8080. So, you would require to open port 8080 in your security group.

Go to >> Security Group > Select the Security Group that you have created for the server > Edit inbound rules > Add a new TCP rule > Enter Port Range as 8080

edit inbound rules mobisoft infotech

Allow it to be connected from ‘anywhere’ from the internet.

So, now if we go to our Elastic IP Address

Open port 8080 in EC2 security group and check it at: http://52.54.92.131:8080/

We should see the “Tomcat page”.

tomcat page mobisoft infotech

And it is there!

manager app mobisoft infotech

The “Manager App” should not be present here and should be stopped immediately for the reason that hackers looks for 8080 port and they try to hack this manager app.

Hence, we need to delete it. If it is setup, we will tell you how to delete it and restart Tomcat.

Stop the Tomcat.

tomcat has stopped mobisoft infotech

Run these commands to stop Tomcat:

$ sudo systemctl stop tomcat
$ cd ~/opt/tomcat
$ cd webapps/
$ rm -rf manager/
$ rm -rf docs/
$ rm -rf examples/
$ rm -rf host-manager/
$ ls

Now, go back and refresh to check if Tomcat has stopped.

tomcat has stopped mobisoft infotech

Once it is stopped, let’s start Tomcat. We need to run this command:

$ sudo systemctl start tomcat

To check the status:

$sudo systemctl status tomcat

its running manager app removed mobisoft infotech

manager app has stopped error 404 mobisoft infotech

Tomcat is running and Manager app has stopped, you can see it when you go to the browser and check!

This is critically important to remove ‘Manager App’ so that hackers cannot get into our Tomcat instances. Anyway, we manage everything from the command line so we don’t really need this ‘Manager’.

So, that’s it. Our Apache Tomcat Server is ready for use.

Author’s Bio

mobisoft-pritam
Pritam Barhate

Pritam Barhate, with an experience of 10+ years in technology, heads Technology Innovation at Mobisoft Infotech. He has a rich experience in design and has been a consultant for a variety of industries and startups. At Mobisoft Infotech, he primarily focuses on technology resources and develops the most advanced solutions. Follow him @pritambarhate