Apache Tomcat is a popular open-source Java web server that can be used to host web applications. In this article, we will show you how to install Apache Tomcat on Ubuntu 22.04.
Before we start, make sure you have Java installed on your system. You can check this by running the following command:
java -version
If Java is not installed, you can install it by running the following command:
sudo apt update
sudo apt install openjdk-14-jd
Once Java is installed, you can download the latest version of Apache Tomcat from the Apache Tomcat website (https://tomcat.apache.org/download-90.cgi
).
Next, create a new directory to install Tomcat, such as /opt/tomcat
, and extract the downloaded Tomcat archive to this directory:
sudo mkdir /opt/tomcat
sudo tar xvf apache-tomcat-9.0.XX.tar.gz -C /opt/tomcat --strip-components=1
Replace 9.0.XX
with the version of Tomcat you downloaded.
Now, we need to create a user for Tomcat. This is for security reasons, as running Tomcat as the root user is not recommended.
To create a new user, run the following commands:
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
sudo chown -R tomcat:tomcat /opt/tomcat
Next, we need to create a systemd service file for Tomcat, which will allow us to easily start, stop, and manage Tomcat as a service.
Create a new file called tomcat.service
in the /etc/systemd/system
directory using your favorite text editor, such as nano:
sudo nano /etc/systemd/system/tomcat.service
Paste the following contents into the file and save it:
[Unit]
Description=Apache Tomcat Web Server
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"
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
Next, reload the systemd daemon to read the new service file:
sudo systemctl daemon-reload
Now, we can start the Tomcat service and enable it to automatically start on boot:
sudo systemctl start tomcat
sudo systemctl enable tomcat
To verify that Tomcat is running run sudo systemctl stauts tomcat
.