Hamsa K
Editor
8 min read | 6 months ago

How to Install Apache Tomcat 9 on Ubuntu 18 04 LTS

Install  and configure Apache Tomcat 9 on Ubuntu 18.04 lts

Apache Tomcat 9 is a free and open source web server which is used to serve java based web applications.It is an open-source implementation of the Java Servlet and JavaServer Pages (JSP) technologies. Tomcat provides an HTTP Web Server for Java applications that supports HTTP/2, OpenSSL for JSSE, and TLS virtual hosting.

Step 1: Install java 8 or higher version

we must have Java installed on the system before installing Apache Tomcat.you can install java using default configured apt repositories on ubuntu 18.

root@linuxbox:~# apt update
root@linuxbox:~# apt install default-jdk

you can check java version by running below command

root@linuxbox:~# java -version
openjdk version "11.0.6" 2020-01-14

Step 2: set java environment variables

we will check java path first by using alternatives command

root@linuxbox:~# update-java-alternatives -l
java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd64

copy installation path of your preferred installation. Next open /etc/environment 

root@linuxbox:~# nano /etc/environment

add below line to above file and save it.

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

run the following command to apply the changes to your current session

root@linuxbox:~# source /etc/environment

Now verify java_home environment variable with following command.

root@linuxbox:~# echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-amd64

Step 3: download and install Apache Tomcat

Download and Install Tomcat 9 archive file using below command.You can also check official page to download latest available version

root@linuxbox:~# wget https://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.30/bin/apache-tomcat-9.0.30.tar.gz
root@linuxbox:~# tar -xvzf apache-tomcat-9.0.30.tar.gz
root@linuxbox:~# mv apache-tomcat-9.0.30 /usr/local/tomcat9

add new user,group as tomcat and set permissions like below

root@linuxbox:~# groupadd tomcat
root@linuxbox:~# useradd -s /bin/false -g tomcat -d /usr/local/tomcat9 tomcat
root@linuxbox:~# chgrp -R tomcat /usr/local/tomcat9 
root@linuxbox:~# cd /usr/local/tomcat9
root@linuxbox:/usr/local/tomcat9# chmod -R g+r conf
root@linuxbox:/usr/local/tomcat9# chmod -R g+x conf
root@linuxbox:/usr/local/tomcat9# chown -R tomcat webapps/ work/ temp/ logs/

Next, we will configure the Catalina tomcat servlet container environment by editing the .bashrc file using following commands

root@linuxbox:~# vim ~/.bashrc
### add below line to bashrc and save it ###
export CATALINA_HOME=/usr/local/tomcat9
root@linuxbox:~# source ~/.bashrc
root@linuxbox:~# echo $CATALINA_HOME
/usr/local/tomcat9

Step 4: Setup tomcat user accounts

Configure Tomcat users so they can access admin/manager sections.

root@linuxbox:~# nano /usr/local/tomcat9/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<user username="lampblogs" password="xxxx" roles="manager-gui,admin-gui"/>

Step 5: enable host/manager for remote ip

The default manager and host-manager web pages are enabled to access from localhost only. To access these pages from the remote system, you need to comment out one line in both the files.otherwise you can also add remote ip.

root@linuxbox:~# nano /usr/local/tomcat9/webapps/manager/META-INF/context.xml
root@linuxbox:~# nano /usr/local/tomcat9/webapps/host-manager/META-INF/context.xml

sample output

<!--<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Step 6: create systemd service

Next create a systemd file with name as /etc/systemd/system/tomcat.service 

root@linuxbox:~# vi /etc/systemd/system/tomcat.service

paste below content to above 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=/usr/local/tomcat9/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat9
Environment=CATALINA_BASE=/usr/local/tomcat9
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/usr/local/tomcat9/bin/startup.sh
ExecStop=/usr/local/tomcat9/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target

Reload the systemd daemon and start tomcat service with following commands

root@linuxbox:~# systemctl daemon-reload
root@linuxbox:~# systemctl start tomcat
root@linuxbox:~# systemctl enable tomcat
root@linuxbox:~# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-01-29 18:29:26 IST; 5min ago
 Main PID: 9459 (java)
    Tasks: 43 (limit: 1400)
   CGroup: /system.slice/tomcat.service
           └─9459 /usr/lib/jvm/java-11-openjdk-amd64/bin/java -Djava.util.logging.config.file=/usr/local/tomcat9/conf/logging.properties -Djava.util.logging.manager=org
Jan 29 18:29:26 linuxbox systemd[1]: Starting Apache Tomcat Web Application Container...
Jan 29 18:29:26 linuxbox startup.sh[9452]: Tomcat started.
Jan 29 18:29:26 linuxbox systemd[1]: Started Apache Tomcat Web Application Container.

Step 6: Test tomcat server

if firewall is enabled on your system,then run below command to allow it.

root@linuxbox:~# ufw allow 8080
Rules updated
Rules updated (v6)

Now open your web browser and type http://ip_address:8080

now we have successfully installed Tomcat 9, but to Access Application Web manager and Virtual Host Manager it will prompt username and password.Enter the details which are defined in tomcat-users.xml and try to access both.

virtual host manager



Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy