How to Install Tomcat on Ubuntu
Tomcat is a widely used open-source web server that provides a robust platform for running Java Servlets and JavaServer Pages (JSPs). This tutorial will guide you through the process of installing Tomcat on Ubuntu 18.04, 20.04, and 22.04. The instructions are also applicable to Debian-based operating systems.
Prerequisites
Before you begin, ensure that you have the following:
- A server running Ubuntu 18.04, 20.04, or 22.04.
- A user account with
sudo
privileges. - A stable internet connection for downloading necessary packages.
Step 1: Install Java
Tomcat 9 requires Java 8 or higher. You can choose between OpenJDK and Oracle JDK. This guide uses OpenJDK 11. To install it, execute the following commands in your terminal:
$ sudo apt update
$ sudo apt install openjdk-11-jdk
The apt update
command refreshes the package lists, ensuring you download the latest versions. The apt install openjdk-11-jdk
command installs the OpenJDK 11 Development Kit.
Step 2: Download Tomcat
Download the Tomcat distribution from the official Apache Tomcat website. Navigate to the official website and select the latest version of Tomcat 9. At the time of this writing, the current version is 9.0.86.
Locate the "Binary Distributions" section and copy the link address of the tar.gz
file under the "Core" subsection.
Use the wget
command to download the file to your server:
$ wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.86/bin/apache-tomcat-9.0.86.tar.gz
This command downloads the specified Tomcat archive to your current directory.
Step 3: Extract Tomcat
After downloading, extract the Tomcat archive to your desired installation location. For this tutorial, we’ll use /opt/tomcat
.
First, create the /opt/tomcat
directory:
$ sudo mkdir /opt/tomcat
Then, extract the downloaded archive using the tar
command:
$ sudo tar xzvf apache-tomcat-*.tar.gz -C /opt/tomcat --strip-components=1
This command extracts the contents of the archive into the /opt/tomcat
directory, removing the top-level directory structure within the archive (--strip-components=1
).
Step 4: Configure Tomcat User
To access the Tomcat web interface and manage your applications, create a user with the necessary roles.
Open the tomcat-users.xml
file in a text editor:
$ sudo nano /opt/tomcat/conf/tomcat-users.xml
Add a user element inside the <tomcat-users>
element with username
, password
, and roles
attributes. For example:
<tomcat-users>
<!-- ... -->
<user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>
Replace "admin"
and "password"
with your desired username and password. The roles
attribute grants the user access to the manager and admin web applications. Save and close the file.
Step 5: Start Tomcat
Start Tomcat using the startup.sh
script located in the /opt/tomcat/bin
directory:
$ sudo sh /opt/tomcat/bin/startup.sh
You should see output similar to this:
Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME: /usr/lib/jvm/java-11-openjdk-amd64/
Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Tomcat started.
Verify that Tomcat is running using the ps
command:
$ ps -ef | grep tomcat
This will display a list of processes, including the Tomcat process.
Shutdown the server to proceed to the next step:
$ sudo sh /opt/tomcat/bin/shutdown.sh
Step 6: Create a Tomcat User and Group
For enhanced security, create a dedicated user and group for running the Tomcat service.
Create a new tomcat
group:
$ sudo groupadd tomcat
Create a new tomcat
user, adding it to the tomcat
group and setting /opt/tomcat
as its home directory:
$ cd /opt/tomcat
$ sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Set the group ownership of the entire installation directory to the tomcat
group:
$ sudo chgrp -R tomcat /opt/tomcat
Grant read access to the conf
directory and execute access to its parent directory:
$ sudo chmod -R g+r conf
$ sudo chmod g+x conf
Ensure the tomcat
user owns the webapps
, work
, temp
, and logs
directories:
$ sudo chown -R tomcat webapps/ work/ temp/ logs/
Step 7: Create a systemd Unit File
To manage Tomcat as a service, create a systemd unit file.
Create a file named tomcat.service
in the /etc/systemd/system/
directory:
$ sudo nano /etc/systemd/system/tomcat.service
Paste the following configuration into the file:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-11-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
Save and close the file. Notify systemd of the new unit file:
$ sudo systemctl daemon-reload
Enable and start the Tomcat service:
$ sudo systemctl start tomcat
$ sudo systemctl enable tomcat
Step 8: Test Tomcat
Open your web browser and navigate to http://your_server_ip:8080
. Replace your_server_ip
with the actual IP address of your server.
You should see the default Tomcat homepage.
To access Tomcat’s web interface (Manager App), click on the "Manager App" button and enter the username and password you configured in Step 4.
You should see a page displaying your deployed applications and allowing you to manage them. Congratulations! You have successfully installed Tomcat on Ubuntu.
Alternative Solutions
While the above steps provide a manual installation method, here are two alternative ways to install Tomcat on Ubuntu:
1. Using Tomcat Docker Image:
Docker provides a containerized environment, making it easier to deploy and manage applications. Using a Tomcat Docker image simplifies the installation process.
-
Explanation: Docker containers encapsulate the application and its dependencies, ensuring consistency across different environments. The official Tomcat Docker image provides a pre-configured Tomcat instance.
-
Steps:
-
Install Docker:
sudo apt update sudo apt install docker.io sudo systemctl start docker sudo systemctl enable docker
-
Pull the Tomcat Docker image:
docker pull tomcat:latest
-
Run the Tomcat container:
docker run -d -p 8080:8080 -p 8005:8005 --name mytomcat tomcat:latest
-d
: Runs the container in detached mode (background).-p 8080:8080
: Maps port 8080 on the host to port 8080 in the container.-p 8005:8005
: Maps port 8005 (shutdown port) on the host to port 8005 in the container.--name mytomcat
: Assigns the name "mytomcat" to the container.tomcat:latest
: Specifies the Tomcat image to use.
-
Access Tomcat in your browser at
http://your_server_ip:8080
.
-
2. Using SDKMAN!:
SDKMAN! (Software Development Kit Manager) is a tool for managing parallel versions of multiple Software Development Kits on most Unix-like systems. It can be used to install and manage Tomcat.
-
Explanation: SDKMAN! simplifies the installation and management of various software development kits, including Tomcat. It handles the downloading, installation, and configuration processes.
-
Steps:
-
Install SDKMAN!:
curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh"
-
Install Tomcat:
sdk install tomcat
This command installs the latest version of Tomcat. To install a specific version, use:
sdk install tomcat <version>
(e.g.,
sdk install tomcat 9.0.86
) -
Start Tomcat:
tomcat start
-
Stop Tomcat:
tomcat stop
-
Access Tomcat in your browser at
http://localhost:8080
. Note that SDKMAN! installs Tomcat in your user’s home directory, and it runs under your user account. You can configure SDKMAN! to install Tomcat in a different location if needed. Also, if accessing the Tomcat server remotely, remember to adjust your firewall settings accordingly.
-
These alternative methods offer different approaches to installing and managing Tomcat, each with its own advantages and disadvantages. Docker provides containerization for consistency, while SDKMAN! simplifies SDK management. Choose the method that best suits your needs and environment.