How to install tomcat on AWS EC2 instance

When you start up your EC2 instance, it comes with a default jdk installed (at the time of writing this article, jdk6). So you will need to upgrade it to a newer version.

Install Java

To install jdk8 on an EC2 instance, run following commands in the terminal:

sudo apt-get update
sudo apt-get install openjdk-8-jdk

Or you can use sudo apt-get install default-jdk. After it finishes, run java -version command to make sure it’s properly installed. It should print something like:

openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-2ubuntu1.16.04.3-b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)

Install tomcat

Now go to tomcat’s official download site and download the latest tomcat distribution. Unzip the file: tar -xvzf apache-tomcat-9.0.0.M26.tar.gz and copy the extracted content to the location you want to install your tomcat. I will assume it’s installed in the /opt/tomcat-9

Now create tomcat group and user and assign the tomcat folder to it:

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

Note: the -s /bin/false switch, prevents this user of being used as a normal user to login.

Now you can start and use tomcat using its standard startup and shutdown commands like: ./ run in the /opt/tomcat-9/bin folder.

Although at this stage, you are able to run tomcat manually, a good practice is to register it as a service so that every time EC2 is restarted, tomcat starts up automatically. If this is what you wish to do, keep reading!

Running Tomcat as a service

Create the systemd file: nano /etc/systemd/system/tomcat.service and copy the following to the file, save and exit:

Description=Apache Tomcat


Environment="CATALINA_OPTS=-Xms512m -Xmx1G"
Environment="JAVA_OPTS=-Dfile.encoding=UTF-8 -Djava.awt.headless=true"




Now run following commands to start the tomcat:

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

After this, tomcat should be up and you should be able to access http://localhost:8080

From now on, you can use following commands to start or stop the tomcat:

sudo service tomcat start
sudo service tomcat stop

Redirecting requests to tomcat

If you would like all the requests to the EC2 instance to be redirected to your tomcat, you have 2 options. First is to have tomcat listen to the port 80. For this approach, you will need to install tomcat as root user because root is the only user who can open ports under 1000. This approach is not recommended as it exposes some security risks. The second option is to forward all the requests from port 80 to port 8080 using iptables. To do so, run following commands in the terminal:

sudo apt-get install iptables-persistent
sudo /sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo netfilter-persistent save
sudo netfilter-persistent reload

Congratulations! You are all set now!

Share post:

Follow Us On:

Receive Our News and Announcements

*Stay up to date with events, blogs and announcements.

Copyright ©2020 Taikera.