Introduction
Apache Tomcat is an open-source web server and servlet container. Nginx is a high-performance web server and reverse proxy. This tutorial will guide you through the process of installing Apache Tomcat with Nginx acting as a reverse proxy on Ubuntu 22.04.
Prerequisites
Before you begin, ensure you have:
- An Ubuntu 22.04 server
- Root or sudo privileges
Step 1: Install Apache Tomcat
Install Apache Tomcat using the following command:
sudo apt update
sudo apt install -y tomcat9
Step 2: Install Nginx
Install Nginx using the following command:
sudo apt install -y nginx
Step 3: Configure Apache Tomcat
Open the Tomcat server configuration file:
sudo nano /etc/tomcat9/server.xml
Update the following line to allow Tomcat to listen on localhost:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" address="127.0.0.1" />
Save and close the file.
Step 4: Configure Nginx as Reverse Proxy
Create a new Nginx configuration file for Tomcat:
sudo nano /etc/nginx/sites-available/tomcat
Add the following configuration to the file:
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Save and close the file. Create a symbolic link to enable the site:
sudo ln -s /etc/nginx/sites-available/tomcat /etc/nginx/sites-enabled/
Test Nginx configuration and restart the service:
sudo nginx -t
sudo systemctl restart nginx
Step 5: Access Apache Tomcat via Nginx
Open a web browser and navigate to your domain (e.g., http://your_domain.com) to access Apache Tomcat via Nginx.
Conclusion
Congratulations! You have successfully installed Apache Tomcat with Nginx acting as a reverse proxy on Ubuntu 22.04. You can now deploy and manage your Java web applications using Tomcat with the added benefits of Nginx as a reverse proxy.