Install Apache Tomcat 9 on Ubuntu 20.04[Step by Step]

0
Install Apache Tomcat 9 on Ubuntu

Apache Tomcat is an open-source technology based on Apache. It is used to deploy and server Java Server Pages(JSP), Java Expression Language, Jave WebSocket technologies. In this article, I am going to share a step-by-step guide to install Apache Tomcat 9 on Ubuntu 20.04 or other old versions such as 18.04 and 16.04.

So let’s start

How to Install Apache Tomcat 9 on Ubuntu

Requirement

  • Ubuntu system
  • Logged in user with Sudo privileges.

Step 1: Java Installation

Check your system if Java is already installed or not by following command.

$ java -version

Tomcat requires Java to be installed on the server so that any Java web application code can be executed. Install OpenJDK by running the following command.

$ sudo apt install default-jre

Now, Java is installed on your machine. To make sure it is installed perfectly, check Java version.

$ java -version

Upon successful installation, it should show latest java version.

Check Java Version

Step 2: Create Tomcat User

Create a new user and group that will run the Tomcat service. First, create a new group “tomcat”.

$ sudo groupadd tomcat

Next, create a new tomcat user and add the user to the group “tomcat”.

$ sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Now, you can download and install Tomcat.

Step 3: Install Tomcat

Best way to install Apache Tomcat 9 on Ubuntu by downloading the latest release and configure it manually. You can download the latest version from tomcat’s official site. To download the latest version, navigate to Binary Distributions section, then under the Core list, copy the link from the “tar.gz”.

Now, change the directory to /tmp

$ cd /tmp

Now using CURL, download the Tomcat from the official website as mentioned above.

$ curl -O https://downloads.apache.org/tomcat/tomcat-8/v8.5.66/bin/apache-tomcat-8.5.66.tar.gz

Let’s install Tomcat under /opt/tomcat directory. Create a directory, then extract the archive to it.

$ sudo mkdir /opt/tomcat
$ sudo tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1

Now, setup the proper user permissions for installation.

Step 4: Update Permissions

Set permission for newly created Tomcat user. Change the directory where we already unpacked the Tomcat installation.

$ cd /opt/tomcat

Now, set group ownership to the entire directory.

$ sudo chgrp -R tomcat /opt/tomcat
$ sudo chmod -R g+r conf
$ sudo chmod g+x conf

Make the tomcat user the owner of the webapps, work, temp and log directories.

$ sudo chown -R tomcat webapps/ work/ temp/ logs/

Step 5: Create a systemd Unit File for Install Apache Tomcat 9

To run Tomcat as a service you need to create a new unit file.

Tomcat needs to know where Java is installed. To know Java installation location run following command.

$ sudo update-java-alternatives -l

Output should look like

java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd6

The highlighted part is JAVA Home

/usr/lib/jvm/java-1.11.0-openjdk-amd6

Now, create systemd service file. Open a file called tomcat.service from the directory /etc/systemd/system.

Use VI editor to edit the file and modify the JAVA Home by your JAVA Home location.

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

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd6
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

After adding the JAVA_Home, save the file followed by reload the systemd daemon so that it knows about our service file

$ sudo systemctl daemon-reload

Start the Tomcat service

$ sudo systemctl start tomcat

Step 6: Firewall Configuration and Test the Tomcat Server

Now allow port 8080 to serve the browser request.

$ sudo ufw allow 8080

Now open the link with port in the broswer.

Open in web browser
http://server_domain_or_IP:8080

Conclusion

In this tutorial, you have learned how to install Apache Tomcat 9 on Ubuntu. If you like this tutorial please share this on your social media.

LEAVE A REPLY

Please enter your comment!
Please enter your name here