Introduction
Apache Tomcat is an open source web server and servlet container developed by the Apache Software Foundation. It is used to deploy and serve Java-based web applications. In this tutorial, we will show you how to install Apache Tomcat 9 on Ubuntu 18.04. We will also show you how to configure Tomcat to run as a service and how to manage the Tomcat server.
How to Install Apache Tomcat 9 on Ubuntu 18.04
1. Update the apt package index:
sudo apt update
2. Install the Tomcat 9 package:
sudo apt install tomcat9
3. Check the status of the Tomcat service:
sudo systemctl status tomcat9
4. Enable the Tomcat service to start on boot:
sudo systemctl enable tomcat9
5. Open the Tomcat configuration file:
sudo nano /etc/tomcat9/server.xml
6. Change the port number from 8080 to a different port number:
7. Save and close the file.
8. Restart the Tomcat service:
sudo systemctl restart tomcat9
9. Test the installation by accessing the Tomcat web page in a web browser:
http://your_server_ip_address:port_number
Introduction
Apache Tomcat is a free, open-source, lightweight application server used for Java-based web applications. Developers use it to implement Java Servlet and JavaServer Pages technologies (including Java Expression Language and Java WebSocket).
Read this guide to learn how to install and configure Apache Tomcat on Ubuntu 18.04.
Note: These steps apply to other Ubuntu-based distributions, including Ubuntu 16.04, Linux Mint, and Elementary OS.
Prerequisites
- An Ubuntu-based distribution (such as Ubuntu 18.04)
- A user account with sudo privileges
- A terminal window (Ctrl–Alt–T)
- The apt package manager, included by default
Steps for Installing Tomcat 9 on Ubuntu
Follow the steps below to Install Tomcat on Ubuntu.
Step1: Check if Java is Installed
Before you can download and install Tomcat, make sure you have the required Java installation for Ubuntu (OpenJDK).
Open the terminal (Ctrl+Alt+T) and use the following command check the Java version:
java -version
The output will show the Java version running on your system. Currently, the latest release is OpenJDK 11.0.3:
Step 2: Install OpenJDK
If you do not have OpenJDK or have a version older than Java 8, install the newest release by typing the following:
sudo apt install default-jdk
Step 3: Create Tomcat User and Group
For security reasons, do not run Tomcat under the root user. Create a new group and system user to run the Apache Tomcat service from the /opt/tomcat
directory.
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Step 4: Download Tomcat 9
1. Download the latest binary Tomcat release navigate to the official Apache Tomcat Download page.
2. On it, find the Binary Distributions > Core list and the tar.gz link in it. Copy the link of the file.
3. Go back to the terminal and change to the /tmp
directory with the command:
cd /tmp
4. Now, use the curl command with the tar.gz link you copied in step 2 to download the package:
curl -O https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz
1. To extract the tar.gz Tomcat file, create a new /opt/tomcat/
directory with the command:
sudo mkdir /opt/tomcat
2. Then, extract the file in the new directory with the following command:
sudo tar xzvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1
Step 6: Modify Tomcat User Permission
The new Tomcat user you created does not have executable privileges, but it needs to access the installation directory. You need to setup execute privileges over the directory.
1. Move to the directory where the Tomcat installation is located:
cd /opt/tomcat
2. Grant group and user ownership over the installation directory to the tomcat
group and user with the command:
sudo chown -RH tomcat: /opt/tomcat
3. Lastly, change script permissions to grant execute access in /opt/tomcat/bin/with:
sudo sh -c 'chmod +x /opt/tomcat/bin/*.sh'
Step 7: Create System Unit File
Since you are going to to use Tomcat as a service, you need to create a systemd service file.
1. To configure the file, you first need to find the JAVA_HOME
path. This is the exact location of the Java installation package.
To do so, prompt the system to give you information about the Java packages installed on the system. In the terminal, type:
sudo update-java-alternatives -l
As the output shows, there are two available versions of Java. Accordingly, it also shows two paths displaying their location.
Choose the version you want to use and copy its location. With that, you can move on to create the service file.
2. Create and open a new file in the /etc/system/system under the name tomcat.service:
sudo nano /etc/systemd/system/tomcat.service
3. Once the file opens, copy and paste the content below, changing the JAVA_HOME
value to the information you found in the previous step.
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"
Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
4. Save and Exit the file (Ctrl+X, followed by y[es] and Enter).
5. For the changes to take place, reload the system daemon with the command:
sudo systemctl daemon-reload
6. Now, you can finally start the Tomcat service:
sudo systemctl start tomcat
7. Verify the Apache Tomcat service is running with the command:
sudo systemctl status tomcat
The message you want to receive is that the service is active (running).
Step 8: Adjust Firewall
If you are using a firewall to protect your server (as you should), you will not be able to access the Tomcat interface. Tomcat uses Port 8080, which is outside your local network.
1. Open Port 8080 to allow traffic through it with the command:
sudo ufw allow 8080/tcp
2. If the port is open, you should be able to see the Apache Tomcat splash page. Type the following in the browser window:
http://server_ip:8080
or
http://localhost:8080
Your web browser should open the web page as in the image below:
Step 9: Configure Web Management Interface
Once you verified the service is running properly, you need to create a user who can use the web management interface.
To do this, open and edit the users file.
1. Open the users file with the command:
sudo nano /opt/tomcat/conf/tomcat-users.xml
The file should appear like the one in the image below:
2. Delete everything from the file and add the following:
<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="admin" password="Your_Password" roles="manager-gui, manager-script, manager-jmx, manager-status, admin-gui, admin-script"/>
</tomcat-users>
Make sure to replace the Your_Password
value with a strong password of your preference.
3. Save and Exit the file.
Step 10: Configure Remote Access
Finally, you need to configure remote access. This is required. By default, Tomcat is only accessible from the local machine.
1. First, open the manager file:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
2. Next, decide whether to grant access from a. anywhere or b. from a specific IP address.
a. To make it publicly accessible, add the following lines to the file:
<Context antiResourceLocking="false" privileged="true">
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+.d+|::1|0000:1" />
-->
</Context>
b. To allow access from a specific IP address, add the IP to the previous command, as follows:
<Context antiResourceLocking="false" privileged="true">
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0000:1|THE.IP.ADDRESS." />
-->
</Context>
3. Repeat the same process for the host-manager file.
Start by opening the file with the command:
sudo nano /opt/tomcat/latest/webapps/host-manager/META-INF/context.xml
4. Followed by granting access from a. anywhere or b. from a specific IP address (as in the previous step).
Conclusion
With the help of this guide, you have installed Tomcat on Ubuntu 18.04! You now have a working Tomcat installation on your Apache server and can start deploying your Java web applications.
How to Install Apache Tomcat 9 on Ubuntu 18.04
Apache Tomcat is an open source web server and servlet container developed by the Apache Software Foundation. It is used to deploy Java Servlets and JSPs (Java Server Pages). In this tutorial, we will show you how to install Apache Tomcat 9 on Ubuntu 18.04.
Prerequisites
Before you begin with this guide, you should have a non-root user with sudo privileges set up on your server. You can learn how to set up a user with these privileges by following our Ubuntu 18.04 initial server setup guide.
Step 1 — Installing Java
Apache Tomcat requires Java to be installed on the server. We will install OpenJDK 8, which is the recommended version. You can install it by running the following command:
sudo apt-get update
sudo apt-get install default-jdk
Once the installation is complete, you can check the version of Java by running the following command:
java -version
You should see the following output:
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
Step 2 — Downloading Apache Tomcat
Now that Java is installed, we can download Apache Tomcat. At the time of writing this tutorial, the latest version of Apache Tomcat is 9.0.14. You can check for the latest version on the Apache Tomcat download page.
You can download the latest version of Apache Tomcat by running the following command:
wget http://mirrors.estointernet.in/apache/tomcat/tomcat-9/v9.0.14/bin/apache-tomcat-9.0.14.tar.gz
Once the download is complete, extract the downloaded file by running the following command:
tar xzf apache-tomcat-9.0.14.tar.gz
This will extract the files into a directory called apache-tomcat-9.0.14
.
Step 3 — Installing Apache Tomcat
Now that the files are extracted, we can move the files to the /opt
directory. This is the recommended location for installing applications on Ubuntu.
You can move the files by running the following command:
sudo mv apache-tomcat-9.0.14 /opt/tomcat
Next, we will create a tomcat
user and group. This user will be used to run the Tomcat service.
You can create the user and group by running the following command:
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Next, we will give the tomcat
user ownership of the /opt/tomcat
directory. This will allow the user to access the files in the directory.
You can give the user ownership of the directory by running the following command:
sudo chown -R tomcat: /opt/tomcat
Step 4 — Configuring Apache Tomcat
Now that the files are in place, we can configure Apache Tomcat. We will start by creating a systemd service file for Tomcat.
You can create the service file by running the following command:
sudo nano /etc/systemd/system/tomcat.service
Add the following lines:
[Unit]
Description=Apache Tomcat Web Application 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"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
Save and close the file when you are finished.
Next, we will reload the systemd daemon so that it can read the service file.
You can do this by running the following command:
sudo systemctl daemon-reload
Now, we can start the Tomcat service and enable it to start on boot.
You can do this by running the following commands:
sudo systemctl start tomcat
sudo systemctl enable tomcat
You can check the status of the Tomcat service by running the following command:
sudo systemctl status tomcat
You should see the following output:
● tomcat.service - Apache Tomcat Web Application Container
Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-05-02 11:45:17 UTC; 1min 10s ago
Main PID: 8092 (java)
Tasks: 51 (limit: 1152)
CGroup: /system.slice/tomcat.service
└─8092 /usr/lib/jvm/default-java/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat -Dcatalina.home=/opt/tomcat -Djava.io.tmpdir=/opt/tomcat/temp org.apache.catalina.startup.Bootstrap start
May 02 11:45:17 ubuntu1804 systemd[1]: Started Apache Tomcat Web Application Container.
Step 5 — Configuring the Firewall
If you have UFW firewall enabled on your server, you will need to open port 8080 to allow external access to the Tomcat web interface.
You can open port 8080 by running the following command:
sudo ufw allow 8080/tcp
You can check the status of the UFW firewall by running the following command:
sudo ufw status
You should see the following output:
Status: active
To Action From
-- ------ ----
8080/tcp ALLOW Anywhere
8080/tcp (v6) ALLOW Anywhere (v6)
Step 6 — Accessing the Tomcat Web Interface
Now that Apache Tomcat is installed and the firewall is configured, you can access the Tomcat web interface. To do this, you will need to know your server’s public IP address.
You can find your server’s public IP address by running the following command:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
You should see your server’s public IP address:
203.0.113.0
Now, open your web browser and type the URL http://203.0.113.0:8080
. You should see the Apache Tomcat welcome page:
You can log in to the Tomcat web interface using the default username and password. The default username is admin
and the default password is admin
.
You should see the Tomcat web interface:
Conclusion
In this tutorial, you learned how to install Apache Tomcat 9 on Ubuntu 18.04. You also learned how to configure the firewall and access the Tomcat web interface.