- Step by step guide
- Deploying your application
- Advanced configuration
Tomcat is an application server to run Java applications.
With our Tomcat Service, you get a Tomcat 8 or 9 installation that can be managed directly by the
www-data user. This means more flexibility in the configuration of the instances, without having to keep them up-to-date yourself.
Step by step guide
You can skip this section if you migrated from a Tomcat 6 managed solution, as we have done all that for you already.
In this section, we will guide you through the (rather short) process of setting up your own Tomcat instance with a reverse proxy.
- Ubuntu 16.04 Xenial (tomcat8) or 18.04 Bionic (tomcat9)
- Tomcat 8 or 9 configured by us on your server (contact us to install it: firstname.lastname@example.org)
nine-manage-vhostsinstalled on the server
Creating a Tomcat instance
We will use the
tomcat9-instance-create tool (official tomcat8 documentation) or (official tomcat9 documentation) to create our Tomcat instances. A few parameters should be provided:
HTTP port (defaults to 8080)
Control port (defaults to 8005)
The following command will create a tomcat instance in the folder
~/test-server.ch on port
8081 with control port
tomcat8-instance-create -p 8081 -c 8006 ~/test-server.ch
tomcat9-instance-create -p 8081 -c 8006 ~/test-server.ch
- When creating multiple tomcat instances, you need to ensure that the HTTP port and the control port are unique.
Making Tomcat accessible to the outside world with a reverse-proxy
Tomcat listens on localhost by default. It is common practice to place a reverse-proxy in front of Tomcat. We will use
nine-manage-vhosts to create that reverse-proxy.
The following command will create a vhost named
test-server.ch that will forward all of its traffic to the Tomcat instance on port
sudo nine-manage-vhosts virtual-host create test-server.ch --template=proxy --template-variable PROXYPORT=8081
We assume in the Tomcat template that all of the Tomcat instances are located under
To make use of a Let’s Encrypt certificate and https you can set the template
Start the Tomcat instance on system startup
We provide a default systemd template to start/stop your Tomcat instances.
The first thing we need to do is to ensure that the instance will be launched on server boot with the following command:
systemctl --user enable email@example.com
The next commands will start/stop the Tomcat instance named
systemctl --user start firstname.lastname@example.org systemctl --user stop email@example.com
To check the current status of the Tomcat instance, you can use the following command:
systemctl --user status firstname.lastname@example.org
- The first part of the service name is unchanged :
user-tomcat@. What comes after the
@is your Tomcat instance name.
For more information about managing services with systemd, please refer to this article.
Putting it all together
The following commands will setup a tomcat installation accessible from http://test-server.ch :
# Create the Tomcat instance, accessible on http://localhost:8081 tomcat8-instance-create -p 8081 -c 8006 ~/test-server.ch # Add a reverse proxy for Tomcat to make it accessible from the outside. sudo nine-manage-vhosts virtual-host create test-server.ch --template=proxy --template-variable PROXYPORT=8081 # Deploy your application to the instance (see below). [...] # Make the instance launch on boot systemctl --user enable email@example.com # Start the Tomcat instance. systemctl --user start firstname.lastname@example.org
Deploying your application
In this section, you will deploy your Java application to the Tomcat instance. Below are a few things to know about the deployment process:
An application usually has the form of a
Where to put the application?
An application must be copied to
There is also the possibility to run multiple applications in one virtual host. For that, every application needs its own directory under
/home/www-data/<vhost-name>/webapps. The applications are then accessible at “http://servername.com/name_of_the_folder_in_webapps/”.
Let’s say we have the same Tomcat instance as before:
We want to deploy our application
test-application, that we currently have as a war file:
cp test-application.war /home/www-data/test-server.ch/webapps/ # Tomcat will extract it to a folder called "test-application": # /home/www-data/test-server.ch/webapps/test-application
The application is accessible at http://test-server.ch/test-application, and its logs are under
Where does it log?
If your applications log to the standard output, the logs will be written in
Please bear in mind that your
catalina.out will grow big quickly with this method, and that it is recommended to use a logging utility instead of the standard output.
If you still want to use STDOUT in your application for logging purposes, there are two things you should do:
- Disable the log rotation in Tomcat, because it is not rotating the logs properly. This can be done by editing the file
conf/logging.propertiesand add following lines:
1catalina.org.apache.juli.FileHandler.rotable = false 2localhost.org.apache.juli.FileHandler.rotable = false
- Configure a proper log rotation.
For more information on deployment, please refer to the official documentation.
Since the tomcat instances are managed by
www-data, you can change all the configuration you need without using sudo or contacting nine (see the official documentation tomcat8 or official documentation tomcat9 as a reference).
This being said, we would like to show you some common configuration tweaks, and we do them via environment variables.
Some parts of the behaviour of Tomcat can be controlled by setting some environment variables in the file
Below are some examples of what can be changed using them:
Using a different Java version
Add the following to the
setenv.sh file and restart your instance to switch the current java version:
Change the output log location
Add the following to the
setenv.sh file and restart your instance to log to a different file:
Change the maximum amount of memory
tomcat9-instance-create script sets in
setenv.sh a default of 128MB of memory for a given instance. Just replace the 128MB with the value of your choosing (must a multiple of 1024 greater than 2 MB).
For instance, the following would allow your instance to use at most 512MB of memory:
if [ -z "$JAVA_OPTS" ]; then JAVA_OPTS="-Djava.awt.headless=true -Xmx512M" fi
Some parameters can not be set by Tomcat but rather need to be set by systemd. Those settings can be modified by so-called systemd drop-ins.
They can be set either for all instances of a user or for a specific instance.
To modify the configuration for all instances, place the file in
To only modify a single instance, place the file into
For example, to set the Max Open Files limit, drop this file into one of the folders mentioned above:
# nofiles.conf [Service] LimitNOFILE=2048
Custom startup services
If you prefer to use your own startup services instead of the one provided by nine, please refer to this article.