How to Install Apache Tomcat on Debian 9

Install Apache Tomcat on Debian 9

In this tutorial we are going to learn how to install Apache Tomcat on Debian 9. We will also install and configure its prerequisites. Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, published by the Apache Software Foundation.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Debian 9 Stretch minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Apache Tomcat on Debian 9

Step 1. First, before we install packages, it’s a good idea to update repository and software packages. Run the following command on your Debian 9 OS:

sudo apt update
sudo apt upgrade

Step 2. Installing Java.

Apache Tomcat is written in Java application, so Java runtime environment needs to be installed:

sudo apt install default-jdk

Once Java is installed, you can verify the Java version by using the following command:

java -version

Step 3. Create Tomcat Group and User.

Tomcat should not be run as root user. So, create a normal user for running the Tomcat service:

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

Step 4. Installing the latest Apache Tomcat on Debian 9.

First, download the latest version of the Apache Tomcat from the website:

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

Then extract the Tomcat archive and move it to your desired (/opt/tomcat) directory:

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

Next, we will create a symbolic link named latest which will point to the Tomcat installation directory:

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

Now change the proper ownership using following command:

sudo chown -R tomcat: /opt/tomcat
sudo chmod +x /opt/tomcat/latest/bin/*.sh

Step 5. Create Systemd Unit for Tomcat.

Now you will need to create a systemd service file to manage the Tomcat process so you can run Tomcat as a service:

nano /etc/systemd/system/tomcat.service

Add the below information to Tomcat systemd service file:

[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:

systemctl daemon-reload
systemctl start tomcat 
systemctl enable tomcat

Step 6. Configure Firewall Apache Tomcat.

In order to test Apache Tomcat in a web browser, you need to modify the firewall rules:

sudo ufw allow 8080

Step 7. Configure Tomcat Web Management Interface.

In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

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

Place the following two lines just above the last line:

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

Restart Apache Tomcat to put your modifications into effect:

systemctl restart tomcat

Step 8. Accesing Apache Tomcat.

The Apache Tomcat is now accessible on the default port, which is 8080. The admin UI should be accessible at:

http://<your_domain_or_IP_address>:8080

Congratulation’s! You have successfully install and configured Tomcat on your Debian Stretch. Thanks for using this tutorial installing Apache Tomcat on Debian 9 (Stretch) system.

How to Install Apache Tomcat on Ubuntu 18.04

Install Apache Tomcat on Ubuntu 18.04

In this tutorial we are going to learn how to install Apache Tomcat on Ubuntu 18.04. We will also install and configure its prerequisites. Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, published by the Apache Software Foundation.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Ubuntu 18.04 LTS Bionic Beaver minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Apache Tomcat on Ubuntu 18.04

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y
apt-get upgrade -y

Step 2. Installing Java.

Apache Tomcat is written in Java application, so Java runtime environment needs to be installed:

apt install python-software-properties
add-apt-repository ppa:webupd8team/java

We will install the latest version of Oracle Java 8:

apt-get update -y
apt-get install oracle-java8-installer

After installation, you can verify the version by running:

java -version

Step 3. Create Tomcat Group and User.

Tomcat should not be run as root user. So, create a normal user for running the Tomcat service:

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

Step 4. Installing the latest Apache Tomcat on Ubuntu 18.04 Bionic Beaver.

First, download the latest version of the Apache Tomcat from the website:

wget http://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.10/bin/apache-tomcat-9.0.10.tar.gz

Then extract the Tomcat archive and move it to your desired (/opt/tomcat) directory:

sudo tar -xzvf apache-tomcat-9.0.10.tar.gz
sudo mv apache-tomcat-9.0.10 /opt/tomcat

Now change the proper ownership using following command:

sudo chgrp -R tomcat /opt/tomcat
sudo chown -R tomcat /opt/tomcat
sudo chmod -R 755 /opt/tomcat

Step 5. Create a Systemd Service File.

Now you will need to create a systemd service file to manage the Tomcat process so you can run Tomcat as a service:

nano /etc/systemd/system/tomcat.service

Add the following content:

[Unit]
Description=Apache Tomcat Web Server
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
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=15
Restart=always

[Install]
WantedBy=multi-user.target

Reload the systemd daemon, then start and add the Apache Tomcat service at boot time:

systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat

Step 6. Configure Firewall.

In order to test Apache Tomcat in a web browser, you need to modify the firewall rules:

sudo ufw allow 8080

Step 7. Configure Tomcat Web Management Interface.

In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

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

Place the following two lines just above the last line:

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

Restart Apache Tomcat to put your modifications into effect:

systemctl restart tomcat

Step 8. Accesing Apache Tomcat.

The Apache Tomcat is now accessible on the default port, which is 8080. The admin UI should be accessible at:

http://<your_domain_or_IP_address>:8080

Congratulation’s! You have successfully install and configured Apache Tomcat on your Ubuntu 18.04 server. Thanks for using this tutorial installing Apache Tomcat on Ubuntu 18.04 LTS (Bionic Beaver) system.

How to Install Apache Tomcat on Ubuntu 16.04

Install Apache Tomcat on Ubuntu 16.04

In this tutorial we will show you how to install Apache Tomcat on Ubuntu 16.04. We will also install and configure its prerequisites. Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, published by the Apache Software Foundation.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Ubuntu 16.04 minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Apache Tomcat on Ubuntu 16.04

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y
apt-get upgrade -y

Step 2. Installing Java.

Apache Solr is written in Java application, so Java runtime environment needs to be installed:

apt install python-software-properties
add-apt-repository ppa:webupd8team/java

We will install the latest version of Oracle Java 8:

apt-get update -y
apt-get install oracle-java8-installer

After installation, you can verify the version by running:

java -version

Step 3. Create Tomcat Group and User.

Tomcat should not be run as root user. So, create a normal user for running the Tomcat service:

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

Step 4. Installing the latest Apache Tomcat.

First, download the latest version of the Apache Tomcat from the website:

wget http://apache.mirrors.ionfish.org/tomcat/tomcat-8/v8.5.12/bin/apache-tomcat-8.5.12.tar.gz

Then extract the Tomcat archive and move it to your desired (/opt/tomcat) directory:

sudo tar -xzvf apache-tomcat-8.5.12.tar.gz
sudo mv apache-tomcat-8.5.12 /opt/tomcat

Now change the proper ownership using following command:

sudo chgrp -R tomcat /opt/tomcat
sudo chown -R tomcat /opt/tomcat
sudo chmod -R 755 /opt/tomcat

Step 5. Create a Systemd Service File.

Now you will need to create a systemd service file to manage the Tomcat process so you can run Tomcat as a service:

nano /etc/systemd/system/tomcat.service

Add the following content:

[Unit]
Description=Apache Tomcat Web Server
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
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=15
Restart=always

[Install]
WantedBy=multi-user.target

Reload the systemd daemon, then start and add the Apache Tomcat service at boot time:

systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat

Step 6. Configure Firewall.

In order to test Apache Tomcat in a web browser, you need to modify the firewall rules:

sudo ufw allow 8080

Step 7. Configure Tomcat Web Management Interface.

In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

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

Place the following two lines just above the last line:

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

Restart Apache Tomcat to put your modifications into effect:

systemctl restart tomcat

Step 8. Accesing Apache Tomcat.

The Apache Tomcat is now accessible on the default port, which is 8080. The admin UI should be accessible at http://your_server_ip:8080/solr.

Congratulation’s! You have successfully install and configured Apache Tomcat on your Ubuntu 16.04 server. Thanks for using this tutorial installing Apache Tomcat on Ubuntu 16.04 LTS (Xenial Xerus) system.

How to Install Apache Tomcat on CentOS 7

Install Apache Tomcat on CentOS 7

In this tutorial we’ll learn how to install Apache Tomcat on CentOS 7. We will also install and configure its prerequisites. Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, published by the Apache Software Foundation.

I recommend to use a minimal CentOS server setup as a basis for the tutorial, that can be a virtual or a root server image with an CentOS 7 minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Apache Tomcat on CentOS 7

Step 1. First, ensure your system is fully up-to-date by running the following:

yum -y install epel-release
yum -y update

Step 2. Installing Java.

Install the latest available version of Java on your server:

yum install java-1.8.0-openjdk.x86_64

Once Java is installed, you can verify it by running the following command:

java -version

Step 3. Create Tomcat User.

Tomcat should not be run as root user. So, create a normal user for running the Tomcat service:

groupadd tomcat
useradd -g tomcat -d /opt/tomcat -s /bin/nologin tomcat

Step 4. Installing the latest Apache Tomcat.

First, download the latest version of the Apache Tomcat from the website:

wget http://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.2/bin/apache-tomcat-9.0.2.tar.gzhttp://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.2/bin/apache-tomcat-9.0.2.tar.gz.md5

Then extract the Tomcat archive and move it to your desired (/opt/tomcat) directory:

tar -zxvf apache-tomcat-*.tar.gz
mv apache-tomcat-9.0.2/* /opt/tomcat/

Now change the proper ownership using following command:

chown -R tomcat:tomcat /opt/tomcat/

Step 5. Testing Tomcat.

Apache Tomcat can be started and stopped by the script which comes with the package. Start the Apache Tomcat:

cd /opt/tomcat/bin/
sh startup.sh

Tomcat is using port 8080 now, check the open port on the server with the netstat command:

netstat -plntu

Stop the Apache Tomcat:

cd /opt/tomcat/bin/
sh shutdown.sh

Step 6. Setup Systemd unit file for Apache Tomcat.

You should setup a Systemd unit file for Apache Tomcat:

nano /etc/systemd/system/tomcat.service

Add below information to Tomcat systemd service file:

[Unit]
Description=Apache Tomcat 9.x Web Application Container
Wants=network.target
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.el7_3.x86_64/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1G -Djava.net.preferIPv4Stack=true'
Environment='JAVA_OPTS=-Djava.awt.headless=true'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
SuccessExitStatus=143

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Reload the systemd daemon, then start and add the Apache Tomcat service at boot time:

systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat

Step 7. Configure Firewall.

In order to test Apache Tomcat in a web browser, you need to modify the firewall rules:

sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

Step 8. Configure Tomcat Web Management Interface.

In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

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

Place the following two lines just above the last line:

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

Restart Apache Tomcat to put your modifications into effect:

systemctl restart tomcat

Step 9. Accesing Apache Tomcat.

The Apache Tomcat is now accessible on the default port, which is 8080. The admin UI should be accessible at http://your_server_ip:8080/solr.

Congratulation’s! You have successfully install and configured Apache Tomcat on your CentOS 7 server. Thanks for using this tutorial for installing Apache Tomcat on CentOS 7 system.