Install and Configure Apache Tomcat on Ubuntu.

Introduction

Apache Tomcat is one of the most popular HTTP server and a Servlet container. It is a free open-source server that provides a Java HTTP web server environment in which Java code (Servlet) can run. Tomcat supports servlet and expression languages. The default port of Tomcat is 8080.

Here we will learn how to install and configure tomcat on the Ubuntu server.

Installing Apache Tomcat

Step 1. Install Java

 $ sudo apt update
 $ sudo apt install default-jdk

Step 2. Create a Tomcat user

We don’t use root user for security reasons.

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

Step 3. Install Apache Tomcat

Download tomcat in tmp/ directory

 $ cd /tmp 

Get the latest available package from tomcat download page.

tomcat-download

Download the tar package with the following command:

$ wget <copied_link> 

Step 4. Extract the Downloaded Package.

Make a directory for tomcat

$ sudo mkdir /opt/tomcat

Extract package in tomcat directory

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

Go to the tomcat directory.

$ cd /opt/tomcat

Step 5. Change Owner and Permissions

Set group of /opt/tomcat to tomcat.

$ sudo chgrp -R tomcat /opt/tomcat

Update the read access of the tomcat group over the conf/ directory and set execute permissions to the directory:

$ sudo chmod -R g+r conf
$ sudo chmod g+x conf

Now, make tomcat user owner of the webapps, work, temp, and logs directories:

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

Step 6. Create a Systemd Service File For Tomcat

To run Tomcat as a service we need to create a systemd service file. Also, Tomcat will have to know the location of Java in your system.

$ sudo update-java-alternatives -l
java-path

This output is the location of Java i.e JAVA_HOME.

You can choose any of them. In most case, there may be one location of Java.

Now create a service file.

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

Paste the complete code below in the service and replace the JAVA_HOME value with the value we get above.

[Unit]
 Description=Apache Tomcat Web Application Container
 After=network.target
 [Service]
 Type=forking
 Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-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
 User=tomcat
 Group=tomcat
 UMask=0007
 RestartSec=10
 Restart=always
 [Install]
 WantedBy=multi-user.target

Step 7. Reload the Deamon

$ sudo systemctl daemon-reload

Step 8. Star the Tomcat

$ sudo systemctl start tomcat
$ sudo systemctl status tomcat
tomcat-status

You can check by going to the browser and hitting on the server’s Public IP and port 8080.

tomcat-homepage

In case if you don’t see the Tomcat page then check your Security Group in the case of the public cloud provider. Or allow 8080 in the server’s firewall.

$ sudo ufw allow 8080

Enable the Tomcat service

$ sudo systemctl enable tomcat

Configuring Apache Tomcat.

Apache Tomcat already has a pre-installed web manager app. To use this, we need to set authentication in tomcat’s tomcat-users.xml file.

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

Add the below code to the file to add a user to access the admin and manager interfaces. Replace the Username and Password in your file.

<user username="Username" password="Password" roles="manager-gui,admin-gui"/> 
tomcat-conf

By default the access to Manager and Host Manager is restricted, we have to alter or remove the existing restrictions.

For Manager app:

$ sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml

For Host manager app:

$ sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

You can comment the IP restriction or allow your public IP. In this case, we will comment the restrictions.

tomcat-conf2

Restart the tomcat service.

$ sudo systemctl restart tomcat

Now check the Manager app by visiting http://ipaddress:8080/manager/. You will get a prompt for username and password then fill in the credentials configured in tomcat-users.xml.

tomcat-webapp

For Host manager, visit http://ip-address:8080/host-manager/.

Test Tomcat By Creating a Demo File

We can check if Tomcat working fine by creating a demo file inside of /opt/tomcat/webapps/ROOT/ directory.

$ sudo nano /opt/tomcat/webapps/ROOT/demo.jsp

Save the file and set ownership of the file as shown below.

$ sudo chown tomcat: /opt/tomcat/webapps/ROOT/demo.jsp

Now check by visiting the http://ip-address:8080/demo.jsp.

demo-page

Learn more about other webservers here.

Leave a Reply

Your email address will not be published. Required fields are marked *