Friday, June 14, 2024
HomeHow toHow to Install Apache Tomcat with Nginx Reverse Proxy on Ubuntu 24.04

How to Install Apache Tomcat with Nginx Reverse Proxy on Ubuntu 24.04

Apache Tomcat or “Tomcat” is a free and open-source Java Servlet container for Java code and applications. It provides a “pure Java” HTTP server environment where Java code can run. Tomcat also implements JavaServer Pages, Java Expression Language, and WebSocket technologies. It supports SSL for securing sensitive data such as authentication.

This guide will teach you how to install Apache Tomcat on an Ubuntu 24.04 server. You will install Apache Tomcat 10 with the Java OpenJDK 21 and then install and configure Nginx as a reverse proxy.

Prerequisites

Before moving on, make sure you have:

  • An Ubuntu 24.04 server.
  • A non-root user with administrator privileges.

Installing Java OpenJDK

Before installing Apache Tomcat, you need to ensure that Java is installed. The latest Apache Tomcat 10 required at least Java 11 or later. In this section, you will install Java OpenJDK 21 from the official Ubuntu repository.

To start, update your Ubuntu package index with the command below.

sudo apt update

Now install the Java OpenJDK with the following command. On Ubuntu 24.04, the ‘default-jdk’ refers to the OpenJDK 21, which is the LTS version of Java.

sudo apt install default-jdk

Input Y to proceed with the installation.

After the installation is complete, check the Java version using the following command. You should see that Java OpenJDK 21 is installed on your Ubuntu server.

java --version

Installing Apache Tomcat 10

The Apache Tomcat 10 is available by default on the Ubuntu repository on the latest Ubuntu version. This allows you to install Tomcat easily using the APT package manager.

First, check the ‘tomcat10‘ package information with the following command.

sudo apt info tomcat10

The following shows that Apache Tomcat 10.1.16 is available on the Ubuntu ‘universe/java‘ repository.

Now install Apache Tomcat to your Ubuntu system with the command below. With this command, you will also install the Tomcat admin web applications, tools to create users, and examples of Tomcat web applications.

sudo apt install tomcat10 tomcat10-common tomcat10-admin tomcat10-user tomcat10-examples

Input Y to confirm the installation.

The ‘tomcat10’ service will be created when the installation is finished.

Check the ‘tomcat10‘ service status with the following command:

sudo systemctl is-enabled tomcat10
sudo systemctl status tomcat10

You can see below, the ‘tomcat10‘ service is enabled and active(running). The ‘tomcat10‘ service will start automatically at boot.

Now run the command below to check the Apache Tomcat port 8080. If Tomcat is running, you will see the Java application uses port 8080.

ss -tulpn

Lastly, visit http://192.168.5.30:8080/ using your web browser and you will see the Apache Tomcat default index page like the following:

Setting up Apache Tomcat authentication

In this section, you will set up basic authentication and allow access to the Tomcat host manager and manager. You will enable Tomcat basic authentication by editing the ‘tomcat-users.xml’ file, then allowing access to both Tomcat host-manager and manager through the ‘tomcat10-admin’ package.

Open the Tomcat user configuration ‘/etc/tomcat10/tomcat-users.xml’ using the nano editor.

sudo nano /etc/tomcat10/tomcat-users.xml

Insert the following configuration between the ‘<tomcat-users…> …. </tomcat-users>’. In this example, you will create Apache Tomcat user ‘admin‘ with the password ‘tomcatadmin‘ and multiple roles including ‘admin-gui’ and ‘manager-gui’.

<tomcat-users>

<!--
user: admin pass: tomcatadmin
-->
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="admin" password="tomcatadmin" roles="manager-gui, manager-script, manager-jmx, manager-status, admin-gui, admin-script"/>

</tomcat-users>

When finished, save and exit the file.

Now open the configuration for the Tomcat ‘host-manager‘ and ‘manager‘ with the nano editor command below.

sudo nano /usr/share/tomcat10-admin/host-manager/META-INF/context.xml
sudo nano /usr/share/tomcat10-admin/manager/META-INF/context.xml

Comment on the following configuration by adding ‘<!– … –>’. With this, you will allow remote access to the Apache Tomcat ‘host-manager‘ and ‘manager‘.

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

Save and exit the file.

After editing, run the following command to restart the ‘tomcat10’ service and apply the new changes to your Apache Tomcat installation.

sudo systemctl restart tomcat10

Lastly, visit http://192.168.5.30:8080/host-manager/ to access the Apache Tomcat virtual host manager and http://192.168.5.30:8080/manager/ for the Tomcat manager.

Type the user ‘admin‘ and password ‘tomcatadmin‘ when prompted.

If authentication is successful, you should get the Apache Tomcat host-manager and manager page like the following:

Apache Tomcat manager.

Apache Tomcat host-manager.

Setting up Nginx as a reverse proxy

In this guide, you will install the Nginx web server and configure it as a reverse proxy for your Apache Tomcat installation. By using a reverse proxy, you can easily access your Tomcat application through your local domain name.

To install the Nginx web server on your Ubuntu, run the following:

sudo apt install nginx -y

When the installation is finished, create a new server block configuration ‘/etc/nginx/sites-available/tomcat10‘ that will be used as a reverse proxy for Apache Tomcat.

sudo nano /etc/nginx/sites-available/tomcat10

Add the configuration below and change your local domain name using the server_name option.

server {
listen 80;

server_name tomcat10.hwdomain.io;
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 exit the file when finished.

Now run the command below to activate the ‘tomcat10’ server block configuration and test your Nginx syntax.

sudo ln -s /etc/nginx/sites-available/tomcat10 /etc/nginx/sites-enabled/
sudo nginx -t

If you have correct and proper Nginx syntax, you will see an output ‘syntax is ok … test is successful‘.

Next, run the following command to restart the Nginx service and apply the ‘tomcat10’ server block. Then, verify the Nginx service status to ensure it is running.

sudo systemctl restart nginx
sudo systemctl status nginx

The output below shows that the Nginx web server is running. And you can access your Apache Tomcat installation using a domain name such as ‘tomcat10.hwdomain.io‘.

You can add the following configuration to the ‘hosts’ file on your local machine. For Linux or Mac users, add the following to the ‘/etc/hosts‘ file. As for Windows users, add the configuration below to the ‘C:\Windows\System32\drivers\etc\hosts‘ file.

192.168.5.30 tomcat10.hwdomain.io

Lastly, visit http://tomcat10.hwdomain.io using your web browser. If your Nginx reverse proxy installation is successful, you will see the Apache Tomcat index page like the following:

Visit http://tomcat10.hwdomain.io/host-manager/ to access the Apache Tomcat host-manager and http://tomcat10.hwdomain.io/manager/ to access the Apache Tomcat manager. You will see both applications running under the Nginx reverse proxy.

Conclusion

Congratulations! You have successfully installed Apache Tomcat on Ubuntu 24.04 server. You’ve installed Tomcat 10 with the Java OpenJDK 21 on Ubuntu. Also, you have installed and configured Nginx as a reverse proxy for Apache Tomcat.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular