How to Install Tomcat 9 on Debian 9

How to Install Tomcat 9 on Debian 9

Installing Tomcat 9 on Debian 9 is a fairly easy task, just carefully follow our tutorial below and you should have Tomcat 9 installed on your Debian 9 server in less than 10 minutes. Tomcat is an open-source web server and servlet container developed by the Apache Software Foundation, it was previously known as Tomcat or Jakarta Tomcat. Tomcat is required if you want to deploy and execute Java applications that are written in Java technologies including Java Servlet, JSP, etc. In this tutorial, we show you how to install Tomcat 9 on Debian 9. If your machine is running CentOS 7, you can follow our tutorial on how to install Tomcat 9 on CentOS 7.

1. Prerequisites

Tomcat requires Java JDK to be installed on the machine. You can either install Oracle JDK or OpenJDK. You need to have Java version 8 or higher installed on your system to run Tomcat 9.

2. Login and update the system

Login to your Debian 9 VPS via SSH as user root

ssh root@IP_Address -p7022
apt update
apt upgrade

3. Install Java

In this tutorial, we are using Java version 10.01

Go to http://www.oracle.com/technetwork/java/javase/downloads/jdk10-downloads-4416644.html and download the archive file:

wget http://download.oracle.com/otn-pub/java/jdk/10.0.1+10/fb4372174a714e6b8c52526dc134031e/jdk-10.0.1_linux-x64_bin.tar.gz?AuthParam=1524098700_a9d659266ceb23794153d6c6060cdc44 -O jdk-10.0.1_linux-x64_bin.tar.gz

Extract the tarball.

tar -xzvf jdk-10.0.1_linux-x64_bin.tar.gz

Now, let’s create some environment variables

nano /etc/profile.d/tomcat9

Add the following lines to the file:

export CATALINA_HOME="/usr/local/apache-tomcat-10.0.1"
export JAVA_HOME="/usr/lib/jvm/jdk-10.0.1"
export JRE_HOME="/usr/lib/jvm/jdk-10.0.1/jre"

Run this command to load the environment variables now:

source /etc/profile.d/tomcat9

Configure the newly installed Oracle Java JDK as default on your system. Invoke the following commands:

update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-10.0.1/bin/java" 1
update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk-10.0.1/bin/javac" 1
update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk-10.0.1/bin/javaws" 1

Let’s correct the permissions and ownership of the executable files

chmod a+x /usr/bin/java
chmod a+x /usr/bin/javac
chmod a+x /usr/bin/javaws
chown -R root: /usr/lib/jvm/jdk-10.0.1

Now, let’s run this command:

update-alternatives --config java

If you see the following message:

There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/jdk-10.0.1/bin/java
Nothing to configure.

It means that you have never installed Java before, you installed it just now.
If you see another message, containing Java versions to choose from, then you have installed another Java version before.

4. Install Tomcat 9 on Debian 9

Now, let’s proceed with the Apache Tomcat 9 installation

Go to Apache Tomcat official download website http://tomcat.apache.org/download-90.cgi and download the tarball version under Core

curl -O http://apache.mirrors.tds.net/tomcat/tomcat-9/v9.0.7/bin/apache-tomcat-9.0.7.tar.gz

Extract the tarball using the following command:

tar -xzvf apache-tomcat-9.0.7.tar.gz

Then, move the uncompressed directory to /opt

mv apache-tomcat-9.0.7 /opt/tomcat

As this stage, the tomcat directory is owned by root.
For security reasons, it is not recommended to run Tomcat as user root, so we will create a new system user

useradd -r tomcat9 --shell /bin/false

Change the ownership of the extracted directory to tomcat user we created just now.

chown -R tomcat9: /opt/tomcat/

5. Create Tomcat 9 user account

You can create a new Tomcat user in order to be able to access the Tomcat manager. Open the tomcat-users.xml file and add the following lines:

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

<role rolename="admin-gui" />
<user username="admin" password="PASSWORD" roles="manager-gui,admin-gui"
</tomcat-users>

Don’t forget to replace PASSWORD with an actual strong password.

6. Enable Host/Manager for Remote IP

By default, manager and host-manager pages are only accessible from localhost, or from a browser running on the same machine as Tomcat. It means, if you access from another IP address, you will see error 403 message. To access the pages from remote IPs, you need to allow your remote IP address in each deployed webapp. You can do it by editing context.xml file.

Manager File: webapps/manager/META-INF/context.xml
Host Manager File: webapps/host-manager/META-INF/context.xml

Edit the context.xml file and add your IP address in the “allow” section

<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|123.123.123.123" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$$
</Context>

7. Start/Stop Tomcat Service

By default, tomcat uses port 8080, make sure you don’t have another application running on the same port before running tomcat.

To start tomcat, you can invoke this command:

/opt/tomcat/bin/startup.sh

The output you will see after invoking the command:

Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Tomcat started.

To stop it, use the command:

/opt/tomcat/bin/shutdown.sh

You can access tomcat at http://IP_address:8080/ on your web browser.

You will be able to access the Apache Tomcat Manager by clicking the ‘Manager App’ button on the homepage, or directly at http://IP_address:8080/manager/html using user ‘admin’ and password ‘PASSWORD’ we created earlier.

To access Tomcat from a fully qualified domain name and use port 80 instead of 8080, you can add the following apache virtual host file:

<VirtualHost *:80>
ServerName yourdomain.com

ProxyRequests Off
ProxyPreserveHost On

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>

Restart apache, then you will be able to access tomcat at http://yourdomain.com

installing tomcat 9 on debian 9

 

Install Tomcat 9 on Debian 9Of course, you don’t have to install Tomcat 9 on Debian 9 if you use one of our SSD Tomcat Hosting services, in which case you can simply ask our expert Linux admins to install Tomcat 9 for you. They are available 24/7 and will take care of your request immediately.

PS. If you liked this post, on how to install Tomcat 9 on Debian 9, please share it with your friends on social networks using the buttons on the left or simply leave a reply below. Thanks.

 

4 thoughts on “How to Install Tomcat 9 on Debian 9”

Leave a Comment