Install Tomcat 8 on AWS EC2

I am in the process of moving my client's app from Elastic Beanstalk to EC2.

In this post, I will outline the steps that I undertook to get Tomcat 8 running with Apache web server in front of it - all deployed in a Linux based EC2 in AWS.

The assumption of this post is, you already have an AWS account and know your way around EC2.

First off, login to your AWS account and go to EC2. Launch an EC2 instance from the menu.

Pick a Linux based AMI here, my recommendation is Amazon Linux AMI. In the time of writing, Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type (ami-09b42976632b27e9b) is the latest Amazon Linux AMI and picked that up.

Remember to configure a security group, if you don't already, that allows traffic on:

  • port 80 for the http
  • port 22 for ssh

Once your instance has launched, ssh into your instance.

ssh -i your-amazon.pem ec2-user@instance-address

Install Java 8

This step is optional as the AMI would have come with Java installed.

In my case, the pre-installed Java in the AMI is Java 7. However, I needed it to be Java 8 because Java version of the machine that produces the Grails WAR must match the Java version where the WAR is deployed, I outlined why in this post.

sudo yum install java-1.8.0
sudo yum remove java-1.7.0-openjdk

Install Tomcat 8

When you are inside your instance, run the following commands:

ssh-in run sudo yum update # good practice to update installed packages
sudo yum install tomcat8 tomcat8-webapps
sudo service tomcat8 start

Install Apache httpd

Next we want to install Apache httpd to be a proxy in front of tomcat.

sudo yum -y install httpd
sudo service httpd start

Install mod_jk

To get httpd and tomcat talk to each other we need a connector called mod_jk.

sudo yum install libtool httpd-devel autoconf.noarch
wget http://apache.mirror.digitalpacific.com.au/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.44-src.tar.gz
tar xvzf tomcat-connectors-1.2.44-src.tar.gz

Build mod_jk

The steps below is a summary from Apache HTTP Server (mod_jk) HOWTO, which I encourage you to have a quick read before attempting the build below.

cd /home/ec2-user/tomcat-connectors-1.2.44-src/native
./buildconf.sh
./configure -with-apxs=/usr/sbin/apxs
make
sudo cp apache-2.0/mod_jk.so /etc/httpd/modules/ # mod_jk.so is the binary result from the compilation

Configuration changes on httpd and tomcat8

mod_jk comes with a worker.properties example, copy that to your tomcat8 directory:

sudo cp conf/workers.properties /usr/share/tomcat8/conf/

Modify the workers.properties:

sudo vim /usr/share/tomcat8/conf/workers.properties

Add the following on workers.properties:

worker.list=jk-status
worker.jk-status.type=status
worker.jk-status.read_only=true
worker.jk-status.port=8009
worker.jk-status.host=localhost
worker.jk-status.type=ajp13
worker.jk-status.lbfactor=1

Modify httpd.conf:

sudo vim /etc/httpd/conf/httpd.conf

Add the following lines on the httpd.conf, you can place them anywhere in the file. However to be a little organised, I recommend putting these near the other LoadModule statements.

LoadModule jk_module modules/mod_jk.so
JkWorkersFile /usr/share/tomcat8/conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel error
JkLogStampFormat "[%y-%m-%d %H:%M:%S.%Q] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /* jk-status

Now if all goes well, type in the URL of the EC2 to your browser and you should see the Tomcat welcome page. Worth noting, with this setup your site is served on port 80 as opposed to the usual port 8080 for tomcat.

Hope that helps :)