Install Apache Tomcat on Ubuntu 18.04
By Jithin on July 11th, 2020
The Apache Tomcat is a free, open-source Java-based web application that offers an HTTP webserver environment to run Java applications. It helps in the implementation of the JavaServer Pages, Java Expression Language, Java Servlet, and Java WebSocket technologies. Recent studies show that Apache is one of the most widely adopted applications and web servers. It is robust and easy to use. Tomcat is useful if you want to run web pages with Java server page coding or Java servlets. Some of the additional features of Tomcat 9, when compared to other Tomcat versions, are:
- Apache supports HTTP/2.
- It supports TLS virtual hosts (SNI).
- It supports the use of OpenSSL with the JSSE connectors for TLS support.
This knowledgebase helps you to install and configure Apache Tomcat 9 on Ubuntu 18.04 server. Please note that you can use this instruction to install and configure Apache Tomcat 9 on Ubuntu 16.04, other Ubuntu-based distributions, Elementary OS and Linux Mint.
Log in to the Ubuntu server as a user with sudo privileges.
Steps to Install and Configure Apache Tomcat 9 on Ubuntu
To install Apache Tomcat on your Ubuntu server, follow the below steps:
1) Install Java
Tomcat helps you to run Java-based web applications, so it is essential to install Java on your server. First, you need to update the apt packages before installing OpenJDK. Run the following commands to install OpenJDK.
$ sudo apt update $ sudo apt install default-jdk
2) Create a Tomcat User
For security purposes, we should disable direct root access in Tomcat. For the same, create a new system user and group in the home directory ‘/opt/tomcat’ on which the Tomcat service runs.
To create a tomcat group, run the following command.
$ sudo groupadd tomcat
Next, create a tomcat user as a member of the tomcat group under the ‘/opt/tomcat’ home directory.
$ sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
3) Download and Install Apache Tomcat
Download the Tomcat 9 latest binary release from the official Tomcat downloads page: https://tomcat.apache.org/download-90.cgi.
Run the following wget command to install the Tomcat archive in the /tmp directory.
$ sudo wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.30/bin/apache-tomcat-9.0.30.tar.gz -P /tmp
After the download, extract the Tomcat archive and move the extracted copy to the /opt/tomcat directory.
$ sudo tar -xf /tmp/apache-tomcat-9*.tar.gz -C /opt/tomcat
Create a symbolic link called ‘new’ and point it to the Tomcat installation directory to have more control over the Tomcat updates and version.
$ sudo ln -s /opt/tomcat/apache-tomcat-9.0.30 /opt/tomcat/new
If you want to upgrade your Tomcat version, then unpack the newer version and point the symlink to the latest version.
Change the directory ownership of the latest installed Tomcat using the following command.
$ sudo chown -RH tomcat: /opt/tomcat/new
Next, make the script inside the ‘/opt/tomcat/new/bin’ executable by using the following command.
$ sudo sh -c ‘chmod +x /opt/tomcat/new/bin/*.sh’
4) Create a Unit File
To run Tomcat on your server, you need to create a new unit file. To create the systemd unit file, follow the below steps:
Open a text editor and create a file called ‘tomcat.service’ in the ‘/etc/systemd/system/’ directory.
$ sudo nano /etc/systemd/system/tomcat.service
In the tomcat.service unit file, paste the following configuration. In the below configuration file, replace the <Java/home/path> flag with the path of your Java installation.
[Unit] Description=Tomcat 9 servlet container After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=<Java/home/path>" Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true" Environment="CATALINA_BASE=/opt/tomcat/new" Environment="CATALINA_HOME=/opt/tomcat/new" Environment="CATALINA_PID=/opt/tomcat/new/temp/tomcat.pid" Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC" ExecStart=/opt/tomcat/new/bin/startup.sh ExecStop=/opt/tomcat/new/bin/shutdown.sh [Install] WantedBy=multi-user.target
To find the path of the Java installation, run the following command. Replace the JAVA_HOME variable in the above configuration file with the result of the following command.
$ sudo update-java-alternatives -l
Save and close the unit file.
Update the systemd that we have created a new unit file by using the following command.
$ sudo systemctl daemon-reload
Next, start the Tomcat service.
$ sudo systemctl start tomcat
After staring, check the status of the Tomcat service by running the following systemctl command.
$ sudo systemctl status tomcat
If the status of the Tomcat service is up, then enable it to start up during the reboot.
$ sudo systemctl enable tomcat
5) Adjust the Firewall Settings
If the server is firewall protected and you want to access Tomcat from other networks, then you need to open the 8080 port in the firewall. To allow the traffic on 8080 port, run the following ufw command from the home directory.
$ sudo ufw allow 8080/tcp
6) Test Tomcat Installation
To test if the Tomcat installation and configuration completed without any error, do the following.
Open any web browser.
Then, hit any of the following links on your browser.
http://<your domain name>:8080 http://<your server IP address>:8080
This above link opens a screen similar to the one shown below if the installation and configuration were successful.
7) Test Apache Tomcat
To test if the Apache Tomcat service runs smoothly, follow the below process.
Create a test file with .jsp extension under the ‘/opt/tomcat/webapps/ROOT/’ directory.
$ sudo nano /opt/tomcat/webapps/ROOT/test.jsp
In the test file, paste the following html Java code.
<html> <head> <title>Test post:TomcatServerTest</title> </head> <body> <START OF JAVA CODES> <% out.println("Hello World! I am running my first JSP Application"); %> <END OF JAVA CODES> </body> </html>
Save and close the file and then change the ownership permission by using the following command.
$ sudo chown tomcat: /opt/tomcat/apache-tomcat-9.0.30/webapps/ROOT/test.jsp
Now, if the Tomcat is working smoothly, you can see the following content in the web page, if you hit the browser by using the link: http://<IP_address>:8080/test.jsp.
We hope this knowledge base was helpful to you. Please comment below for any questions or queries. If you are an InterServer customer, please reach out to our support team for further help.