On this page
- Which Tomcat and Java versions should you pick?
- How do you install Java?
- How do you download and install Tomcat?
- How do you run Tomcat as a systemd service?
- How do you keep Tomcat off the public internet?
- How do you put Nginx in front with HTTPS?
- How do you deploy an application?
- How do you upgrade Tomcat?
- Questions
Which Tomcat and Java versions should you pick?
Tomcat 11 implements Jakarta EE 11 and needs Java 17 or newer; Tomcat 10.1 implements Jakarta EE 10 and needs Java 11 or newer. Both use the jakarta.* package names. Apps that still import javax.servlet belong on the older Tomcat 9 branch, or must be migrated first. If you are starting fresh, choose Tomcat 11 with Java 21, which is a long term support release and is packaged on almost every current system:
- Ubuntu 24.04 and 22.04:
openjdk-21-jdk-headless - Debian 13:
openjdk-21-jdk-headless(check withapt policy) - Debian 12:
openjdk-17-jdk-headless, which still meets Tomcat 11's minimum - Rocky Linux 9 and AlmaLinux 9:
java-21-openjdk-headless
The OpenJDK guide covers switching between several installed Java versions.
How do you install Java?
Install the headless JDK, since a server needs no graphics libraries, and check the version:
# Ubuntu, Debian 13
sudo apt update
apt policy openjdk-21-jdk-headless
sudo apt install -y openjdk-21-jdk-headless curl
# Debian 12
sudo apt install -y openjdk-17-jdk-headless curl
# Rocky Linux 9 and AlmaLinux 9
sudo dnf install -y java-21-openjdk-headless tar curl
java -version
readlink -f /usr/bin/java | sed "s:/bin/java::"The last line prints the Java home directory, for example /usr/lib/jvm/java-21-openjdk-amd64 on Ubuntu. Note it down for the service file.
How do you download and install Tomcat?
Tomcat is not packaged in a current version on most distributions, so the Apache binary archive is the usual route. Create a system user without a login shell, then fetch the newest 11.0 release and verify it against the checksum Apache publishes:
sudo useradd -r -m -U -d /opt/tomcat -s /usr/sbin/nologin tomcat
VER=$(curl -s https://dlcdn.apache.org/tomcat/tomcat-11/ | grep -oE "v11\.0\.[0-9]+" | sort -V | tail -n 1 | cut -c2-)
echo "$VER"
cd /tmp
curl -fLO https://dlcdn.apache.org/tomcat/tomcat-11/v$VER/bin/apache-tomcat-$VER.tar.gz
curl -fLO https://downloads.apache.org/tomcat/tomcat-11/v$VER/bin/apache-tomcat-$VER.tar.gz.sha512
sha512sum -c apache-tomcat-$VER.tar.gz.sha512
sudo tar xzf apache-tomcat-$VER.tar.gz -C /opt/tomcat --strip-components=1
sudo chown -R tomcat:tomcat /opt/tomcat
sudo chmod -R u+x /opt/tomcat/binThe checksum line must end in OK. For Tomcat 10.1, replace tomcat-11 with tomcat-10 and v11\.0 with v10\.1 in the commands.
How do you run Tomcat as a systemd service?
Write a unit that starts Tomcat in the foreground under the tomcat user, so systemd tracks the process and restarts it on failure. Create /etc/systemd/system/tomcat.service:
[Unit]
Description=Apache Tomcat
After=network.target
[Service]
Type=simple
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_OPTS=-Xms256m -Xmx512m"
ExecStart=/opt/tomcat/bin/catalina.sh run
SuccessExitStatus=143
Restart=on-failure
NoNewPrivileges=true
[Install]
WantedBy=multi-user.targetSet JAVA_HOME to the path you noted, and size -Xmx to leave room for the system and Nginx: about half the RAM is a sensible starting point on a small server. Start it and check:
sudo systemctl daemon-reload
sudo systemctl enable --now tomcat
systemctl status tomcat --no-pager
curl -sI http://127.0.0.1:8080/ | head -n 1A 200 status means Tomcat answered.
How do you keep Tomcat off the public internet?
Bind the HTTP connector to the loopback address so only Nginx on the same server can reach it. Edit /opt/tomcat/conf/server.xml, find the connector on port 8080, and add an address attribute:
<Connector port="8080" address="127.0.0.1" protocol="HTTP/1.1"
connectionTimeout="20000" redirectPort="8443"
maxParameterCount="1000" />While in that file, add a RemoteIpValve inside the <Host> element so Java apps see the real client address and know the request came in over HTTPS:
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="X-Forwarded-For"
protocolHeader="X-Forwarded-Proto" />Restart with sudo systemctl restart tomcat and confirm with sudo ss -tlnp | grep 8080 that it shows 127.0.0.1:8080. The shutdown port 8005 already listens on localhost only. Do not open 8080 in UFW or firewalld; only 80 and 443 should be public, as set up in the firewall tutorial.
How do you put Nginx in front with HTTPS?
Install Nginx and Certbot, then add a server block that forwards everything to Tomcat:
server {
listen 80;
listen [::]:80;
server_name app.example.net;
client_max_body_size 50m;
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 it as /etc/nginx/sites-available/app.example.net and link it into sites-enabled on Ubuntu and Debian, or as /etc/nginx/conf.d/app.example.net.conf on Rocky and Alma. On Rocky and Alma, SELinux blocks Nginx from opening network connections by default, so allow it once:
sudo setsebool -P httpd_can_network_connect 1
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d app.example.netThe Nginx reverse proxy guide lists timeouts and buffering options worth tuning for slow Java endpoints.
How do you deploy an application?
Copy a WAR file into webapps; Tomcat unpacks and starts it automatically. The file name becomes the path, and a file named ROOT.war takes the site root:
sudo rm -rf /opt/tomcat/webapps/ROOT
sudo cp myapp.war /opt/tomcat/webapps/ROOT.war
sudo chown tomcat:tomcat /opt/tomcat/webapps/ROOT.war
sudo tail -f /opt/tomcat/logs/catalina.outWith the systemd unit above, Tomcat's console output also goes to the journal, so journalctl -u tomcat -f shows startup messages and stack traces. If you want the Manager web app for deployments, add a user with the manager-gui role in conf/tomcat-users.xml. Its default context only accepts requests from localhost, so reach it through an SSH tunnel with ssh -L 8080:127.0.0.1:8080 deploy@198.51.100.24 and leave that restriction in place.
How do you upgrade Tomcat?
Keep your configuration and apps, replace the program files. A practical pattern is to unpack each new release next to the old one and copy over conf and webapps:
sudo systemctl stop tomcat
sudo mv /opt/tomcat /opt/tomcat-old
sudo mkdir /opt/tomcat
sudo tar xzf /tmp/apache-tomcat-$VER.tar.gz -C /opt/tomcat --strip-components=1
sudo cp -a /opt/tomcat-old/conf/. /opt/tomcat/conf/
sudo cp -a /opt/tomcat-old/webapps/. /opt/tomcat/webapps/
sudo chown -R tomcat:tomcat /opt/tomcat
sudo systemctl start tomcatCompare the new release's default server.xml with yours for new attributes before you copy blindly, then remove /opt/tomcat-old once the site works. Java itself updates through the normal package manager.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Apache Tomcat for Java apps
Why not use the distro tomcat package?
Some distributions ship one, but often an older branch. The Apache archive gives you the current release on every system and the same layout everywhere.
My app fails with ClassNotFoundException for javax.servlet. Why?
The app was built for Java EE, which Tomcat 10 and 11 no longer support. Rebuild it against Jakarta EE or run it on Tomcat 9.
How much RAM does a Tomcat server need?
It depends on the app. A small app runs in a 512 MB heap; large frameworks want 1 to 2 GB or more. Leave memory for the operating system on top of the heap.
Can Tomcat serve HTTPS itself?
Yes, with an HTTPS connector and a certificate file, but Nginx in front makes renewals with Certbot simpler and adds caching and compression.
Where are the logs?
In /opt/tomcat/logs/ (catalina.*.log, access logs) and in the journal for console output: journalctl -u tomcat.
More in Web servers and proxies
- Install and configure Nginx
- Install and configure Apache
- OpenLiteSpeed web server
- HAProxy load balancer and TLS proxy
- Load balancing with Nginx upstreams
- Nginx as a reverse proxy for an app
- Apache as a reverse proxy
- Compression with gzip and Brotli
- HTTPS and www redirects
- Turn off Apache directory listings
- Nginx Proxy Manager with Docker
- Nginx UI web interface
- Traefik for Docker containers
- Encrypted FTP with vsftpd (FTPS)
- Varnish cache in front of a site