How to Install Apache Tomcat Server on ubuntu 20.04 LTS.

Apache Tomcat is an open source web server and Java servlet container for publishing web applications.We can use Apache Tomcat for deploying Java Servlet and JSP applications.It is lightweight & easy to run applications in Ubuntu.It provides fast loading and helps run a server more efficiently.

Install Apache Tomcat on ubuntu

Update the System.

apt-get update

Install Java.

apt-get install openjdk-11-jdk

Check Java version.

java -version

Here is the command output.

openjdk version "11.0.11" 
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

Create a Tomcat Account or System User.

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

Install Tomcat 10 version.

wget https://downloads.apache.org/tomcat/tomcat-10/v10.0.8/bin/apache-tomcat-10.0.8.tar.gz   

Extracted downloaded archive file & copy all content to tomcat folder.

tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1 

GIve the permissions.

chown -R tomcat:tomcat /opt/tomcat/ 
chmod -R u+x /opt/tomcat/bin 

Create Tomcat User.Open tomcat user file.

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

Add the following lines.

<!-- user manager can access only manager section -->
<role rolename="manager-gui" />
<user username="manager" password="manager_password" roles="manager-gui" />

<!-- user admin can access manager and admin section both -->
<role rolename="admin-gui" />
<user username="admin" password="admin_password" roles="manager-gui,admin-gui" />

</tomcat-users>

Create a Systemd Unit File for tomcat.

Open the file.

vim /etc/systemd/system/tomcat.service 

Add the following lines.

[Unit]
Description=Tomcat
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"
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

Reload, Start & Enable the tomcat service.

systemctl daemon-reload 
systemctl start tomcat.service 
&
systemctl enable tomcat.service 

Check Tomcat service status.

systemctl status tomcat.service 

Here is the command output.

● tomcat.service - Tomcat
     Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
     Active: active (running) ; 38min ago
   Main PID: 5448 (java)
      Tasks: 29 (limit: 1160)
     Memory: 228.2M
     CGroup: /system.slice/tomcat.service
             └─5448 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.>

Access Tomcat.

Open tomcat web-interface.

http://server-ip:8080/

Fig 2

 

Access Tomcat Manager application.

  • We can access Tomcat manager and host-manager applications.
  • Modify the context.xml file.
vim /opt/tomcat/webapps/manager/META-INF/context.xml

Comment the following lines.

<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
  ...
</Context>

Open host manager application.

vim /opt/tomcat/webapps/host-manager/META-INF/context.xml

 Comment the following lines.

<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <!--<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
  ...
</Context>

Restart tomcat service.

systemctl restart tomcat

Test Tomcat manager web-page.

http://server-ip:8080/manager/

Provide the tomcat manager username & password.

Fig 1

 

Now Tomcat manager home page is ready.

Fig. 3

 

Leave a Reply