Install Nginx, Tomcat 7 and Java 8 on Ubuntu 16.04

by Didin J. on Jan 29, 2017 Install Nginx, Tomcat 7 and Java 8 on Ubuntu 16.04

How to install production ready Nginx as a reverse proxy to redirect port 80 to Tomcat 7 with Java 8 on Ubuntu Server 16.04.

This is a tutorial about how to install production ready Nginx, Tomcat 7 and Java 8 on Ubuntu 16.04 machine that is used by today's web server.

So, don't waste your time. Let's begin.

1. Install Java 8

We assume that you already have VPS hosting that include ubuntu 16.04 as default operating system. With this clean ubuntu 16.04, we start by install Java 8 first. Now, enter your VPS via putty or ssh client in the terminal. In putty or terminal run this command.

sudo add-apt-repository ppa:webupd8team/java

Next, update apt by this command.

sudo apt update

Now, install Oracle Java 8 in your ubuntu by this command.

sudo apt install oracle-java8-installer

Check Java installation by this command.

javac -version

If you see like below, your Java installation is ok.

javac 1.8.0_121

To make your Java 8 installation as default run this command.

sudo apt install oracle-java8-set-default

For using Tomcat 7 with Java 8, make sure that JAVA_HOME is set. To find where Java installation, type this command.

sudo update-alternatives --config java

You will see like this, just type enter and copy the used path.

There is 1 choice for the alternative java (providing /usr/bin/java).

  Selection    Path                                     Priority   Status
  0            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      auto mode
* 1            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Now, edit "/etc/environment" by the terminal editor.

sudo nano /etc/environment

Add this line to the end of the file.


Save, quit and reload the configuration.

source /etc/environment

Test your configuration by this command.


2. Install Tomcat 7

It's easy to install Tomcat 7 using apt. Just type this command.

sudo apt-get install tomcat7

If you see an error at the of installation like this.

Creating config file /etc/logrotate.d/tomcat7 with new version
Job for tomcat7.service failed because the control process exited with error code. See "systemctl status tomcat7.service" and "journalctl -xe" for details.
invoke-rc.d: initscript tomcat7, action "start" failed.
dpkg: error processing package tomcat7 (--configure):
 subprocess installed post-installation script returned error exit status 1
Setting up authbind (2.1.1+nmu1) ...
Processing triggers for systemd (229-4ubuntu12) ...
Processing triggers for ureadahead (0.100.0-19) ...
Errors were encountered while processing:
E: Sub-process /usr/bin/dpkg returned an error code (1)

Edit "/etc/default/tomcat7" and uncomment and change JAVA_HOME like this.


Now, start Tomcat7 service by this command.

sudo service tomcat7 start

To make Tomcat 7 running smoothly, edit again "/etc/default/tomcat7" and change "JAVA_OPTS".

JAVA_OPTS=" -Djava.awt.headless=true -Xmx512m -XX:MaxPermSize=256m -XX:+UseConcMarkSweepGC"

Restart and test your Tomcat 7 in the browser.

sudo service tomcat7 restart

3. Install Nginx

To install Nginx just type this command.

sudo apt-get install nginx

If there is show choices of "/etc/default/tomcat7" configuration, leave it to default locally modified configuration and enter.

Now, test your Nginx HTTP server in your browser.

4. Make Tomcat 7 reverse to Nginx

Now is the time to make Tomcat 7 reverse to Nginx, so the user can access Tomcat 7 application via Nginx on port 80. Next, edit Nginx configuration.

sudo nano /etc/nginx/sites-enabled/default

Find "location /" and change to this code.

location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;

Restart Nginx.

sudo service nginx restart

Open your web server in the browser without port 8080. If you see tomcat welcome page, you successfully install Nginx as a reverse proxy of Tomcat 7.

If you using the limited resource of VPS (eg. 1 GB RAM), set swap on ubuntu server. Check if the system has configured swap by this command.

sudo swapon --show

To check if there's no active swap on disk use this command.

free -h

Before setting a swap, check your hard disk space by this command.

df -h

If there's enough available space, you can create a swap file. For my requirements of Tomcat 7 application, we need to add 1GB swap.

sudo fallocate -l 1G /swapfile

Verify the amount of swap file that just created.

ls -lh /swapfile

Next, enable the swap file. But first, make /swapfile accessible to "root".

sudo chmod 600 /swapfile

Check permission changes.

ls -lh /swapfile

You should see permission like this.

-rw------- 1 root root 1.0G Jan 23 01:44 /swapfile

Next, mark the swap space by this command.

sudo mkswap /swapfile

Next, make enable the swap file.

sudo swapon /swapfile

Verify available swap.

sudo swapon --show

Check again swap space by this command.

free -h

To make swap permanent, add this file information to "/etc/fstab".

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

That it's, now your server is ready for production and serve world wide web.