How to Install Tomcat on Ubuntu 18.04
This tutorial will teach you how to install Tomcat 9 on Ubuntu 18.04 and how to configure it for use. The same tutorial applies to Ubuntu 16.04 and other Ubuntu-based distributions.
Before you begin with the guide, make sure you have a non-root user with sudo privileges set up on your server. Remember to access your server using SSH.
Step 1: Install Java
Before we install Tomcat on Ubuntu, we need to install Java to execute the Java web application code. OpenJDK is the default Java development in Ubuntu 18.04. Installing Java is simple and quick. Just follow the commands below:
sudo apt update
Install the OpenJDK package by running:
sudo apt install default-jdk
Now that JDK is installed on your system, you can create the 'tomcat' user by following the next step.
Step 2: Create Tomcat User
For security reasons, you should not use Tomcat without a dedicated user. This will make the installation of Tomcat on Ubuntu easier. Create a new 'tomcat' group that will run the service:
sudo groupadd tomcat
Next, create a new 'tomcat' user. Create user members of the 'tomcat' group with a home directory '/opt/tomcat' for running the Tomcat service:
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Step 3: Install Tomcat on Ubuntu
The best way to install Tomcat 9 on Ubuntu is to download the latest binary release from the Tomcat 9 downloads page and configure it manually. If the version is not 9.0.60 or it’s the latest version, then follow the latest stable version. Just copy the link of the core tar.gz file under the Binary Distributions section.
Now, change to the /tmp directory on your server to download the items which you won’t need after extracting the Tomcat contents:
cd /tmp
To download from the copied link (from Tomcat website), use the following curl command:
curl -O https://dlcdn.apache.org/tomcat/tomcat-9....63.tar.gz
Step 4: Update Permissions
Now that you've finished installing Tomcat on Ubuntu, you need to set up the 'tomcat' user to have full access to the Tomcat installation. This user needs to have access to the directory. Follow the steps below:
sudo mkdir /opt/tomcat
cd /opt/tomcat
sudo tar xzvf /tmp/apache-tomcat-9.0.*tar.gz -C /opt/tomcat --strip-components=1
Now, give the 'tomcat' group ownership over the entire installation directory with the chgrp command:
sudo chgrp -R tomcat /opt/tomcat
Next, give the 'tomcat' user access to the 'conf' directory to view its contents and execute access to the directory itself:
sudo chmod -R g+r conf
sudo chmod g+x conf
Make the 'tomcat' user the owner of the 'webapps', 'work', 'temp', and 'logs' directories:
sudo chown -R tomcat webapps/ work/ temp/ logs/
Step 5: Create a systemd Unit File
We will need to create a new unit file to run Tomcat as a service. Open your text editor and create a file named 'tomcat.service' in the '/etc/systemd/system/' directory:
sudo nano /etc/systemd/system/tomcat.service
Next, paste the following configuration:
[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.awt.headless=true -Djava.security.egd=file:/dev/v/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
Save and close the file after completing the above commands.
Next, inform the system that you've created a new file by issuing the following command in the command line:
sudo systemctl daemon-reload
The following commands will allow you to execute the Tomcat service:
cd /opt/tomcat/bin
sudo ./startup.sh run
Step 6: Adjust the Firewall
It is essential to adjust the firewall so that requests can reach the service. Tomcat uses port 8080 to accept conventional requests. Allow traffic to that port using UFW:
sudo ufw allow 8080
Step 7: Configure the Tomcat Web Management Interface
Follow the command below to add a login for your Tomcat user and edit the 'tomcat-users.xml' file:
sudo nano /opt/tomcat/conf/tomcat-users.xml
Now, define the user who can access the files and add usernames and passwords:
For the Manager app, type:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
For the Host Manager app, type:
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
To restart the Tomcat service and view the effects:
sudo systemctl restart tomcat
Step 8: Access the Online Interface
Now that you have a user, you can access the web management interface in a browser. Once again, you can access the interface by providing your server's domain name or IP address followed by port 8080 in your browser - http://server_domain_or_IP:8080
Let's take a look at the Manager App, accessible via the link - http://server_domain_or_IP:8080/manager/html.
Make sure that you entered the account credentials in the 'tomcat-users.xml' file.
We use the Web Application Manager to manage our Java applications. You can Begin, Stop, Reload, Deploy, and Undeploy all apps here. Lastly, it provides data about your server at the bottom of the page.
Now let's look at the Host Manager, accessible via http://server_domain_or_IP:8080/host-manager/html/
From the Virtual Host Manager page, you can also add new virtual hosts that follow your application form's guidelines.

