How to install Apache Tomcat on Ubuntu 18.04

This software (Apache Tomcat) was developed in an open environment. It was released under the Apache License version 2. The Apache Tomcat project is designed to work together the best developers from around the world.
This guide will help you how to install this software on a server with the Ubuntu 18.04 operating system.

System Update and Java Installation

First you need to update your system, making sure that Ubuntu version 18.04 is installed

sudo apt-get update 

Install Java

sudo apt-get install default-jdk 

Update JAVA_HOME variable

First, you need to find the JAVA_HOME directory

update-alternatives --config java 

Copy the directory, and then enter the following:

nano /etc/environment 

This will give you the opportunity to open a file containing environment variables

Add JAVA_HOME

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java"

Save and close the file, then reload the environment variables for them to take effect

source /etc/environment

After checking the accuracy of your work and that everything is correct, you can use the following command:

echo $JAVA_HOME 

Create a user to run the Tomcat web server

This user (special for this tutorial) will accept the name ‘tomcat’, but you can use any name for this user

groupadd tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Be sure to find out which version of Tomcat you will install. This is in this link, you can familiarize yourself with it

http://tomcat.apache.org/

Remember to choose to download with the .gz extension

Next, create a directory for Tomcat files

mkdir /opt/tomcat 

Download Tomcat

cd 
wget your-tomcat-link 

Unzip the files to the Tomcat folder

tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1

Also, you can press the “Tab” key after entering apache, since you only have one download in your folder. Make sure the folders have the correct permissions

cd /opt/
sudo chown -R tomcat tomcat/

JAVA_HOME Display the path again so you can copy it

nano /etc/systemd/system/tomcat.service 

Tomcat setup

In order to configure Tomcat you need to Copy and paste the settings below into the tomcat.service file that you just created. Edit the JAVA_HOME path to the one you copied. After that, save and close the file

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

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

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

[Install]
WantedBy=multi-user.target

Reboot the SystemD daemon so that the added service settings are enabled

sudo systemctl daemon-reload

Now you can start Tomcat service

sudo systemctl start tomcat 

If you want to check the status of your Tomcat server, need to add this сommand:

sudo systemctl status tomcat 

You can also see your Tomcat service page by clicking on the link:

http://your_ip_address:8080

Was this article helpful?

Related Articles

Leave A Comment?