How to Install and Configure Apache Tomcat on an Ubuntu 18.04 VPS or Dedicated Server

How to Install and Configure Apache Tomcat on an Ubuntu 18.04 VPS or Dedicated Server

Introduction

Web containers are applications written mainly to allow for running of Java code on the web server. It creates an environment for running java servlets and java server pages with the purpose of providing dynamic web content.

Apache Tomcat is one of the most popular Java web containers out there which is open source because it offers almost all commercial features of a typical web application container. It offers some advantages, of course including:

  • Being open source, your rest assured Apache Tomcat is free for use.
  • Although Tomcat may not be feature rich, it is extremely lightweight.
  • The web container puts a lot of focus on stability hence you are sure that your application will rarely crush, and even in the event so, your server will continue to run just fine.

Before You Start

Steps

Update System Packages

$ sudo apt update -y && sudo apt upgrade -y

Install and Configure Java Environment

$ sudo apt install default-jre
$ sudo apt install default-jdk
$ java -versionopenjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-1ubuntu0.18.04.1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

Set your JAVA_HOME path

$ sudo sh -c 'echo export JAVA_HOME=/usr/lib/java-8-openjdk-adm64 > /etc/profile.d/java.sh'
$ source /etc/profile.d/java.sh
$ echo$JAVA_HOME
/usr/lib/jvm/java-8-openjdk-amd64

Install and configure Apache Tomcat

Download the latest version of Tomcat binary from the official site.

$ wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.12/bin/apache-tomcat-9.0.12.tar.gz

Create a directory called tomcat or whatever you want (though relevant names are highly advised) in your /opt folder and extract the downloaded contents there.

$ sudo mkdir -p /opt/tomcat
$ sudo tar -xf apache-tomcat-9.0.12.tar.gz -C /opt/tomcat

Create a symbolic link so as to control the version release and updates.

$ sudo ln -s /opt/tomcat/apache-tomcat-9.0.12 /opt/tomcat/latest

Create a new user called tomcat and change ownership of the tomcat folder to the added user.

$ sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
$ sudo chown -RH tomcat: /opt/tomcat/latest

Change the scripts in the binary folder to an executable mode

$ sudo chmod o+x /opt/tomcat/latest/bin/

To make the Tomcat service easily manageable, we will create a unit file and paste in the code below so as to run it as a service. Ensure to edit the JAVA_HOME path appropriately

$ sudo vim /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat 9 servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"
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

Like any other service, start and enable Tomcat service.

$ sudo systemctl start tomcat
$ sudo systemctl enable tomcat
$ sudo systemctl status tomcat● tomcat.service - Tomcat 9 servlet container
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-11-0722:43:15 EAT; 1s ago
  Process:13927 ExecStart=/opt/tomcat/latest/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 13938 (java)
    Tasks:16
   Memory:125.9M
      CPU:1.902s
   CGroup: /system.slice/tomcat.service
           └─13938/usr/lib/jvm/java-8-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/latest/conf/logging.properties
Nov 0722:43:15 replicationserver systemd[1]: Starting Tomcat 9 servlet container...
Nov 0722:43:15 replicationserver startup.sh[13927]: Tomcat started.
Nov 0722:43:15 replicationserver systemd[1]: Started Tomcat 9 servlet container.

Configure a username and password for accessing the web administration panel. These settings may be found in the tomcat-users.xmlfile.

$ sudo vim /opt/tomcat/latest/conf/tomcat-users.xml

Add the following block of code inside the tomcat-users element

<role rolename="administrator"/>
  <role rolename="manager"/>
  <user username="admin"password="admin123"roles="administrator,manager"/>

Exit while saving the changes. To enable remote access to the Managerand HostManager applications, edit both of the files context.xml i.e. the /opt/tomcat/latest/webapps/manager/META-INF/context.xml and /opt/tomcat/latest/webapps/host-manager/META-INF/context.xml to look like the code below while replacing the 192.168.56.30with your appropriate IP address.

...
<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1|192.168.56.30" />
  <Manager sessionAttributeValueClassNameFilter="java.lang.(?:Boolean|Integer|Long|Number|String)|org.apache.catalina.filters.CsrfPreventionFilter$LruCache(?:$1)?|java.util.(?:Linked)?HashMap"/>
</Context>

Now restart Tomcat service.

$ sudo systemctl restart tomcat.

Head over to your browser and go to http://YOUR_IP_ADDRESS:8080 (use your IP address in place of the YOUR_IP_ADDRESS). You should see a screen similar to the one below.

Conclusion

You have now installed Apache Tomcat. You can start doing your Java web development right away or explore the features to be offered. Remember Apache Tomcat is only as secure as you configure it.

A heavy burden of securing the application remains on the administrator, issues such as password policy can be the first important step to ensuring a safe web application container software.

Check out these top 3 Java hosting services:

Kamatera
$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3

How to Set Up WordPress Multisite on Ubuntu 18.04 with Apache Web Server

In this tutorial, we explain how to set up WordPress multisite on an Ubuntu 18.0
3 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install a Let’s Encrypt Certificate on your Ubuntu 18.04 Dedicated Server or VPS

If you are hosting your website on a VPS server running Ubuntu 18.04, we will sh
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Install WordPress with Nginx & Redis

This is an easy-to-follow guide written to help you learn how to install WordPre
5 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Install Own Cloud On an an Ubuntu 18.04 Dedicated Server or VPS

You can create your own self-hosted cloud storage services by installing the ope
3 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top