How to run Apache Tomcat on Mac and Linux

Last updated : Jul 30, 2023 12:00 AM

This article describes how to download and run the Apache Tomcat server as a standalone server. These instructions are applicable for Mac and Linux environments.

1. Download the Tomcat server from the Apache Tomcat website.

2. Extract the Tomcat server to a folder where you intend it to deploy. This tutorial uses ApacheTomcat folder to contain the Tomcat folder. The path is /Users/learnbestcoding/ApacheTomcat/apache-tomcat-10.0.20

3. To run the server, navigate to /Users/learnbestcoding/ApacheTomcat/apache-tomcat-10.0.20/bin
and run ./startup.sh

No JAVA_HOME set

If your environment has no JAVA_HOME set, you will encounter some errors related to Java Runtime.
The operation couldn't be completed. Unable to locate a Java Runtime.
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program

That means you don't have the JAVA_HOME set.

How to set JAVA_HOME

1. Run which java to find the Java installation path in your system
In my system, which java returns /opt/homebrew/opt/openjdk/bin/java. Yours may differ. Change this path according to your system. Ignore the /bin/java from this path. We need the path leads to the /bin folder.

2. Set the JAVA_HOME by running export JAVA_HOME=/opt/homebrew/opt/openjdk

3. Confirm the setting by running echo $JAVA_HOME

4.Now go to /Users/learnbestcoding/ApacheTomcat/apache-tomcat-10.0.20/bin and run
./startup.sh

You should be able to access the Tomcat home page using http://localhost:8080

How to stop Tomcat Server

Go to /Users/learnbestcoding/ApacheTomcat/apache-tomcat-10.0.20/bin and run ./shutdown.sh

How to change the default Tomcat port

The server runs on the default port 8080. To change that
1. Go to /Users/learnbestcoding/ApacheTomcat/apache-tomcat-10.0.20/conf
2. Edit server.xml

Locate

server.xmlDescription
<Connector port="8080" protocol="HTTP/1.1" 	
	connectionTimeout="20000"
        redirectPort="8443" />
and change 8080 to the port that you want the server to run.

3. Re-start the server

Lance

By: Lance

Hi, I'm Lance Raney, a dedicated Fullstack Developer based in Oklahoma with over 15 years of exp

Read more...