Tomcat as windows service

Creating the Windows service is only possible on the command line. The basic syntax is:

"C:\Program Files\Tomcat\bin\tomcat8.exe" //IS//Tomcat8 --DisplayName="Apache Tomcat Application Server"

This line would install the service. The argument //IS// installs the service without starting it. Probably the service wouldn’t start anyway: A lot of configuration data needs to be stored into the registry first. Actually Tomcat is blind like a mole.

The basic installation from above can be extended with configuration data. The installation can be run multiple times in an update mode by using the argument //US//. Using multiple runs makes our life easier and we can break down the configuration into logical blocks without losing the global picture.

Let’s use this line as the first action to create the service on your server:

"C:\Program Files\Tomcat\bin\tomcat8.exe" //IS//Tomcat8 --DisplayName="Apache Tomcat Application Server" --Description="Apache Tomcat Application Server" --Startup="auto"

Now we append the path to the jvm.dll and the class path:

"C:\Program Files\Tomcat\bin\tomcat8.exe" //US//Tomcat8 --Classpath="C:\Program Files\Tomcat\bin\bootstrap.jar;C:\Program Files\Tomcat\bin\tomcat-juli.jar"
"C:\Program Files\Tomcat\bin\tomcat8.exe" //US//Tomcat8 --Jvm="C:\Program Files\Oracle Java Server\jre\bin\server\jvm.dll"

Important: The path to the jvm.dll depends, which Java kit you installed previously on your server.

C:\Program Files\Oracle Java Server\jre\bin\server\jvm.dll
C:\Program Files\Oracle Java JDK\jre\bin\server\jvm.dll

Now let’s add the remaining Java Options:

"C:\Program Files\Tomcat\bin\tomcat8.exe" //US//Tomcat8 ++JvmOptions="-Dcatalina.home=C:\Program Files\Tomcat;-Dcatalina.base=D:\Tomcat;-Djava.endorsed.dirs=D:\Tomcat\endorsed;-Djava.io.tmpdir=D:\Tomcat\Temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=D:\Tomcat\conf\logging.properties"

Let’s continue by adding the logging properties:

"C:\Program Files\Tomcat\bin\tomcat8.exe" //US//Tomcat8 --LogLevel="Info" --LogPrefix="tomcat_service_" --LogPath="D:\Tomcat\Logs" --StdOutput="auto" --StdError="auto" --PidFile="tomcat8.pid"

And finally we add the Startup and Shutdown properties:

"C:\Program Files\Tomcat\bin\tomcat8.exe" //US//Tomcat8 --StartClass="org.apache.catalina.startup.Bootstrap" --StartMode="jvm" ++StartParams="start"
"C:\Program Files\Tomcat\bin\tomcat8.exe" //US//Tomcat8 --StopClass="org.apache.catalina.startup.Bootstrap" --StopMode="jvm" ++StopParams="stop" --StopTimeout="0"

Please check the configuration by starting the Tomcat applet:

«C:\Program Files\Tomcat\bin\tomcat8w.exe» //ES//Tomcat8

apache-tomcat-properties-java1.png

Note: Anything we created is stored the windows registry:

HKML\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\Tomcat8


Reference: Apache Tomcat — Windows Service


Tomcat runtime configuration

One very important thing is left: Tomcat needs to know about his memory configuration and how he should behave in general.

Disclaimer: The following values are working well on the authors servers. You can take these settings as a starting point.
These settings are NOT an official recommendation of the Lucee Association Switzerland.

So, stop this shouting and let’s get back to business. For this guide, we will use the following values:

-Xms4096m
-Xmx4096m
-Xss512k
-XX:NewSize=1024M
-XX:MaxNewSize=1024M
-XX:GCTimeRatio=5
-XX:ThreadPriorityPolicy=42
-XX:ParallelGCThreads=4
-XX:MaxGCPauseMillis=50
-XX:+DisableExplicitGC
-XX:MaxHeapFreeRatio=70
-XX:MinHeapFreeRatio=40
-XX:+OptimizeStringConcat
-XX:+UseTLAB
-XX:+ScavengeBeforeFullGC
-XX:CompileThreshold=1500
-XX:+TieredCompilation
-XX:+UseBiasedLocking
-Xverify:none
-XX:+UseThreadPriorities
-XX:+UseFastAccessorMethods
-XX:+UseCompressedOops
-XX:ReservedCodeCacheSize=256m

Let’s apply these settings on the server:

"C:\Program Files\Tomcat\bin\tomcat8.exe" //US//Tomcat8 ++JvmOptions="-Xms4096m;-Xmx4096m;-Xss512k;-XX:NewSize=1024M;-XX:MaxNewSize=1024M;-XX:GCTimeRatio=5;-XX:ThreadPriorityPolicy=42;-XX:ParallelGCThreads=4;-XX:MaxGCPauseMillis=50;-XX:+DisableExplicitGC;-XX:MaxHeapFreeRatio=70;-XX:MinHeapFreeRatio=40;-XX:+OptimizeStringConcat;-XX:+UseTLAB;-XX:+ScavengeBeforeFullGC;-XX:CompileThreshold=1500;-XX:+TieredCompilation;-XX:+UseBiasedLocking;-Xverify:none;-XX:+UseThreadPriorities;-XX:+UseFastAccessorMethods;-XX:+UseCompressedOops;-XX:ReservedCodeCacheSize=256m"

For you convenience, these command line snippets are available here as a script.

set jvm.serverDLL="C:\Program Files\Java\jre8u92\bin\server\jvm.dll"
set catalina.base=D:\Tomcat
set catalina.home=C:\Program Files\Tomcat
set catalina.binary="%catalina.home%\bin\tomcat8.exe"
set catalina.instanceName=Tomcat8
set catalina.displayName="Apache Tomcat Application Server"

%catalina.binary% //IS//%catalina.instanceName% --DisplayName=%catalina.displayName% --Description=%catalina.displayName% --Startup="auto" %catalina.binary% //US//%catalina.instanceName% --Classpath="%catalina.home%\bin\bootstrap.jar;%catalina.home%\bin\tomcat-juli.jar" %catalina.binary% //US//%catalina.instanceName% --Jvm=%jvm.serverDLL% %catalina.binary% //US//%catalina.instanceName% ++JvmOptions="-Dcatalina.home=%catalina.home%;-Dcatalina.base=%catalina.base%;-Djava.endorsed.dirs=%catalina.base%\endorsed;-Djava.io.tmpdir=%catalina.base%\Temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%catalina.base%\conf\logging.properties" %catalina.binary% //US//%catalina.instanceName% --LogLevel="Info" --LogPrefix="tomcat_service_" --LogPath="%catalina.base%\Logs" --StdOutput="auto" --StdError="auto" --PidFile="tomcat8.pid" %catalina.binary% //US//%catalina.instanceName% --StartClass="org.apache.catalina.startup.Bootstrap" --StartMode="jvm" ++StartParams="start" %catalina.binary% //US//%catalina.instanceName% --StopClass="org.apache.catalina.startup.Bootstrap" --StopMode="jvm" ++StopParams="stop" --StopTimeout="0" %catalina.binary% //US//%catalina.instanceName% ++JvmOptions="-Xms4096m;-Xmx4096m;-Xss512k;-XX:NewSize=1024M;-XX:MaxNewSize=1024M;-XX:GCTimeRatio=5;-XX:ThreadPriorityPolicy=42;-XX:ParallelGCThreads=4;-XX:MaxGCPauseMillis=50;-XX:+DisableExplicitGC;-XX:MaxHeapFreeRatio=70;-XX:MinHeapFreeRatio=40;-XX:+OptimizeStringConcat;-XX:+UseTLAB;-XX:+ScavengeBeforeFullGC;-XX:CompileThreshold=1500;-XX:+TieredCompilation;-XX:+UseBiasedLocking;-Xverify:none;-XX:+UseThreadPriorities;-XX:+UseFastAccessorMethods;-XX:+UseCompressedOops;-XX:ReservedCodeCacheSize=256m"

Now we are ready to start the Tomcat service for the very first time.

Overview

  • Introduction
  • Setup
    • Get Java
    • Get TomCat
    • Install TomCat as a Windows service
    • Setup TomCat as a Linux Service (systemd)
    • Use nginx as a reverse proxy
  • Basic usage
    • Start and stop TomCat manually
    • Verify TomCat server is running
    • Serve static files
    • Serve Java Server Pages (JSP)
    • Modify settings
    • Deploy web apps
  • Use the admin web interface
    • Create a TomCat admin user
    • Access admin web apps
    • Manage web apps
  • Conclusion
  • Reference links

Introduction

TomCat, in its simplest concept, is a web server. Well, it’s more than that. While it can serve static files, its primary purpose is to act as a ‘servlet container’ that serves Java web applications. It can process [.jsp] files, which are like PHP scripts but for Java, and it can also run [Java Servlets], which are classes that process the GET, POST, and other HTTP requests. TomCat will listen on TCP ports for HTTP requests, and route requests to your Java classes, JSP files, or static files. It is also possible to embed TomCat in to a standalone application, but that is out of the scope of this document.

Beginner level Java knowledge is expected, although there is virtually no Java code in this tutorial specfically. This will cover basic installation, configuration, and admin aspects. This is aimed at beginner to intermediate developers who want to learn about basic Java web app hosting. You should either be a system administrator setting up TomCat or a developer learning how to set up TomCat.

I would always recommend using the official documentation at http://tomcat.apache.org/ for the latest and most accurate information. This is meant as a quick start guide that includes the things I think are important to know to get started. There are a number of features that are not covered here. For example, TomCat can be embedded inside your application.

One popular alternative to TomCat is Eclipse Jetty. It is often used when you need a lightweight embedded servlet container. Servlet containers like TomCat and Jetty only support a subset of JavaEE specifications (JSP and servlets). You can use any full JavaEE server as an alternative since they will support servlets and JSP, but they will also support a lot more like Enterprise Java Beans (EJB), Java Persistence API (JPA), and other things. TomCatEE and Glassfish are examples of full EE servers.

Setup

Get Java

If you don’t already have a copy of Java, you will need a copy of the Java Runtime Environment (JRE) at a minimum. I recommended getting the full Java Development Kit (JDK), which includes the JRE. If you want to develop your own apps you must get the JDK. If you only want to run TomCat then you only need the JRE.

For information on installing multiple JDK versions on Windows, check out my Install multiple JDK in Windows for Java Development tutorial.

Steps:

  • Download Java JDK. I am using OpenJDK 11 in this example.
  • Extract zip file
  • Move Java to wherever you want. Could be your home directory,
    or /opt/openjdk11 for example. For the rest of this example we will
    assume the path /opt/openjdk11 or C:\opt\openjdk11 is used
  • Set the JAVA_HOME environment variable for TomCat. If you are only downloading the JRE and not the full JDK, set JRE_HOME.

Optional: In Linux, storing and running the JDK from your home directory works just fine for personal development, but if you are the system administrator and you want to make Java available for everyone you can move it to a location that everyone can read.

# Move Java to a system location (Linux)
sudo chown -R root:root /opt/openjdk11
chmod -R 775 /opt/openjdk11

Optional: Adding Java executables to your PATH, allowing it to be called from the command line. This is not required if you are setting up TomCat as a service, but is useful you want to use Java and the compiler from the shell to run programs and do development. Add the bin directory inside JAVA_HOME to your path. For example /opt/openjdk11/bin to your PATH.

# Windows update PATH permanently
setx PATH=C:\opt\openjdk11\bin;%PATH%

# Linux
export PATH=/opt/openjdk11/bin:$PATH

Get TomCat

First, download TomCat from http://tomcat.apache.org/. Extract the file after downloading. If you are using an IDE for development like NetBeans or IntelliJ, you just need to configure your IDE to use the TomCat directory and it will take care of starting and stopping the service as well as deploying your app.

There are two important environment variables to set before running:

  • Set CATALINA_HOME to the root of your TomCat directory.
  • Set JAVA_HOME to the root directory of your Java JDK or JRE.

There are a few ways to use TomCat:

  • Manually starting and stopping TomCat process using startup.sh and shutdown.sh
  • Configuring your IDE to manage the TomCat instance for you (for development)
  • Set up TomCat as a system service (for production)
  • Embedding TomCat in your application (not covered here)

Important directories inside the TomCat directory:

  • conf/ — Contains configuration files
  • logs/ — Log files for web apps
  • webapps/ — Directory to store web apps directories and .war files
  • webapps/ROOT/ — The default web root

Install TomCat as a Windows service

To install TomCat as a Windows service, use the «Windows Service Installer» downloadable from https://tomcat.apache.org/index.html. Go to the «Download» page for the version you want and look for the download named 32-bit/64-bit Windows Service Installer. After installation, you will find it in the «Services» manager in Windows. You can start and stop it from there.

To learn more about Windows services, check the official documentation. Here is a quick link to
version 9 Windows service HOW-TO.

Setup TomCat as a Linux Service (systemd)

Setting up TomCat as a service allows it to run in the background and be configured to run at boot time. If you want a TomCat server to run 24/7 and restart automatically, you want to set it up as a service. Most Linux distributions these days use systemd and that is what’s covered here. This assumes you already have Java and TomCat downloaded and extracted. In this example, we assume TomCat was moved to the /opt/ directory.

Prepare low privilege user and directory permissions

This is optional, but recommended. I don’t recommend running the service with the root user. I also recommend against usign your personal user, unless it is your own server for personal use and development only. I do recommend creating a low privilege user named «tomcat» to run the service.

# Create low priv tomcat user
sudo useradd tomcat

# Let scripts be executable (i.e. startup/shutdown scripts)
sudo chmod +x /opt/tomcat9/bin/*.sh

# Reset permissions on tomcat directory
sudo chmod -R 775 /opt/tomcat/webapps
sudo chown -R tomcat:tomcat /opt/tomcat9

# Optionally add your user to the tomcat group
# to write in the tomcat directory
sudo gpasswd -a myusername tomcat

Example systemd service file

This is an example of a systemd service file. For example, you can put this file in /etc/systemd/system/tomcat.service and then it will create a service named «tomcat».

# Example systemd file
[Unit]
Description=Apache Tomcat
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/opt/openjdk11
Environment=CATALINA_PID=/opt/tomcat9/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat9
Environment=CATALINA_BASE=/opt/tomcat9
Environment='CATALINA_OPTS=-Xmx1024M -server'

WorkingDirectory=/opt/tomcat9

ExecStart=/opt/tomcat9/bin/startup.sh
ExecStop=/opt/tomcat9/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
RestartSec=15
Restart=always

[Install]
WantedBy=multi-user.target

Control the systemd service

Control the service and get information using the commands below. Make sure you run the enable command so that it will start automatically on reboot.

sudo systemctl status tomcat
sudo systemctl enable tomcat # Sets it to start on boot
sudo systemctl start tomcat
sudo systemctl stop tomcat
sudo systemctl restart tomcat

Use nginx as a reverse proxy

In some situations, you can use TomCat to listen publicly on port 80 or 443, but in many cases you need to run TomCat on a server with other web services running. In those situations, it can be useful to set up a reverse proxy to listen publicly and handle routing traffic to internal web services like TomCat. I recommend nginx and I have an Nginx Tutorial if you want to learn how to do that.

Basic usage

This section covers some common tasks like starting and stopping the server, serving static files and JSPs, and modifying common settings.

Start and stop TomCat manually

In the bin\ directory, there will be a startup and shutdown script. A .sh is provided for Linux and .bat for Windows. Run the script, setting the environment variables JAVA_HOME and CATALINA_HOME if needed.

Windows example:

set JAVA_HOME=C:\opt\openjdk11
set CATALINA_HOME=C:\opt\tomcat9

C:\opt\tomcat9\bin\startup.bat
C:\opt\tomcat9\bin\shutdown.bat

Linux example:

export JAVA_HOME=/opt/openjdk11
export CATALINA_HOME=/opt/tomcat9

# Might need sudo for these
/opt/tomcat9/bin/startup.sh
/opt/tomcat9/bin/shutdown.sh

Verify TomCat server is running

Visit http://localhost:8080 to see if TomCat is working. You might need to replace localhost with the IP address of your server if it is a remote test. If you installed manually, the default port is 8080. If you used apt-get or the Windows installer, it should default to port 80. We’ll cover how to change the port later in this section about modifying settings.

Serve static files

TomCat can handle serving static files out of the box. By default there is a webapps/ROOT/ directory in the TomCat’s webapps/ directory. There is already an index.jsp and some other resources inside. That is the landing page you hit when you visited the TomCat server in the browser in the previous section at http://<ip>:8080

You can add more static files to the webapps/ROOT/ directory, You can create a new directory inside webapps/ directory and place static files there. accessible at /<directory_name> (e.g. http://localhost:8080/mywebapp).

You can put flat files like .txt, .zip or .html and they will be served directly. You can also create subdirectories.

Serve Java Server Pages (JSP)

Serving .jsp pages is just as easy as serving static files. You just drop in JSP files in a webapp directory like ROOT and point your client at it. There are no XML configurations or WAR files needed. Just point your browser to /yourjsp.jsp and it will run. TomCat will actually compile them and process them dynamically before returning the response to the client. They are treated like PHP scripts in that sense. They can be treated like HTML files with dynamic Java code inserted at various places. There is no need to restart the server or recompile code when making updates to .jsp pages.

Here’s one simple example of a JSP if you want to test. It shows how to print out text, create new objects, and get headers from the request.

<!-- hello.jsp -->
<html>
    <body>
        <p><% out.print("Hello, world!"); %></p>
        <p>8 x 8 = <%= 8 * 8 %></p>
        <p>Time: <%= new java.util.Date() %></p>
        <p>Host requested: <%= request.getHeader("Host") %></p>
    </body>
</html>

Name it hello.jsp and drop it in the webapps/ROOT directory, and then visit https://localhost:8080/hello.jsp. Or, create a directory like hello/ inside webapps and put the file in there, then visit http://localhost:8080/hello/hello.jsp.

Modify settings

TomCat configuration files live in the conf/ directory of the root TomCat directory. The main file is server.xml and that is where you would change the listen port. Inside server.xml the main listening port is defined in a section labeled Connector. The section will look something like this:

<!-- A "Connector" represents an endpoint by which requests are received
    and responses are returned. Documentation at :
    Java HTTP Connector: /docs/config/http.html
    Java AJP  Connector: /docs/config/ajp.html
    APR (HTTP/AJP) Connector: /docs/apr.html
    Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
        connectionTimeout="20000"
        redirectPort="8443" >

Deploy web apps

Deploying web apps can be done in a number of ways which are listed below.

  • Manually copying the web app directory to the TomCat’s webapps/ directory
  • Placing a .war file in the webapps/ directory
  • Using Maven plugin to deploy
  • Using the web app named manager that comes packaged with TomCat

The TomCat manager web app is described further down in the manage web apps section.

Use the admin web interface

There are two web apps that come bundled with TomCat to help with administration.

One is the Virtual Host Manager which lets you listen for multiple different domains. This can be accessed at /host-manager/html and requires certain roles to access (explained below).

The other web app that comes bundled with TomCat is the Web Application Manager. This one is used more often and is more relevant to beginners. This one can be accessed at /manager/html. This one lists al the web apps that are available and which ones are running. You can start, start, deploy, undeploy, and expire sessions. It is quite useful.

Create a TomCat admin user

You can add users to TomCat in the conf/tomcat-users.xml file. There will be a section in the xml labeled tomcat-users. Inside of that there will be several commented out examples showing how to create a user. If you want to grant all roles that allow access to the admin manager app, you can add the lines of code below, changing the username and password.

<!-- Inside <CATALINA_HOME>/conf/tomcat-users.xml -->
<!-- Inside the <tomcat-users> element -->

<!-- Roles for Web Application Manager /manager/html/ -->
<role rolename="manager-gui" />
<role rolename="manager-script" />
<role rolename="manager-jmx" />
<role rolename="manager-status" />
<!-- Role for Virtual Host Manager /host-manager/html/ -->
<role rolename="admin-gui" />

<!-- Create user and assign roles -->
<user username="dano" password="secret" roles="manager-script,manager-gui,manager-jmx,manager-status,admin-gui" />

Note you will need to restart the server. If you installed it as a service, use your service manager to restart TomCat («Services» in Windows; «sudo systemctl restart tomcat» in Ubuntu). If you installed it manually refer to the start and stop TomCat manually section above. Take time to explore the http://localhost:8080/manager/html and http://localhost:8080/host-manager/html links. If you are accessing it remotely and not via localhost, you will get errors. The next section explains how to whitelist your IP address.

Access admin web apps

To access the admin pages, visit the Web Application Manager at /manager/html (e.g. http://localhost:8080/manager/html) and view the Virtual Host Manager at /host-manager/html (e.g. http://localhost:8080/host-manager/html). The Web Application Manager, by default is only accessible on localhost. If you need to access the admin interface from a remote host, you need to whitelist your IP address on the server. You can do this by editing the META-INF/context.xml file inside the specific web app directory.

You will log in to these using the tomcat user and password created in the previous section in tomcat/conf/tomcat-users.xml.

To whitelist your IP, modify the file tomcat/webapps/manager/META-INF/context.xml and append your IP to the list of whitelisted IP addresses, separating your IP with the | separator (see the example below). Do the same for tomcat/webapps/host-manager/META-INF/context.xml

Find the line that looks like this in the <webapp>/META-INF/context.xml file:

&lt;Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

Add your IP address to the list. We’ll pretend it’s 8.8.8.8. Change the allow property to include your IP address at the end, appended after a pipe | like this:

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|8.8.8.8"

Manage web apps

Following the previous sections, you should already have access to the Web Application Manager, available at the /manager/html URL. This page will list the web apps that are available and running. You can start, stop, and undeploy apps from this page. You can also deploy web apps as packaged .war files or as exploded directories. If you are accessing the server remotely, the .war file is the choice you want.

Learn more about the Web Application Manager in the official documentation.

Conclusion

TomCat is a servlet container that has been around and proven its value. It’s a solid choice for hosting JSP and servlet applications. After reading this you should feel comfortable setting up TomCat for basic usage to get started. This guide was only a quick overview of some common tasks. There are many more capabilities that TomCat has. Check out http://tomcat.apache.org/ for the latest and most accurate information.

References

  • TomCat website
  • TomCat Manager web app documentation
  • Nginx Tutorial
  • Install multiple JDK in Windows for Java Development
  • TomCat Windows service documentation
  • Eclipse Jetty website
  • TomCatEE website
  • Glassfish website

Reading Time: 6 minutes

Organizations running Apache Tomcat in production on Windows often want to run Tomcat as a Windows service. This removes the need for someone to be actively logged into the server and provides an easy way to integrate with Windows management tools. In this blog, I will explain the easiest way to run Tomcat as a Windows service and how you can do this for multiple instances as well.

Running an instance of Apache Tomcat as a Windows service is not complicated, if you download the correct distribution of Tomcat (Windows service Installer). However, running multiple instances of Tomcat as Windows services is a more complicated process. To avoid issues, you would have to:

1. Uninstall the service that the installer has installed ( if you used the service installer)

2. Run the service.bat command and give it an unique name ( so, next service install would not fail )

service.bat install MyTomcat2 ( you have to download the zip distribution to get service.bat )

2. For each instance, edit server.xml and manually modify all ports to unique non-default numbers

3. Go to Service Control Manager by running ‘services’ from Start menu and change the startup type for each instance to be “Automatic”

You would have to repeat this process for each instance that you want to install, which can get tedious and potentially quite error-prone.

The Tcat Server installer provides a much better experience by enabling you to select a name for the service and also by enabling you to install multiple Tomcat instances on the same box. All you have to do is to run a standard install of Tcat Server on Windows, and it will automatically install Apache Tomcat as a Windows service. It can detect name conflicts and pick unique service names for the Windows services. (The installer also detects port conflicts, so you don’t run into start-up issues due to port conflicts).

Use Tcat Installer to install Tomcat as a Windows service

Once you have installed Tomcat as a service, you might want to modify the JVM start parameters. With Tcat Server, you can simply run ‘tcatserverw.exe’ from your bin folder. Go to the “Java” tab and enter the start parameter values (for ex: increase memory size).

service options - blog

Thats it! You can repeat this process for as many Tomcat instances as you want to run as Windows services.

Displaying console messages in Windows

While running Tomcat as a Windows service works great, at times, you may want to read the console messages to debug problems. By default, Windows services do not display the console messages. However, you can easily enable the service to display the console messages. Go to Start menu and type ‘services’ to open the Service Control Manager. Once in there, select ‘TcatServer6’ ( or whatever name you chose during install) and double click on it. You will get the service options that include whether you want the service to start automatically or manually. Click on ‘LogOn’ tab. Select ‘Allow Service to Interact with desktop’ option. (This only works with the System account — if you using a special account to run Tomcat service, you will not have this option).

service - interact - blog

From now on, any time you receive a console message, you will get a dialog as shown below. Clicking on ‘View the message’ will show you the console window.

service - desktop - blog

Tcat Server is free for use in development – download your copy here.



Modified on: Thu, 14 Jul, 2022 at 4:25 PM


How to run Apache Tomcat as a Windows Service with FireDaemon Pro

How to run Apache Tomcat as a Windows Service with FireDaemon Pro

Apache Tomcat is an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and WebSocket technologies.

Apache Tomcat can be run as a Windows service using FireDaemon Pro. This allows you to start and run the application automatically when Windows boots and before login. FireDaemon Pro also allows you to start multiple instances of the server, monitoring them and automatically restarting those instances in the event they crash. FireDaemon Fusion can also be used to manage your Apache Tomcat service, plus other Windows services via your web browser.

Apache Tomcat Setup Under FireDaemon Pro

Please follow the steps below to set up an Apache Tomcat as a Windows service with FireDaemon Pro

Step 1: Download and Install Java

Java 8 is supported until 2030 so we recommended you use this version. Oracle changed their download policy for Java and to download it, you need to make a free Oracle account and login. There are 2 types of java you can install:

  1. Java Server (JRE): This has performance optimizations and you can download the latest Java 8 Windows x64 Server JRE from the java website at the bottom of the page. Extract the contents of the zip to your hard drive, for the purposes of this guide we will use «C:\jdk1.8.0_271». The folder name will be different if you’re using a different Java version. This guide assumes you’re using this version.
  2. Java SE: This has performance optimizations and you can download the latest Java 8 Windows x64 Jave SE from the java website at the bottom of the page. Extract the contents of the zip to your hard drive, if you use this version extract to «C:\jre1.8.0_271». The folder name will be different if you’re using a different Java version.

Step 2: Download and Install Apache Tomcat

  1. Download from here. Make sure you get the latest (stable) binary zip release.
  2. Extract the zip someplace on your hard drive. For the purposes of this guide we will use: «C:\apache-tomcat-9.0.39». The folder name will be different if you’re using a different version.

Step 3: Set Up Apache Tomcat as a FireDaemon Pro Service

Download and install FireDaemon Pro. Double click the FireDaemon Pro icon on your desktop, then click on the New (i.e. +) button in the toolbar (or type Ctrl + N) to create a new service. Enter the information into the fields as you see below. Adjust the path names to suit your installation. Note the required parameters.

Apache Tomcat Dedicated Server Service Program Tab: Run as Windows Service

The most important field on the tab is the Parameters. The Parameters define the initial setup of your server. The full parameter list should be as follows:

-Djava.util.logging.config.file="%LOG_CONFIG%" -Djava.util.logging.manager=%LOG_MANAGER%  -Djdk.tls.ephemeralDHKeySize=%KEY_SIZE% -Djava.protocol.handler.pkgs=%PROTOCOL_HANDLER%   -Dignore.endorsed.dirs="%ENDORSED_DIRS%" -classpath "%CLASS_PATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMP%" org.apache.catalina.startup.Bootstrap start

The above commands need to be added in their entirety. They allow Tomcat to run as a service.

Step 4: Next, click on the FireDaemon Pro Settings tab:

Apache Tomcat service settings: Run as Windows Service

Enter appropriate values in the fields on the Settings tab as follows:

  • (Required) Job Type: You must set job type to «Global Job».
  • (Required) Logon Account: Enter the Windows account under which the application is to be run, e.g. the current administrator account.
  • Password / Confirm Password:  Enter the logon account’s password twice.
  • (Optional) Interact with Desktop: If this setting is enabled (default), the application’s output messages can be seen in the console window on Windows Session 0. To switch to Session 0, it may be necessary to install FireDaemon Zero and ZeroInput. Alternatively, this setting may be disabled, in which case the application’s messages will be hidden.
  • (Optional) Priority: To allocate more CPU time to the application, select a higher scheduling priority in this field.
  • (Optional) CPU Bindings: To run the application on a specific CPU, specify the appropriate CPU in this field.

Step 5: Next, click on the Lifecycle tab. 

Set Shutdown By to Forceful Termination and check «Console Program».

Apache Tomcat lifecycle settings: Run as Windows Service

Step 6: Now click on the Environment tab. 

This is where you enter environment variables specific to your Tomcat instance. The names and values of the environment variables are below; adjust your paths to suit your installation. Adjust the paths below to the version of Tomcat you have installed. Note that environment variable order is important:

Service Definition: Run as Windows Service

TOMCAT_JAVA_HOME: C:\jre1.8.0_271
LOG_CONFIG: C:\apache-tomcat-9.0.39\conf\logging.properties
LOG_MANAGER: org.apache.juli.ClassLoaderLogManager
KEY_SIZE: 2048
PROTOCOL_HANDLER: org.apache.catalina.webresources
ENDORSED_DIRS
CLASS_PATH: C:\apache-tomcat-9.0.39\bin\bootstrap.jar;C:\apache-tomcat-9.0.39\bin\tomcat-juli.jar
CATALINA_BASE: C:\apache-tomcat-9.0.39
CATALINA_HOME: C:\apache-tomcat-9.0.39
CATALINA_TMP: C:\apache-tomcat-9.0.39\temp

Step 7: Lastly, click the Save and Close (ie. the tick) button on the toolbar. 

If the service’s Startup Type is set to Automatic or Automatic (Delayed-Start), it will start immediately.

FireDaemon Pro Services list: Run as Windows Service

If the service’s Interact with Desktop setting is enabled, the Apache Tomcat status can also be verified by switching to Windows Session 0 to view the Apache Tomcat’s messages.

Apache Tomcat messages window: Run as Windows Service

Point your browser at the Tomcat instance to ensure it is up and running. If you have issues, refer to the FireDaemon debug log file, Windows Event Viewer and the Tomcat log files. Alternately, run the FireDaemon service interactively and turn off logging to Stderr/Stdout so you can see what is happening when the Tomcat instance starts.

Apache Tomcat Local Instance Webpage: Run as Windows Service

**********************************************************************************
How to run Apache Tomcat as a Windows service for 24×7 availability
How to monitor and restart Apache Tomcat if it crashes, hangs or shuts down
How to start Apache Tomcat at system boot, without any user intervention
How to schedule Apache Tomcat to start/restart at specific times and dates
**********************************************************************************


Did you find it helpful?
Yes

No

Send feedback

Sorry we couldn’t be helpful. Help us improve this article with your feedback.

Related Articles

    If you want to install Tomcat on Windows system as a service, you’ll get the recommendation to use the 32-/64-Bit Windows Service Installer. If you want to install the Tomcat manually, it’s fine. But you can’t use this installer for an automatic installation and configuration of Tomcat, because the installer is UI-based. The next sections explain how you can install and configure Tomcat on a CMD.

    Tomcat Installation

    1. Download from the Apache Tomcat 8.5 download page the Core 64-bit Windows zip (or the 32-Bit zip).
    2. Unzip it (for example to C:\tomcat\)

    That’s it. Now we have a ready-to-use Tomcat with default configuration values. But it isn’t install as a service.

    Installation and Configuration As Windows Service

    1. Go to the bin folder in the installation folder of Tomcat (in the example  it’s C:\tomcat\apache-tomcat-8.5.11\bin)
    2. Install Tomcat as service named tomcat8 by calling service.bat install <servicename>
    1C:\tomcat\apache-tomcat-8.5.11\bin>service.bat install tomcat8
    
    1.  tomcat8.exe //US//<servicename> followed by configuration parameter configures the Tomcat service. For example:
    1C:\tomcat\apache-tomcat-8.5.11\bin>tomcat8.exe //US//tomcat8 --Startup=auto --JavaHome="C:\Program Files\Java\jre1.8.0_112" --JvmMs=2048 --JvmMx=4096 ++JvmOptions=-Dkey=value
    
    1. Start the Tomcat service with net start <servicename>
    1. You can check on http://localhost:8080 whether Tomcat is installed correctly.

    The configuration example (step 3) shows how to configure the JVM (heap space, Java option etc.), where Java is installed and which start type should use for the service. The full list of the possible configuration parameter for the Tomcat service can be found in the Apache Tomcat’s Windows Service documentation. Now we have everything together for writing a Powershell script that does these steps automatically.

    Понравилась статья? Поделить с друзьями:
    0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии
  • Cm6206 driver windows 10
  • Загрузочная флешка windows 10 ultraiso инструкция
  • Поиск дублей файлов windows
  • Как посмотреть параметры ноутбука на windows 10 самсунг
  • Как включить режим питания максимальная производительность в windows 10