Read in 3 minutes

How to install Tomcat 8.5 on Ubuntu 18.04

Apache Tomcat is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies. It is one of the most widely adopted applications and web servers in the world today. Tomcat is simple to use and has a robust ecosystem of add-ons.

This tutorial demonstrates how to install Tomcat 8.5 on Ubuntu 18.04.

Install OpenJDK

OpenJDK, the open source implementation of the Java Platform is the default Java development and runtime in Ubuntu 18.04.

The installation is pretty straight forward:

sudo apt install default-jdk

If you want to install Oracle Java instead of OpenJDK please check this instruction.

Create Tomcat user

We will create a new system user and group with home directory /opt/tomcat which will run the Tomcat service:

sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat

Download Tomcat

We will use wget and unzip to download and extract the Tomcat archive. If you don’t have unzip and wget installed on your system install the packages with:

sudo apt install unzip wget

Download the latest version of Tomcat 8.5.x from the Tomcat downloads page. At the time of writing, the latest version is 8.5.31. Before continuing with the next step you should check the download page for a new version.

Change to the /tmp directory and download the zip file with wget:

cd /tmp
wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.zip

Once the download is completed, extract the zip file and move it to the /opt/tomcat directory:

unzip apache-tomcat-*.zip
sudo mv apache-tomcat-*/ /opt/tomcat/

In order to have more control over versions and updates, we will create a symbolic link latest which will point to the Tomcat installation directory:

sudo ln -s /opt/tomcat/apache-tomcat-* /opt/tomcat/latest

Later if you want to upgrade your Tomcat installation you can simply unpack the newer version and change the symlink to point to the latest version.

The tomcat user that we previously set up needs to have access to the tomcat directory, so we will change the directory ownership to user and group tomcat:

sudo chown -R tomcat: /opt/tomcat

and we will also make the scripts inside bin directory executable:

sudo chmod +x /opt/tomcat/latest/bin/*.sh

Advertisement

Create a systemd unit file

To run Tomcat as a service we will create a new tomcat.service unit file in the /etc/systemd/system/ directory with the following contents:

/etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat 8.5 servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

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

[Install]
WantedBy=multi-user.target

Notify systemd that we created a new unit file and start the Tomcat service by executing:

sudo systemctl daemon-reload
sudo systemctl start tomcat

You can check the service status with the the following command:

sudo systemctl status tomcat
● tomcat.service - Tomcat 8.5 servlet container
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-05-05 11:04:40 UTC; 5s ago
  Process: 13478 ExecStart=/opt/tomcat/latest/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 13499 (java)
    Tasks: 45 (limit: 507)
   CGroup: /system.slice/tomcat.service
           └─13499 /usr/lib/jvm/default-java/bin/java -Djava.util.logging.config.file=/opt/tomcat/latest/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.security.

and if there are no errors you can enable the Tomcat service to be automatically started at boot time:

sudo systemctl enable tomcat

Test the Installation

Open your browser and type: http://<your_domain_or_IP_address>:8080

Assuming that installation is successful, a screen similar to the following will appear:

If you can’t access the page then most likely your firewall is blocking port 8080. If you use ufw to open the port 8080 run the following command”

sudo ufw allow 8080/tcp

That’s all! If you hit any other problems or have any feedback, leave a comment below.