Learn how to install Java and Apache Tomcat, understand its directory structure, deploy a basic web application, and put Nginx in front of it as a reverse proxy with proper firewall rules.
Prerequisites
- A server running Ubuntu, Debian, AlmaLinux, Rocky Linux or Fedora with root or sudo access
- At least 1GB RAM and 10GB free disk space
- A domain name pointed at your server's IP address (optional but recommended)
- Basic familiarity with the Linux command line
What this solves
Apache Tomcat is the standard way to run Java web applications on Linux. This tutorial walks you through installing Java and Tomcat, understanding its default layout and ports, deploying a sample application, and placing Nginx in front of it as a reverse proxy so you can serve Tomcat on standard HTTP ports with a proper domain.
By the end you will have Tomcat running as a systemd service, Nginx forwarding traffic to it, and a firewall configured to only expose the ports you actually need.
Step-by-step installation
Install Java
Tomcat runs on the Java Virtual Machine, so you need a JDK installed first. OpenJDK 17 is a solid, widely supported choice for Tomcat 10 and 11.
sudo apt update
sudo apt install -y openjdk-17-jdksudo dnf install -y java-17-openjdk java-17-openjdk-develVerify the Java installation
Confirm the JDK is installed and check the version before moving on.
java -versionCreate a dedicated Tomcat user
Tomcat should never run as root. Create a system account with no login shell to own the Tomcat files and process.
sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcatDownload and extract Tomcat
Download the latest Tomcat 10 release directly from the Apache archive and extract it into /opt/tomcat.
cd /tmp
curl -O https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.33/bin/apache-tomcat-10.1.33.tar.gz
sudo tar xzf apache-tomcat-10.1.33.tar.gz -C /opt/tomcat --strip-components=1Set correct ownership and permissions
Tomcat needs its scripts to be executable by the tomcat user, and its config files should not be world writable. Never use chmod 777 to work around permission errors, it gives every user on the system full read, write and execute access to your application files.
sudo chown -R tomcat:tomcat /opt/tomcat
sudo chmod -R 750 /opt/tomcat
sudo chmod -R +x /opt/tomcat/binCreate a systemd service
Running Tomcat under systemd gives you automatic restarts, boot-time startup, and standard log handling through journald.
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target/usr/lib/jvm/java-17-openjdk without an architecture suffix. Run sudo alternatives --config java to confirm the exact path on your system.Enable and start the Tomcat service
Reload systemd so it picks up the new unit file, then enable and start Tomcat.
sudo systemctl daemon-reload
sudo systemctl enable --now tomcat
sudo systemctl status tomcatIf the service fails to start, check the exit code and logs before troubleshooting further. The systemd 203/EXEC guide covers a common cause of startup failures related to incorrect paths or permissions.
Understanding the Tomcat directory structure and default ports
Everything Tomcat needs lives under CATALINA_HOME, which in this setup is /opt/tomcat. Knowing what each directory does makes troubleshooting much faster.
| Directory | Purpose |
|---|---|
bin | Startup and shutdown scripts |
conf | Configuration files including server.xml and web.xml |
webapps | Deployed web applications, one folder or WAR file per app |
logs | Catalina and access logs |
temp | Temporary files used at runtime |
work | Compiled JSP output and session data |
By default, Tomcat listens on port 8080 for HTTP and port 8005 for the shutdown command. Port 8080 is not meant to be exposed directly to the internet in production, which is exactly why you put Nginx in front of it.
Configuring a basic web application deployment
Deploy a sample application
Tomcat auto-deploys anything dropped into webapps. Create a minimal JSP page to confirm deployments work end to end.
sudo mkdir -p /opt/tomcat/webapps/demo
echo '<html><body><h1>Tomcat demo app is running</h1></body></html>' | sudo tee /opt/tomcat/webapps/demo/index.jsp
sudo chown -R tomcat:tomcat /opt/tomcat/webapps/demoSecure the manager and host-manager apps
The manager interface lets you deploy and undeploy applications through a browser. Restrict it to roles with real credentials instead of leaving it open.
<tomcat-users>
<role rolename="manager-gui"/>
<user username="tomcatadmin" password="Ch4ng3-This-Str0ng-Pass" roles="manager-gui"/>
</tomcat-users>Restart Tomcat after editing this file so the new credentials take effect.
sudo systemctl restart tomcatSetting up Nginx as a reverse proxy for Tomcat
Install Nginx
Nginx will sit in front of Tomcat, handling port 80 and 443 while forwarding requests internally to port 8080.
sudo apt install -y nginxsudo dnf install -y nginxCreate a reverse proxy server block
This configuration forwards all traffic for example.com to Tomcat on localhost port 8080, while preserving the original client IP and host headers.
server {
listen 80;
server_name example.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;
}
}Enable the site and test the configuration
On Debian and Ubuntu, symlink the config into sites-enabled. On RHEL-based distros, Nginx reads directly from conf.d.
sudo ln -s /etc/nginx/sites-available/tomcat.conf /etc/nginx/sites-enabled/tomcat.conf
sudo nginx -t
sudo systemctl reload nginxsudo cp /etc/nginx/sites-available/tomcat.conf /etc/nginx/conf.d/tomcat.conf
sudo nginx -t
sudo systemctl reload nginxFor production deployments you should terminate TLS at Nginx rather than serving plain HTTP. See the Tomcat SSL with Let's Encrypt tutorial once this basic setup is working, or the general Nginx SSL termination with Certbot guide for the Nginx side.
Basic firewall rules for web traffic
Allow only HTTP, HTTPS and SSH
Tomcat's port 8080 should never be reachable from outside the server once Nginx is in front of it. Only open the ports clients actually need.
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw statussudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-allVerify your setup
Check that Tomcat is running and listening locally, then confirm Nginx is forwarding requests correctly.
sudo systemctl status tomcat
sudo ss -tlnp | grep 8080
curl -I http://127.0.0.1:8080
curl -I http://example.com/demo/You should see an HTTP 200 response from the curl request against your domain, and the demo page content should load in a browser.
Common issues
| Symptom | Cause | Fix |
|---|---|---|
| 502 Bad Gateway from Nginx | Tomcat is not running or not listening on 8080 | Run sudo systemctl status tomcat and check /opt/tomcat/logs/catalina.out |
| Tomcat service fails with status 203/EXEC | Wrong JAVA_HOME path or script not executable | Verify the path with sudo alternatives --config java, see the 203/EXEC fix guide |
| 403 Forbidden on manager app | No roles configured in tomcat-users.xml | Add a manager-gui role and user, then restart Tomcat |
| Nginx returns 404 for deployed app | Wrong context path or app not deployed | Confirm the folder exists under /opt/tomcat/webapps and matches the URL path |
| Port 8080 reachable from the internet | Firewall allows all ports by default | Restrict inbound rules to 22, 80 and 443 only |
Next steps
- Setup Tomcat SSL certificates with Let's Encrypt automation
- Configure Tomcat database connection pooling with JNDI and HikariCP
- Implement Tomcat application monitoring with JMX and Grafana dashboards
- Setup Tomcat clustering with HAProxy load balancing for high availability
- Check and manage running services on Linux with systemctl