Categories: LinuxUbuntu

How to Install and Configure Apache Tomcat on Ubuntu

Tomcat, also called Apache Tomcat, is one of the most popular applications for running Java servlets, JSPs, and WebSockets. It is simple, lightweight, and used for rendering Java code and various other applications. It has a robust ecosystem of add-ons and is one of the most widely used applications and web servers worldwide.

This tutorial will show you how to install Tomcat using Nginx as a reverse proxy on Ubuntu 20.04.

Requirements

  • A server with Ubuntu 20.04 and 2 GB RAM.
  • A root password is set up on your server.

Install Java

Tomcat is a Java-based application. Therefore, you need to install Java on your server. You can install it with the following command:

apt-get install openjdk-11-jdk -y

Once Java is installed, check the installed version of Java with the following command:

java --version

You should get the following output:

openjdk 11.0.7 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

Install Tomcat

Before you get started, you need to create a user and a group to run Tomcat.

First, create a group named tomcat with the following command:

groupadd tomcat

Next, create a new user named tomcat with the following command:

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

Next, create a tomcat directory inside /opt and download the latest version of tomcat using the following command:

mkdir /opt/tomcat
apt-get install curl -y
curl -O http://mirrors.estointernet.in/apache/tomcat/tomcat-9/v9.0.35/bin/apache-tomcat-9.0.35.tar.gz

Once the download is complete, unzip the downloaded file to the /opt/tomcat directory using the following command:

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

Next, use the following command to change the owner of the /opt/tomcat directory to tomcat and give it the proper permissions:

chown -R tomcat:tomcat /opt/tomcat
chmod -R 755 /opt/tomcat

Create a systemd service file for tomcat.

First, find the location of Java with the following command:

update-java-alternatives -l

You should see the following output:

java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd64

You can use the above path when creating the systemd file for tomcat.

Next, create a new systemd service file to manage the Tomcat service.

nano /etc/systemd/system/tomcat.service

Add the following lines:

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-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 and reload the systemd daemon with the following command:

systemctl daemon-reload

Next, start the Tomcat service and enable it so that it starts on restart:

systemctl start tomcat
systemctl enable tomcat

Next, check the Tomcat service with the following command:

systemctl status tomcat

You should get the following output:

? tomcat.service - Apache Tomcat Web Application Container
     Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-05-21 17:13:08 UTC; 5s ago
    Process: 77758 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
   Main PID: 77776 (java)
      Tasks: 30 (limit: 4691)
     Memory: 116.2M
     CGroup: /system.slice/tomcat.service
             ??77776 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djav>

May 21 17:13:08 ubuntu2004 systemd[1]: Starting Apache Tomcat Web Application Container...
May 21 17:13:08 ubuntu2004 startup.sh[77758]: Tomcat started.
May 21 17:13:08 ubuntu2004 systemd[1]: Started Apache Tomcat Web Application Container.

Configure Tomcat Web UI

Next, you need to define a user to access the Tomcat Web UI. You can do this by editing the tomcat-users.xml file:

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

Add the following lines above :

<role rolename="manager-gui"/>
<user username="manager" password="secure-password" roles="manager-gui,admin-gui"/>

Save and close the file when you are done.

By default, Tomcat Web UI is configured to be accessible only from localhost. So you need to allow access to the Tomcat Web UI from anywhere.

To enable access for the Manager app, edit the following file:

nano /opt/tomcat/webapps/manager/META-INF/context.xml

Comment out the following line:

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

Save and close the file when you are done.

To enable access for the Host Manager app, edit the following file:

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

Comment out the following line:

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

Save and close the file when you are done. Then restart the Tomcat service to apply the changes.

systemctl restart tomcat

Once you are done, you can proceed to the next step.

Accessing Tomcat

Now open your web browser and enter the URL http://your-server-ip:8080. You will be redirected to Tomcat (see below):

Click on the Manager App button to access the Manager App. You should see the following page:

Enter your username and password and click the " Sign In " button. On the following page, you will see the Manager App dashboard:

Click on the Host Manager App button to access the Host Manager App. You should see the following page:

Enter your username and password and click the " Sign In " button. On the following page, you will see the Host Manager App dashboard:

Configure Nginx as a reverse proxy for Tomcat.

Tomcat runs on port 8080 by default, it is recommended to configure Nginx as a reverse proxy to access Tomcat.

First, install the Nginx web server using the following command:

apt-get install nginx -y

After installation, create a new configuration file for the Nginx virtual host using the following command:

nano /etc/nginx/sites-available/tomcat.conf

Add the following lines:

server {
  listen 80;

  server_name    your-server-ip;
  access_log /var/log/nginx/tomcat-access.log;
  error_log /var/log/nginx/tomcat-error.log;

  location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8080/;
  }
}

Save and close the file when you are done. Then create a symbolic link to the /etc/nginx/sites-enabled/ directory with the following command:

ln -s /etc/nginx/sites-available/tomcat.conf /etc/nginx/sites-enabled/

Next, check Nginx for a syntax error with the following command:

nginx -t

You should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart the Nginx service to apply the changes.

systemctl restart nginx

Next, check the status of the Nginx service with the following command:

systemctl status nginx

You should get the following output:

? nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-03-02 22:28:13 EST; 4min 14s ago
  Process: 984 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 982 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 980 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 985 (nginx)
    Tasks: 3 (limit: 25028)
   Memory: 5.5M
   CGroup: /system.slice/nginx.service
           ??985 nginx: master process /usr/sbin/nginx
           ??986 nginx: worker process
           ??987 nginx: worker process

May 20 21:28:12 ubuntu2004 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 20 21:28:12 ubuntu2004 nginx[982]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 20 21:28:12 ubuntu2004 nginx[982]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 20 21:28:13 ubuntu2004 systemd[1]: Started The nginx HTTP and reverse proxy server.

You can now access Tomcat using the URL http://your-server-ip.

Conclusion

Congratulations! You have successfully installed Tomcat with Nginx on Ubuntu 20.04. Now you can develop and host your Java-based application with Tomcat. For more information, see the Tomcat documentation.

Vitux Staff

Recent Posts

How to Install Magento 2 on AlmaLinux

Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…

1 year ago

How to Install ISPConfig Hosting Control Panel with Apache Web Server on Ubuntu 24.04

ISPConfig is an open-source control panel that allows users to manage multiple servers from a…

1 year ago

How to Test your Email Server (SMTP) Using the Telnet Command

As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…

1 year ago

Managing Network Interfaces and Settings on Ubuntu 24.04 with nmcli

Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…

2 years ago

Using Restic Backup on Ubuntu 24.04

Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…

2 years ago

Installing phpMyAdmin on Rocky Linux 9 and Securing it with Let’s Encrypt SSL

phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…

2 years ago