Apache Tomcat is a free and open-source web server and servlet container that is used to host Java-based web applications. Tomcat is known for its performance and flexibility, and it is widely used in production environments to serve dynamic web content. Nginx is a high-performance web server and reverse proxy that is often used in conjunction with Tomcat to provide additional features and scalability.
In this tutorial, we will show you how to install and configure Apache Tomcat 10 with Nginx on Rocky Linux 8. Before starting, make sure that you have a Rocky Linux 8 server with a non-root user with sudo privileges and a basic firewall configured.
Start by updating the package index and installing the required dependencies:
sudo dnf update
sudo dnf install java-1.8.0-openjdk-devel
Next, download the latest version of Apache Tomcat 10 from the Apache Tomcat website and extract the downloaded archive to the desired location:
wget <https://downloads.apache.org/tomcat/tomcat-10/v10.0.6/bin/apache-tomcat-10.0.6.tar.gz>
tar xvzf apache-tomcat-10.0.6.tar.gz
After extracting the Tomcat archive, create a new system user and group to run the Tomcat service:
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Next, change the ownership of the Tomcat installation directory to the tomcat user and group:
sudo chown -R tomcat:tomcat /opt/tomcat
Now, create a new system unit file for the Tomcat service by creating a new file using your favorite text editor:
sudo vi /etc/systemd/system/tomcat.service
Paste the following content into the file:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="JAVA_OPTS=-Djava.security.egd=file:/dev/./urandom"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
Save the file and close the editor. Then, start the Tomcat service and enable it to start automatically on boot:
sudo systemctl start tomcat
sudo systemctl enable tomcat
To verify that Tomcat is running and functioning properly, open a web browser and access the Tomcat manager web application at the following URL:
http://<server_public_ip>:8080/manager/html
You should see the Tomcat manager login page. Use the default username and password (admin/admin) to log in and access the Tomcat manager dashboard.
Now that Tomcat is running, we can install and configure Nginx to act as a reverse proxy for Tomcat. Start by installing Nginx from the Rocky Linux package repository:
sudo dnf install nginx
After the installation is complete, start the Nginx service and enable it to start automatically on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
To configure Nginx as a reverse proxy for Tomcat, create a new virtual host configuration file for Tomcat using your favorite text editor:
sudo vi /etc/nginx/conf.d/tomcat.conf
Paste the following content into the file:
upstream tomcat {
server 127.0.0.1:8080 fail_timeout=0;
}
server {
listen 80;
server_name <server_public_ip_or_hostname>;
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://tomcat/>;
}
}
Make sure to replace <server_public_ip_or_hostname
> with the server’s public IP address or hostname. This configuration file defines a new upstream group for Tomcat, and a new virtual host that listens on port 80
and proxies requests to the Tomcat server.
Save the file and close the editor. Then, test the Nginx configuration and restart the service if the configuration is valid:
sudo nginx -t
sudo systemctl restart nginx
At this point, Apache Tomcat 10 should be running behind Nginx on your Rocky Linux 8 server. You can now access Tomcat applications through the Nginx reverse proxy using the server’s public IP address or hostname.
To test the setup, open a web browser and access the Tomcat manager web application at the following URL:
http://<server_public_ip_or_hostname>/manager/html
You should see the Tomcat manager login page, and you can log in and access the Tomcat manager dashboard as before. This time, however, the requests are being proxied through Nginx, which provides additional features and scalability for Tomcat.
In this tutorial, we have shown you how to install and configure Apache Tomcat 10 with Nginx on Rocky Linux 8. We have also shown you how to create a new system user and group for Tomcat, and how to create a new virtual host configuration for Nginx to proxy requests to Tomcat.
We hope this tutorial has been helpful and you can now use Apache Tomcat and Nginx together to host and serve dynamic Java-based web applications on Rocky Linux 8.