Configure Nginx to Host Secured Tomcat Application with WordPress on the Same Ubuntu Server

Published Updated

10 min read

Run a Java app on Apache Tomcat and a WordPress site on the same Ubuntu server, both over HTTPS. One Nginx instance terminates SSL on port 443, serves WordPress through PHP-FPM and forwards the Java traffic to Tomcat on localhost. Tested on Ubuntu 24.04 and 22.04.


The short version: Nginx is the only thing listening on ports 80 and 443. WordPress runs through PHP-FPM, Tomcat listens on 127.0.0.1:8080, and Nginx picks the right backend by domain name. Certbot gets a Let's Encrypt certificate for each domain.

shell

1sudo apt install nginx php-fpm php-mysql mysql-server tomcat10
2sudo certbot --nginx -d example.com -d www.example.com
3sudo certbot --nginx -d app.example.com

That's the skeleton. Below is every step, with the config files, the Tomcat settings most guides skip, and fixes for the errors you're likely to hit. All commands were tested on Ubuntu 24.04 and 22.04 with a local test ACME server in place of Let's Encrypt.

How the setup works

You can't bind two web servers to port 443 on one IP address. So instead of giving Tomcat its own SSL port, put Nginx in front of everything and let it handle SSL:

  • Nginx listens on ports 80 and 443, holds both certificates and redirects HTTP to HTTPS.
  • WordPress at example.com runs on PHP-FPM. Nginx talks to it through a Unix socket.
  • Tomcat at app.example.com listens only on 127.0.0.1:8080. Nginx forwards requests to it with proxy_pass, so port 8080 is never open to the internet.

Tomcat speaks plain HTTP behind Nginx. You don't need a keystore, a Java SSL connector or a second certificate format.

Requirements

  • An Ubuntu 24.04 server (22.04 works with a few changes, see below) with a user that can run sudo.
  • Two domain names pointing to the server's IP. In this guide that's example.com (plus www.example.com) for WordPress and app.example.com for Tomcat. Replace them with yours everywhere.
  • Ports 80 and 443 open in the firewall and in your cloud provider's security group. Let's Encrypt checks your domain over port 80, so keep it open.

Update the server first:

shell

1sudo apt update
2sudo apt upgrade

Install the packages

Step 1: Install Nginx, PHP-FPM and MySQL

Install Nginx, PHP-FPM with the extensions WordPress needs, and MySQL:

shell

1sudo apt install nginx php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl mysql-server

On Ubuntu 24.04 this gives you Nginx 1.24, PHP 8.3 and MySQL 8.0. All three services start right after the installation. MariaDB works too if you prefer it: install mariadb-server instead of mysql-server, and the SQL below stays the same.

Older guides add php-mcrypt to this list. Leave it out: the mcrypt extension was removed from PHP in version 7.2, and Ubuntu no longer has the package, so apt would refuse to install the whole list.

Step 2: Install Tomcat

On Ubuntu 24.04, install Tomcat 10.1 from the Ubuntu repository:

shell

1sudo apt install tomcat10

The package pulls in Java 21, creates a tomcat system user and a systemd service, and gets updates through apt along with the rest of the system. Its config lives in /etc/tomcat10, and web apps go to /var/lib/tomcat10/webapps.

Check that it answers locally:

shell

1curl -s http://127.0.0.1:8080/ | grep '<h1>'

You should see the default Tomcat page:

shell

1<h1>It works !</h1>

If you need Tomcat 11 or the very latest 10.1 release on the day it comes out, use the tarball from the Apache Tomcat site instead. The Nginx part of this guide doesn't change, but you'll create the user and the systemd unit yourself and update Tomcat by hand.

Put Tomcat behind Nginx

Step 3: Bind Tomcat to localhost

By default Tomcat listens on port 8080 on every network interface, so anyone can skip Nginx and open the app over plain HTTP. It also means the same content lives at two addresses. Bind Tomcat to localhost so only Nginx can reach it.

Open /etc/tomcat10/server.xml and find the HTTP connector:

html

1<Connector port="8080" protocol="HTTP/1.1"

Add the address attribute:

html

1<Connector address="127.0.0.1" port="8080" protocol="HTTP/1.1"

Leave the rest of the connector as it is. You can find all connector options in the Tomcat HTTP connector reference.

Step 4: Tell Tomcat it runs behind a proxy

Behind Nginx, Tomcat sees every request as plain HTTP from 127.0.0.1. Your app then thinks it's on http://, logs the wrong client IP and may build http:// links and redirects on an HTTPS site. In our test, a page that prints the request details showed this over HTTPS:

shell

1scheme=http secure=false host=app.example.com port=80 remote=127.0.0.1

The Remote IP Valve fixes that. It reads the X-Forwarded-For and X-Forwarded-Proto headers that Nginx sends. In the same server.xml, add it inside the <Host> element, right before the closing </Host> tag:

html

1<Valve className="org.apache.catalina.valves.RemoteIpValve"
2 remoteIpHeader="X-Forwarded-For"
3 protocolHeader="X-Forwarded-Proto" />

The valve trusts these headers only from internal addresses such as 127.0.0.1, so a client can't fake them by going around Nginx. Details are in the Tomcat valve reference.

Restart Tomcat and check what it listens on:

shell

1sudo systemctl restart tomcat10
2sudo ss -ltnp | grep 8080

shell

1LISTEN 0 100 [::ffff:127.0.0.1]:8080 *:* users:(("java",pid=13283,fd=39))

[::ffff:127.0.0.1] is how Java shows an IPv4 address on an IPv6 socket. It's localhost only. A request to the server's public IP on port 8080 now gets Connection refused.

To serve your own application at the root of app.example.com, deploy it as ROOT.war:

shell

1sudo systemctl stop tomcat10
2sudo rm -rf /var/lib/tomcat10/webapps/ROOT
3sudo cp myapp.war /var/lib/tomcat10/webapps/ROOT.war
4sudo systemctl start tomcat10

Prepare WordPress

Step 5: Create the database

On Ubuntu, the MySQL root user logs in through the system account, without a password. Open the MySQL shell with sudo:

shell

1sudo mysql

Create a database and a separate user for WordPress. Use a strong password of your own:

shell

1CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'change-this-password';
3GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost';
4EXIT;

MySQL listens only on 127.0.0.1 by default, so the database isn't reachable from outside. If you want to remove the test database and set other basic options, run sudo mysql_secure_installation.

Step 6: Download WordPress

Get the latest WordPress and put it in the site's root directory:

shell

1cd /tmp
2curl -O https://wordpress.org/latest.tar.gz
3tar -xzf latest.tar.gz
4sudo mv wordpress /var/www/example.com
5sudo chown -R www-data:www-data /var/www/example.com

Don't create wp-config.php by hand. The web installer will ask for the database details in Step 12 and write the file itself, with unique security keys.

Step 7: Raise the PHP upload limit

PHP accepts uploads of up to 2 MB by default, which is too small for most WordPress media. Raise it with a separate config file, so package updates don't overwrite it:

shell

1printf "upload_max_filesize = 64M\npost_max_size = 64M\n" | sudo tee /etc/php/8.3/fpm/conf.d/99-wordpress.ini
2sudo systemctl restart php8.3-fpm

Keep this value in line with client_max_body_size in the Nginx config below.

Configure Nginx

Start with plain HTTP server blocks. Certbot will add HTTPS to them in Step 11. If you write the SSL lines first, nginx -t fails because the certificate files don't exist yet.

Step 8: Create a server block for WordPress

Create /etc/nginx/sites-available/example.com:

nginx

1server {
2 listen 80;
3 listen [::]:80;
4 server_name example.com www.example.com;
5
6 root /var/www/example.com;
7 index index.php;
8
9 # Allow media uploads up to 64 MB
10 client_max_body_size 64M;
11
12 location / {
13 try_files $uri $uri/ /index.php?$args;
14 }
15
16 location ~ \.php$ {
17 include snippets/fastcgi-php.conf;
18 fastcgi_pass unix:/run/php/php-fpm.sock;
19 }
20
21 location ~* \.(css|js|gif|ico|jpe?g|png|svg|webp|woff2?)$ {
22 expires 30d;
23 log_not_found off;
24 }
25
26 location ~ /\.ht {
27 deny all;
28 }
29}

A few notes on this config:

  • /run/php/php-fpm.sock is a link to the socket of the installed PHP version (php8.3-fpm.sock on 24.04, php8.1-fpm.sock on 22.04). The config keeps working after a PHP upgrade, and you won't end up with a stale path like php7.0-fpm.sock, which gives you a 502 error.
  • snippets/fastcgi-php.conf comes with the Ubuntu Nginx package. It passes the script path to PHP-FPM and returns 404 for PHP files that don't exist.
  • try_files sends pretty permalinks to index.php.

Step 9: Create a server block for Tomcat

Create /etc/nginx/sites-available/app.example.com:

nginx

1server {
2 listen 80;
3 listen [::]:80;
4 server_name app.example.com;
5
6 location / {
7 proxy_pass http://127.0.0.1:8080;
8
9 proxy_set_header Host $host;
10 proxy_set_header X-Real-IP $remote_addr;
11 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
12 proxy_set_header X-Forwarded-Proto $scheme;
13 }
14}

proxy_pass has no trailing slash, so Nginx passes the request path to Tomcat unchanged. The Host and X-Forwarded-* headers are what the Remote IP Valve from Step 4 reads. More options are in the Nginx proxy module docs.

Step 10: Enable both sites

Disable the default Nginx site, enable the new ones, test the config and reload Nginx:

shell

1sudo rm /etc/nginx/sites-enabled/default
2sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
3sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
4sudo nginx -t
5sudo systemctl reload nginx

nginx -t should print:

shell

1nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2nginx: configuration file /etc/nginx/nginx.conf test is successful

Now app.example.com shows the Tomcat page over HTTP, and example.com redirects to the WordPress setup page.

Get SSL certificates with Let's Encrypt

Step 11: Request a certificate for each site

Install Certbot as described in our guide on how to install Let's Encrypt for Nginx on Ubuntu. On a fresh server it's two commands:

shell

1sudo snap install --classic certbot
2sudo ln -s /snap/bin/certbot /usr/local/bin/certbot

Then get a certificate for each site. Certbot finds the matching server_name, adds the SSL settings and sets up the redirect from HTTP to HTTPS:

shell

1sudo certbot --nginx -d example.com -d www.example.com
2sudo certbot --nginx -d app.example.com

After that, the Tomcat server block looks like this (the WordPress one gets the same changes):

nginx

1server {
2 server_name app.example.com;
3
4 location / {
5 proxy_pass http://127.0.0.1:8080;
6
7 proxy_set_header Host $host;
8 proxy_set_header X-Real-IP $remote_addr;
9 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
10 proxy_set_header X-Forwarded-Proto $scheme;
11 }
12
13 listen [::]:443 ssl; # managed by Certbot
14 listen 443 ssl; # managed by Certbot
15 ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem; # managed by Certbot
16 ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem; # managed by Certbot
17 include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
18 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
19}
20server {
21 if ($host = app.example.com) {
22 return 301 https://$host$request_uri;
23 } # managed by Certbot
24
25 listen 80;
26 listen [::]:80;
27 server_name app.example.com;
28 return 404; # managed by Certbot
29}

The included options-ssl-nginx.conf turns on TLS 1.2 and 1.3 with a modern cipher list, so you don't need to write ssl_protocols and ssl_ciphers yourself. Certbot also sets up automatic renewal for both certificates. Check it with a dry run:

shell

1sudo certbot renew --dry-run

Check both apps over HTTPS

Step 12: Test the sites and finish the WordPress installer

Check the redirect and both sites from the command line:

shell

1curl -sI http://app.example.com/ | grep -i location
2curl -s https://app.example.com/ | grep '<h1>'
3curl -sI https://example.com/ | grep -i location

shell

1Location: https://app.example.com/
2<h1>It works !</h1>
3Location: https://example.com/wp-admin/setup-config.php

Now open your WordPress domain over HTTPS in a browser and finish the WordPress installer. On the first screen, enter the database name, user and password from Step 5, and keep localhost as the database host. On the second, set the site title and the admin account.

WordPress detects HTTPS on its own and saves the HTTPS address as the site address, so you don't need to change any URL settings.

The installer creates wp-config.php readable and writable by everyone (mode 666). Tighten it right away:

shell

1sudo chmod 640 /var/www/example.com/wp-config.php

At this point both apps run on the same server behind one Nginx, each with its own Let's Encrypt certificate. Tomcat can only be reached through Nginx, and it knows the request came over HTTPS.

On Ubuntu 22.04

The steps are the same, with three differences:

  • Install tomcat9 instead of tomcat10, because Ubuntu 22.04 has no tomcat10 package. Use /etc/tomcat9/server.xml, /var/lib/tomcat9/webapps and sudo systemctl restart tomcat9. The connector and valve lines stay the same.
  • PHP is version 8.1, so the upload limit file goes to /etc/php/8.1/fpm/conf.d/ and the service is php8.1-fpm. The Nginx config stays the same thanks to the php-fpm.sock link.
  • Tomcat 9 runs Java EE apps (javax.* packages), and Tomcat 10 runs Jakarta EE apps (jakarta.*). An app built for one won't deploy on the other without changes. If you're starting fresh, Ubuntu 24.04 with Tomcat 10.1 is the better choice.

Alternative: Tomcat on a path of the same domain

If you have only one domain, you can serve the Java app from a path such as example.com/myapp/. Deploy it as myapp.war, so Tomcat serves it under /myapp/, and add this block to the WordPress server block, above location /:

nginx

1location ^~ /myapp/ {
2 proxy_pass http://127.0.0.1:8080;
3
4 proxy_set_header Host $host;
5 proxy_set_header X-Real-IP $remote_addr;
6 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
7 proxy_set_header X-Forwarded-Proto $scheme;
8}

The ^~ matters. Without it, the regex locations for .php files and static files win over a plain prefix, and requests like /myapp/style.css go to the WordPress folder instead of Tomcat.

In our test, the CSS file returned 404 without ^~ and 200 with it. The Nginx location docs explain the matching order.

Keep the path in Nginx the same as the app's context path in Tomcat. Mapping /app/ to a Tomcat app deployed as ROOT is possible, but then you have to rewrite redirects and cookie paths as well. A subdomain is simpler.

Troubleshooting

502 Bad Gateway on the WordPress site. Nginx can't reach PHP-FPM. Look at the last line of /var/log/nginx/error.log. A message like connect() to unix:/run/php/php7.0-fpm.sock failed (2: No such file or directory) means the socket path in the config is wrong. Use /run/php/php-fpm.sock or check the real name with ls /run/php/. If the path is right, make sure PHP-FPM is running: sudo systemctl status php8.3-fpm.

502 Bad Gateway on the Tomcat site. The log says connect() failed (111: Connection refused) while connecting to upstream. Tomcat is stopped, still starting (give it a few seconds after a restart) or listening on another address. Check with sudo systemctl status tomcat10 and sudo ss -ltnp | grep 8080.

The Java app redirects to http:// or logs 127.0.0.1 as the client IP. The Remote IP Valve from Step 4 is missing, or Nginx doesn't send X-Forwarded-Proto. Check both and restart Tomcat.

nginx -t fails with SSL_CTX_set_cipher_list(...) failed (SSL: error:0A000118:SSL routines::invalid command). Your ssl_ciphers value is split across several lines. OpenSSL doesn't accept line breaks in the cipher list. Put the whole list on one line, separated by colons, or use the defaults from Certbot's options-ssl-nginx.conf. The TLSRef Configurator, the successor of the Mozilla SSL Configuration Generator, gives you a ready list if you need your own.

413 Request Entity Too Large when uploading to WordPress. The file is bigger than client_max_body_size. If WordPress says the file exceeds the maximum upload size, it's the PHP limit from Step 7.

Certbot can't verify the domain. Make sure the DNS record points to this server and port 80 is open, including in the cloud security group. More causes are covered in the Let's Encrypt guide.

Wrapping up

One Nginx in front of everything is the simplest way to run WordPress and a Java app on the same Ubuntu server. You get one place for certificates, one place for redirects, and no Java keystores. The two details people miss are binding Tomcat to 127.0.0.1 and adding the Remote IP Valve, so the app knows it runs over HTTPS.

From here, follow the WordPress hardening guide for the WordPress side. If you'd rather have someone else run and secure servers like this one, take a look at our cloud engineering services.

Alex Kondratiev

Alex Kondratiev

Founder and CEO of ITsyndicate. DevOps engineer with 15+ years in cloud infrastructure, Infrastructure as Code, Kubernetes and automation, focused on secure, scalable and resilient systems.

More about Alex

An Infrastructure Review Should Give You a Plan, Not a Dependency

Why we want the review to be useful even if your own team does all the work afterward. Of course we want to help with the implementation. We are an engineering company. We like solving infrastructure problems, and we want relationships where we can take responsibility for the work over time. So why offer an Infrastructure Review that a client can take back to their own team? Because I want the first engagement to earn its fee on its own. The client should leave with a better understanding of their situation, clear priorities, and a practical next step. They can then decide whether they want us to carry out the changes. That is the principle behind how we are building our Infrastructure Review. It shapes what we agree to investigate, how we explain our findings, and what the client can do with them afterward.

Your Infrastructure Can Be Expensive Even When Nothing Is Broken

A quiet production environment can still hide rising costs, wasted engineering capacity, and risks that appear only during growth or recovery. A familiar weekly update sounds like this: production is stable, the latest release went out, and there were no serious customer complaints. The cloud bill is higher again, but there is probably a reasonable explanation. One engineer spent half a day helping with the release, another fixed a monitoring issue, and the database may need attention next quarter. Nothing is broken badly enough to become a leadership problem. That is exactly why the underlying problem can survive for so long. I have seen this pattern more than once. Production looks stable, while senior engineers quietly spend much of their time keeping it that way. The platform works, but the company is paying for that stability through cloud spend, manual effort, slower releases, and assumptions about growth or recovery that nobody has tested recently. Infrastructure doesn’t need to cause an outage to become expensive. Outages are obvious. Inefficiency is quieter, and it can keep working for years while charging you for the privilege.

How to Run a Docker Container on Your Local Machine

This article is a quick, hands-on guide to running Docker locally. It walks through essential CLI commands for managing images and containers (build, pull, run, ps, start/stop, rm/rmi) and shows how to automate image creation with a Dockerfile. You’ll build an image, run a container with port mapping, verify it’s running, and learn tips for environment setup - everything you need to spin up and manage local containers efficiently.

Stay online.
Stay in command.