Install Tomcat on AWS Ubuntu and IDE

Install Tomcat on | Ubuntu | Windows | Amazon EC2 | Docker | Intellij | Jenkins

To install Tomcat 7, Tomcat 8 and Tomcat 9 is a habitual task because Tomcat is synonymous with Java web application development. You install Apache Tomcat

  1. on a local Windows or Linux (Ubuntu) Server
  2. on the Amazon AWS cloud EC2 machines
  3. within Docker and Amazon Docker containers
  4. inside Eclipse and IntelliJ IDE’s for Java
  5. to run Jenkins, Jira and Nexus software

Yes you can use cloud managed Apache Tomcat instances, but you’ll encounter Tomcat within the IDE, the local network and containerized and clouds.

How to Install Tomcat 7 on Ubuntu

Amazon AWS has no official support for Ubuntu 16.04 so most EC2 containers run Ubuntu 14.04 which in turn has no easy apt-get installation for Tomcat8 and Tomcat9.

Hence Tomcat 7 is still the container of choice for production Java web applications for Ubuntu EC2 machines on the Amazon cloud.

Follow instructions to install Oracle Java 8 for a stable supported Tomcat instance. Once done install Tomcat7 with the below steps

  1. install Tomcat 7 with sudo apt-get install tomcat7
  2. install the manager app with sudo apt-get install tomcat7-admin
  3. allow tomcat7 user access with sudo chgrp -R tomcat7 /etc/tomcat7
  4. update the tomcat7 group with sudo chmod -R g+w /etc/tomcat7

But if you start Tomcat 7 you will get a JAVA_HOME error.

Tomcat 7 Does Not Read JAVA_HOME

You get no JDK or JRE found - please set JAVA_HOME if you run sudo service tomcat7 start

This is slightly disconcerting but do not worry. If you followed the Oracle 8 Java install and you run whereis java you will get a reply like this.

java: /usr/bin/java /usr/bin/X11/java /usr/share/java /usr/share/man/man1/java.1.gz

But cut to the chase, your JAVA installation is in /usr/lib/jvm/java-8-oracle/jre/bin – you can choose to use the heavily linked /usr/bin/java – we prefer calling a spade a spade – it is better to tell it like it is so that your Tomcat7 configuration file clearly states what version of JAVA is being used.

How to Set JAVA_HOME in Tomcat 7

You need to tell Tomcat7 where JAVA_HOME is and the /etc/default/tomcat7 file is the best place to do that.

To see the whole file visit the tomcat7 configuration in /etc/default.

All you need do is to uncomment the JAVA_HOME line and set it to the folder that includes that an executable called java

JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre

Configuring Tomcat’s Home Directory

Web applications will frequently write logs and read files from the ${user.home} (home) directory. We need to give the tomcat7 user ownership (unfettered access) to its own home (directory).

For Tomcat 7 home is /usr/share/tomcat7 so run these commands.

  1. give tomcat7 ownership to its own home sudo chown -R tomcat7:tomcat7 /usr/share/tomcat7
  2. set the usual 755 permissions sudo chmod -R 755 /usr/share/tomcat7

Do not forget to deploy any assets required by web applications (like application.properties) into your newly prepared tomcat7 home.

Starting and Testing Your Tomcat 7 Server

Now you are ready. Just start the Tomcat service and smoke test it.

  1. to start tomcat7 – sudo service tomcat7 start
  2. to smoke test it – wget http://localhost:8080/

If all is well the reply will look like this.

–2016-10-16 16:37:24– http://localhost:8080/
Resolving localhost (localhost)… 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:8080… connected.
HTTP request sent, awaiting response… 200 OK
Length: 1895 (1.9K) [text/html]
Saving to: ‘index.html’
100%[======================================>] 1,895 –.-K/s in 0s
2016-10-16 16:37:25 (376 MB/s) – ‘index.html’ saved [1895/1895]

If setting up Tomcat 7 in the AWS Cloud or on any internet visible machine – it pays to run it behind the Apache2 Web Server. The web server acts as a proxy for Tomcat.

How to Run Tomcat7 Behind an Apache Proxy

To configure Tomcat7 behind an Apache web server proxy can be done in one of two ways depending on whether you are using (and enforcing) HTTPS (SSL) traffic.

If you are not – the Tomcat7 configuration involves enabling a couple of Apache modules and adding proxypass configuration inside the 000-default.conf file in /etc/apache2/sites-available

This is documented in another blog.

How to Install Tomcat 8 on Ubuntu

The install process has changed for Tomcat 8 and Tomcat 9 especially around the manager and deploy use cases. You are now encouraged to use a robot to deploy your next web application version as per DevOps, continuous delivery and continuous integration (with the Jenkins CI Server).

After installing Tomcat, you can proxy it behind the Apache Web Server using the middleware service behind Apache pattern. You should also consider using the SSL (HTTPS) protocol as a transport mechanism. And how about Work, REST and Play2? Will you use the Spring FrameWork, a REST architecture or Play2 to help with your MVC needs.

We propose not using Spring and not using Play2 to Setup a Killer Java Web Application.

How to Install Tomcat 9 on Ubuntu

Use the below steps.

  1. Use apt-get to install tomcat9 and tomcat9-admin
  2. Setup the Tomcat GUI (html) manager role
  3. Setup the robot deploy (text) manager role
  4. Deploy your new tomcat-users.xml file
  5. Setup the CATALINA environment variables

1. Using apt-get to install tomcat9 and tomcat9-admin

It pays to install Java 8 before installing Tomcat 8. Check with the java -version command.

With Java 8 on board, Once you have

  1. sudo apt-get install tomcat9
  2. sudo apt-get install tomcat9-admin
  3. sudo chgrp -R tomcat9 /etc/tomcat9
  4. sudo chmod -R g+w /etc/tomcat9

Explain the chgrp and chmod commands?

By default the tomcat9 user cannot write to the /etc/tomcat9 directory. However some admin features like application deployment and virtual host creation need write access to the directory. So to use them we execute sudo chgrp -R tomcat9 /etc/tomcat9 and sudo chmod -R g+w /etc/tomcat9 commands to give the tomcat 9 group the necessary permissions.

2. Tomcat GUI Admin Role Setup

These roles enable Maven and Jenkins CI (and/or Archiva) to release your web application automatically into Tomcat assuming they pass the build quality criteria.

This configuration step has changed for Tomcat8 and Tomcat9.

The role names have changed. The primary role is now manager-gui. It is neither manager nor admin. Also the xml tag is now username and not, name.

Visit the full tomcat-users.xml configuration file here.

This is the snippet to add in the /etc/tomcat9/tomcat-users.xml file

    <role rolename="manager-gui"/>
    <user username="admin" password="p455w0rd" roles="manager-gui" />

Now it is time to set CATALINA (and co) environment variables.

3. Tomcat Robot (text) Role for RESTful Service Management

These roles enable Maven and Jenkins CI (and/or Archiva) to release your web application automatically into Tomcat assuming they pass the build quality criteria.

This configuration step has changed for Tomcat8 and Tomcat9.

The role names have changed. The primary role is now manager-gui. It is neither manager nor admin. Also the xml tag is now username and not, name.

Visit the full tomcat-users.xml configuration file here.

This is the snippet to add in the /etc/tomcat9/tomcat-users.xml file

    <role rolename="manager-gui"/>
    <user username="admin" password="p455w0rd" roles="manager-gui" />

4. Deploy the tomcat-users.xml file

These roles enable Maven and Jenkins CI (and/or Archiva) to release your web application automatically into Tomcat assuming they pass the build quality criteria.

This configuration step has changed for Tomcat8 and Tomcat9.

The role names have changed. The primary role is now manager-gui. It is neither manager nor admin. Also the xml tag is now username and not, name.

Visit the full tomcat-users.xml configuration file here.

Now it is time to set CATALINA (and co) environment variables.

5. Setting Tomcat’s Catalina Environment Variable

Very little works in Tomcat without CATALINA (and co) environment variables being correctly set. Check with the printenv command.

printenv | grep -i "CATALINA"

It is not set if nothing happens. Do a control test with printenv | grep -i "JAVA" which should return something. We need to push 2 environment variables into the /etc/environment file.

    sudo echo "CATALINA_HOME=/var/lib/tomcat9" >> /etc/environment
    sudo echo "CATALINA_BASE=/var/lib/tomcat9" >> /etc/environment

(If you get “permission denied” copy the file to your home folder, change it and copy it back).

Important – Until a reboot – adding to /etc/environment has no immediate effect. The /etc/environment move is done to allow the new variables to survive a reboot. So we must physically export the variables now if we want Tomcat and its applications to benefit.

    export CATALINA_HOME=/var/lib/tomcat9
    export CATALINA_BASE=/var/lib/tomcat9

Now you can rerun printenv | grep -i "CATALINA". After a reboot all shells (globally) will know of the CATALINA environment variables.

Archiva Install Addendum

Installing the Archiva repository manager into Tomcat requires another environment variable called CATALINA_OPTS.

Visit environment file loaded with all CATALINA variables.

Summary – Tomcat Install on Ubuntu

Don’t forget that Tomcat is available in the Amazon cloud. Even with this being the case, many developers choose to run Tomcat within their intranet, on their laptops, inside their IDE’s (with Cargo and/or JRebel), and even embedded inside their applications!

You can build Tomcat into your application’s JAR (yes JAR) file and execute it from any command line. Embedded Tomcat is a perfect fit for many use cases.

Leave a Reply

Your email address will not be published. Required fields are marked *