Apache Tomcat is an open source Java servlet container that's used to run Java webapps. It's simple, easy to deploy, and a downright wonderful implementation of Java servlet tech that I've used (especially recently) in software projects. It's also used extensively in other systems that I use and maintain, particularly Atlassian's stack (JIRA, confluence, crowd, etc.).
Below is a guide to deploying a Java webapp with Tomcat in a simple (the simplest?) way on a Linux server (in this example I'll be using Ubuntu Server 16.04).
I prefer just running Tomcat without any bells and whistles (like the included manager app) - hence I'll cover setting up Tomcat and deploying a .war file.
Tomcat will need JDK (Java Development Kit). See my Install JDK 8 article for how to do this.
Next, let's download Tomcat. We'll be downloading from https://tomcat.apache.org/download-90.cgi and we want the
tar.gz binary distribution:
An easy way to download this directly to your server is to right-click on the tar.gz link and copy the link address. Then use
wget to download (you can also use
curl if that is what you have on your Linux distro). For example:
We can run Tomcat from any location on our server. We'll simply extract it to wherever and run it from there. You can follow this guide here. We'll do likewise and extract to
Note that if you any specific jar, like the JDBC connector for MySQL, you should copy those required jars to
I prefer to keep things simple and strip Tomcat down to only run the web application that I want to deploy. So, I'm going to delete everything in the
webapps folder on the default tomcat install. This folder (by default) contains applications like the manager app that can be used to upload and deploy webapps. I find Tomcat dead simple enough that I don't need any including management gui and would prefer to start clean:
That's pretty much the setup that I'm going to use.
Installing the .war file to run
Tomcat runs .war files - self-contained web archive files that contain your web application. You don't really install these, just simply copy it to
For example (if I had a .war file in my home folder):
We can now start Tomcat. You can do this by runing the
startup.sh script in
/opt/tomcat/bin, or use
Passing variables to Tomcat
You can define and pass variables to Tomcat in order to change how your application runs. For example, in a Spring Boot app you might defined different
.properties files to define running the application in a development/test mode, where you might use an in-memory database for persistency (h2 is great for this!), and another
.properties file for production (which could connect to a production MySql database for example).
You then can pass variables to Tomcat to deploy according to the variables you pass it.
For example, in one of my vaadin applications, I have
aplication.production.properties files which define development and production modes respectively. By default my spring boot app runs the
You'll note that in the above production properties file we define certain variables like
USERNAME, PASSWORD etc.
To get Tomcat to use not only the above
application.production.properties file, but also know variables values for the above variables, we need to pass these variables to Tomcat. We do this by defining a
Let's do this for the above example, first fire up a text editor (I like nano) and create the
Now add the following context (modified to your needs):
These variables will be passed to Tomcat on startup which should startup your application according to the variables and
.properties files passed.