How to Install Apache Tomcat on Ubuntu: Step-by-Step Guide

Apache Tomcat is an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language, and Java WebSocket technologies. Installing Apache Tomcat on Ubuntu is a straightforward process that allows you to run Java web applications. This guide will walk you through the steps to install Apache Tomcat on Ubuntu.

Prerequisites

Before you begin, ensure that you have the following:

  • A server running Ubuntu.
  • A non-root user with sudo privileges.
  • Java Development Kit (JDK) installed.

Step 1: Update Your Package Repository

First, update your package repository to ensure you have the latest package information.

sudo apt update
sudo apt upgrade

Step 2: Install Java

Apache Tomcat requires Java to run. Install the default JDK package.

sudo apt install default-jdk

Verify the installation by checking the Java version.

java -version

Step 3: Create a Tomcat User

For security reasons, it’s best to run Tomcat as a non-root user. Create a new user and group for Tomcat.

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

Step 4: Download Tomcat

Visit the Apache Tomcat download page and get the latest version. Alternatively, you can use wget to download it directly.

cd /tmp
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz

Step 5: Install Tomcat

Extract the downloaded file and move it to the /opt/tomcat directory.

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

Step 6: Update Permissions

Change the ownership of the Tomcat directory to the tomcat user and group.

sudo chown -R tomcat: /opt/tomcat
sudo chmod -R 755 /opt/tomcat

Step 7: Create a Systemd Service File

Create a new service file for Tomcat to manage it with systemd.

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

Add the following content to the file:

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

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
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

[Install]
WantedBy=multi-user.target

Step 8: Reload Systemd and Start Tomcat

Reload the systemd daemon and start the Tomcat service.

sudo systemctl daemon-reload
sudo systemctl start tomcat

Enable Tomcat to start on boot.

sudo systemctl enable tomcat

Step 9: Configure Tomcat Web Management Interface

To access the web management interface, configure the tomcat-users.xml file.

sudo nano /opt/tomcat/conf/tomcat-users.xml

Add the following lines inside the <tomcat-users> tag.

<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="password" roles="manager-gui,admin-gui"/>

Conclusion

You have successfully installed Apache Tomcat on Ubuntu. You can access the Tomcat web interface by navigating to http://your_server_ip:8080 in your web browser. From here, you can deploy and manage your Java web applications.

Leave a Comment