लोड हो रहा है...... कृपया प्रतीक्षा करें........

July 4, 2022

Install Tomcat 9 on Ubuntu 20.04

Install Tomcat 9 on Ubuntu 20.04

Install Java in Ubuntu 20.04

First we will make sure our system is up-to-date

sudo su
apt update

We need to verify if java is installed

java -version

If we do not have Java installed, we will receive the output shown above. We can install java with the below command.

#for java 8
apt install openjdk-8-jdk -y
#for java 11
sudo apt install openjdk-11-jdk -y
#check installed java version
java -version

Install Tomcat 9 on Ubuntu 20.04

First, we need to create a group and a user for Tomcat.

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

Download tomcat tar file

cd /tmp/
sudo curl -O https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.64/bin/apache-tomcat-9.0.64.tar.gz

Create a directory for tomcat and unpack the tar file in it

mkdir /opt/tomcat
tar -xvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1

Provide permissions to the tomcat directory

cd /opt/tomcat
sudo chgrp -R tomcat /opt/tomcat
sudo chmod -R g+r conf
sudo chmod g+x conf
sudo chown -R tomcat webapps/ work/ temp/ logs/

Open a file called tomcat.service in the /etc/systemd/system directory by executing the below command

sudo nano /etc/systemd/system/tomcat.service

Paste the below content in the file:

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-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=10
Restart=always
[Install]
WantedBy=multi-user.target

Save and exit.

Execute the below command to reload the daemons

systemctl daemon-reload

Start the Tomcat service by executing the below command

systemctl start tomcat

Check tomcat status with the below command

# to check status
systemctl status tomcat
# to stop tomcat
systemctl stop tomcat
# to restart tomcat
systemctl restart tomcat

Now you can access the tomcat service on your browser. If you have installed tomcat in your local system, then enter the URL: http://localhost:8080. If you have installed it on a cloud instance, then enter the URL: http://<public_ip>:8080

Done. We have successfully installed Tomcat 9 on Ubuntu 20.04.

Change Tomcat port

If you have any other service already running on port number 8080, then you with either have to stop that service and then start tomcat to access it on the browser. Or else, you can change the tomcat port so that there is no conflict between the port and services. Follow the below steps to change the tomcat port,

  • Edit the server.xml file in tomcat directory to change the port number. Execute the below command to edit the service.xml file
sudo nano /opt/tomcat/conf/server.xml

Move down and find the below lines in configuration file

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

Change the port number here from 8080 to 8090 or any other port that is free in your system

<Connector port="8090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

Save and exit by pressing Ctrl X -> Y -> Enter keys
Now restart tomcat with the below command for the changes to take effect

sudo systemctl restart tomcat

Now you can access the Tomcat webpage with the new port number.

Posted in TutorialTaggs:
Write a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.