Apache Tomcat

How Do I Start and Stop Apache Tomcat on Linux?

Apache Tomcat is a free, open-source web server and servlet container to implement the Java Servlet and the JavaServer Page specifications developed by the Apache Software Foundation to provide an HTTP server for Java Applications.

This tutorial will discuss two ways to start or stop the Apache Tomcat service on your Linux machine.

Method 1 – Startup Scripts

Apache Tomcat comes with startup scripts that you can use to start or stop the service. This method applies when you have an Apache Tomcat server installed as a binary release using a .zip or .tar archive.

If so, start by navigating to the bin directory of the Apache Tomcat as:

cd apache-tomcat-10.0.10/bin

Inside the bin directory, you can view all the scripts to manage the service using the ls command:

ls -la

To start the Apache Tomcat service using its startup script, run the script as:

./startup.sh

NOTE: Ensure you have executed permissions for the scripts in this directory.

Once you execute the startup script, you should see an output indicating whether the Tomcat service is up.

Using CATALINA_BASE:   /home/debian/apache-tomcat-10.0.10

Using CATALINA_HOME:   /home/debian/apache-tomcat-10.0.10

Using CATALINA_TMPDIR: /home/debian/apache-tomcat-10.0.10/temp

Using JRE_HOME:        /usr

Using CLASSPATH:       /home/debian/apache-tomcat-

10.0.10/bin/bootstrap.jar:/home/debian/apache-tomcat-10.0.10/bin/tomcat-juli.jar

Using CATALINA_OPTS:

Tomcat started.

By default, the Apache Tomcat service runs on port 8080; you can verify if it’s running using the lsof utility as:

sudo lsof -i -P -n

The above command should show port 8080 as LISTEN:

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

java    1562 debian   41u  IPv6   1598      0t0  TCP *:8080 (LISTEN)

java    1562 debian   52u  IPv6  19670      0t0  TCP 127.0.0.1:8005 (LISTEN)

To stop the Apache Tomcat service, run the shutdown script as:

./shutdown.sh

You should get an example output as shown below:

Using CATALINA_BASE:   /home/debian/apache-tomcat-10.0.10

Using CATALINA_HOME:   /home/debian/apache-tomcat-10.0.10

Using CATALINA_TMPDIR: /home/debian/apache-tomcat-10.0.10/temp

Using JRE_HOME:        /usr

Using CLASSPATH:       /home/debian/apache-tomcat-

10.0.10/bin/bootstrap.jar:/home/debian/apache-tomcat-10.0.10/bin/tomcat-juli.jar

Using CATALINA_OPTS:

---OUTPUT-TRUNCATED-----------------------

Adding to PATH

Although starting up Apache Tomcat using its startup scripts is easy, you must specify the full path or be in the bin directory.

To resolve this, you can add the directory to the $PATH variable using the command:

export PATH="/home/debian/apache-tomcat-10.0.10/bin/:$PATH"

Once you have the apache tomcat directory in your path, you can run the scripts from any location without specifying the full path.

You can also use the catalina.sh script to start and stop the apache service.

For example:

catalina.sh start

catalina.sh stop

To start and stop the apache service respectively.

Method 2 – Using Systemctl

Another way to manage the Apache Tomcat service is to use systemctl. This method will work if Apache Tomcat is installed from the system repositories such as APT and RPM.

To start the tomcat service, use the command:

sudo systemctl start tomcat9.service

To stop the service, enter the command:

sudo systemctl stop tomcat9.service

Using Custom Unit File

You will notice that the above commands only work if you have the Apache Tomcat server installed from the system packages.

If you have Tomcat installed manually from a zip or tar package, you can create a custom unit file to manage the service using the systemd.

The following is an example tomcat.service file.

sudo touch /etc/systemd/system/tomcat.service

sudo vim /etc/systemd/system/tomcat.service

Enter the unit file contents as:

[Unit]

Description="Apache Tomcat"

After=network.target

Service]

Type=forking


User=debian

Group=debian


Environment="JAVA_HOME=/usr/lib/jvm/java-11-amazon-corretto/"

Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

Environment="CATALINA_BASE=/home/debian/apache-tomcat-10.0.10"

Environment="CATALINA_HOME=/home/debian/apache-tomcat-10.0.10"

Environment="CATALINA_PID=/home/debian/apache-tomcat-10.0.10/temp/tomcat.pid"

Environment="CATALINA_OPTS="


ExecStart=/home/debian/apache-tomcat-10.0.10/bin/startup.sh

ExecStop=/home/debian/apache-tomcat-10.0.10/bin/shutdown.sh


[Install]

WantedBy=multi-user.target

Save the file and close.

Next, reload the system daemon to load the new unit file as:

sudo systemctl daemon-reload

Finally, manage the tomcat service using the system as:

sudo systemctl start tomcat.service

sudo systemctl stop tomcat.service

Using the methods discussed above, you can now manage the Apache Tomcat service easily.

Conclusion

This article has covered two ways to start and stop the Apache Tomcat service depending on various installation methods.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list