Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / cloud

Setup Tomcat on an Amazon EC2 Ubuntu Instance

0.00/5 (No votes)
14 Dec 2015CPOL8 min read 33K  
This is a study note to setup Tomcat on an Amazon EC2 Ubuntu instance.

Introduction

This is a study note to setup Tomcat on an Amazon EC2 Ubuntu instance.

Background

This is a study note to setup Tomcat on an Amazon EC2 Ubuntu instance. It should be a simple task to setup Tomcat on an Amazon EC2 Ubuntu instance, but there are a few steps that are not so obvious. In this study note, I want to keep a record on these critical steps, so it will be even easier when I need to do it next time to save some time.

Create an Amazon AWS Account

Creating an Amazon AWS account is an easy step. you can go to the Amazon web page to complete the registration. There are different levels of services provided by Amazon. For my purpose, I just created a one year free account. Amazon will need from you an email address, a credit card and a phone number to complete the registration. They promise not to charge you if you do not exceed the usage quota. It should not be difficult to create an account by following the Amazon instructions. But I want to remind you not to exceed the free quota if your usage is not for any commercial purposes. Otherwise you will be charged.

Launch an Ubuntu Instance

After finishing the registration, you can login to the AWS management console from the Amazon AWS link. Because the UI of the AWS management console may end up constantly changing over the time, I do not want to go to the exact details on the launching process. I just want to keep a record of the important steps. When you start launching an AWS instance, Amazon will offer many machine images to choose from. The "Ubuntu Server" image will be available for the users with a free tier account. As a free user, you can only launch a "micro" instance which has only a very limited number of CPUs and memory. But it should be enough for testing your applications. When you launch the instance, you should keep the following items in mind.

  • A key pair - It is a pair of encryption keys. It will be used when you access the Ubuntu instance from your local computer. If you do not already have one, Amazon will offer to create one for you. You need to download this key pair and keep it on your local computer. The key pair file has a "pem" extension;
  • A security group - It is a set of rules to control how the instance is accessed from the outside world. During the launch process, Amazon may not ask you to specify the security group. It will create a default one for you. By default this security group will open the port 22 for SSH connections. You will also have options to configure this security group to specify other network protocols and the IP ports that this Ubuntu instance opens to the outside world;
  • By default the Ubuntu instance will have a user named "ubuntu". You will be using this user to access the Ubuntu instance.

Image 1

After the successful launch of the Ubuntu instance, Amazon web page will be showing you the important information about this instance, such as the public IP, security group and the key pair.

Access the Instance by SSH

The Ubuntu instance can be accessed from your local computer running on any operating systems. Since I have a Linux mint, I will be using the Linux machine to access the Ubuntu instance by SSH. If SSH is not installed on your computer, you need to install it by yourself. Before running SSH, you need to make sure your key pair file accessible only by yourself by issuing the following command:

chmod 400 song-aws-keypair.pem

The file "song-aws-paypair.pem" is the key pair file on my computer. After changing the key pair file attribute, you can connect to your Ubuntu instance by the following command:

ssh -i "$HOME/Development/amazon-aws/keys/song-aws-keypair.pem" ubuntu@52.35.120.158

You will need to make sure the path to the key pair file and the public IP of the Ubuntu instance are correct.

Image 2

The SSH will offer an terminal to your Ubuntu instance that you can issue any shell commands to the Ubuntu instance in the Amazon cloud.

Access the Instance by TightVNC

The SSH enables you to manage your Ubuntu instance through shell commands. But not all the people use shell commands as their primary mean to use Linux. For a lot of users, it is still nice to connect to the Ubuntu instance with some GUI clients. The method shown here is borrowed from this Youtube video. To enable the support for TightVNC from the Ubuntu instance, you will need to connect to the instance using SSH. You can then issue the following command to install the "ubuntu-desktop".

sudo apt-get install ubuntu-desktop

You may need to run an update before the install to make the installation successful.

sudo apt-get update

You will also need to install the "vnc4server" and the "gnome-panel".

sudo apt-get install vnc4server
sudo apt-get install gnome-panel

After the installation, you can run the "vnc4server" by the following command:

vnc4server

When you run "vnc4server" the first time, it will ask you to specify a password. It will be the password that the GUI client uses to access the server. Before you can access the Ubuntu instance from a GUI client from your local computer, you will need to further configure the server. You need to shutdown the server by the following command:

vncserver -kill :1

You can then modify the ".vnc/xstartup" file to the following and save the file.

Image 3

You can then restart the vnc server by the following command so the VNC client can connect to the Ubuntu instance:

vnc4server

You will also need to use Amazon AWS management console to change the security group to accept the port 5901 for the GUI client to connect to the the VNC server.

Image 4

Now the Unbuntu instance is ready for connection. As my first VNC client, I choose the TightVNC. You can go to the download page to the download TightVNC. Since I have installed the VNC server on my Ubuntu instance, I will only need the "TightVNC Java Viewer". After unpack the zip file, you can run the "tightvnc-jviewer.jar" file if you have the appropriate JRE on your local computer.

Image 5

The password that you need to login to the VNC server is the password that you specified when your first time running the "vnc4server". After a successfully login to the VNC server, you should be able see the "ubuntu-desktop" from the TightVNC viewer.

Image 6

If you feel that you are more used to shell commands than the GUI, obviously you do not need to connect to your Ubuntu instance by TightVNC. But if you feel that the GUI can help your to improve your productivity, you can set it up.

Download Java and Tomcat

With the of the help of the GUI client, you can download both Java and Tomcat from the corresponding web sites using the Firefox on your Ubuntu instance.

After downloading Java and Tomcat, you can unpack them to the directory of your choice.

Image 7

In order for the Linux terminals to find the correct JRE and the "catalina.sh", you can add the following lines into the "ubuntu" user's ".profile" file. You need to put the correct path to your files.

export JAVA_HOME=$HOME/Installation/jdk1.8.9_65
PATH=$HOME/Installation/apache-tomcat-7.0.67/bin:PATH

After restarting the Ubuntu instance or issuing the command "source $HOME/.profile", you can start your tomcat by the following command:

catalina.sh start

you can stop your tomcat by

catalina.sh stop

If your tomcat is started, you can open Firefox on your Ubuntu instance, and type in the url "http://localhost:8080", you should see the following web page.

Image 8

Enable Port 80 for the Tomcat

Now that your tomcat is running, you would expect that you can simply go to any browser and type in the url "http://52.35.120.158", you should see the tomcat home page. But the web page is not accessible. You will need to run the following command to redirect any TCP request to port 80 to 8080.

sudo /sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

To save the "iptables", you need to install "iptables-persistent".

sudo apt-get install iptables-persistent

You can then save the "iptables" by the following command:

sudo /etc/init.d/iptables-persistent save

Finally, you need the servlets inside your web application to act as if the incoming requests were directed to port 80 to Include the proxyPort attribute in your HTTP connector configuration in the "server.xml".

<Connector port="8080" proxyPort="80" .../>

Now if you open any web browser that has the internet access and type in the url with the public IP of the Ubuntu instance, you should see the Tomcat home page.

Image 9 

Points of Interest

  • This is a study note to setup Tomcat on an Amazon EC2 Ubuntu instance;
  • The free AWS service may serve as a nice playground to test your applications on the internet, if you do not have a server that has a public IP;
  • If you are an expert on AWS and Ubuntu, you may find that my methods too verbose. But it is nice to keep a record on the steps so it will be easier if I will be doing this again in the future;
  • I hope you like my postings and I hope this study note can help you one way or the other.

History

First Revision - 12/14/2015.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)