This comprehensive guide will walk you through the process of deploying Apache Tomcat with Nginx as a reverse proxy on Ubuntu 22.04. We’ll also provide a step-by-step installation guide of the Java JDK, which is a prerequisite for running Apache Tomcat.
Getting Started
Before we dive into the main process, let’s set the stage by understanding the prerequisites:
- A server running Ubuntu 22.04
- A valid domain name pointed with your server IP
- A root password is configured on the server
Java JDK Installation
Apache Tomcat is Java-based, which means we first need to ensure Java is installed on your server. If it isn’t, you can install it using the following command:
apt install default-jdk-y
You can verify the Java version by using:
java-version
The output should look something like this:
openjdk version"11.0.15"2022-04-19 OpenJDK RuntimeEnvironment(build11.0.15+10-Ubuntu-0ubuntu0.22.04.1) OpenJDK64-Bit ServerVM(build11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)
Apache Tomcat Deployment on Ubuntu 22.04
It’s recommended to run Tomcat as a separate user for security reasons. You can create a Tomcat user with this command:
useradd-m-d /opt/tomcat-U-s/bin/false tomcat
Next, download the Apache Tomcat latest version:
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.20/bin/apache-tomcat-10.0.20.tar.gz
Now, extract the downloaded file inside the /opt
directory, set proper ownership and permissions to the Tomcat directory:
tar xzvf apache-tomcat-10*tar.gz-C /opt/tomcat--strip-components=1 chown-R tomcat:tomcat/opt/tomcat/ chmod-R u+x/opt/tomcat/bin
Tomcat Administrative User Creation
By default, Tomcat can be accessed without any authentication. We suggest enabling authentication and creating an admin user for Tomcat:
nano /opt/tomcat/conf/tomcat-users.xml
Add the following lines above the line </tomcat-users>
:
<role rolename="admin-gui"/> <user username="admin" password="yourpassword" roles="manager-gui,admin-gui"/>
Enabling Tomcat Remote Access
By default, Tomcat is configured to access only from the local host. But, you might want to manage Tomcat from a remote host. To do so, edit the following file:
nano /opt/tomcat/webapps/manager/META-INF/context.xml
Remove this line:
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1"/>
Do the same for the host manager app:
nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
Creating a Service File for Apache Tomcat
Next, you’ll need to create a service file to manage the Tomcat service via systemd:
nano /etc/systemd/system/tomcat.service
Add the following lines:
[Unit] Description=Tomcat After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64" Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom" Environment="CATALINA_BASE=/opt/tomcat" Environment="CATALINA_HOME=/opt/tomcat" Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid" Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC" ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/opt/tomcat/bin/shutdown.sh [Install] WantedBy=multi-user.target
Reload the systemd daemon, start the Tomcat service and enable it to start at system reboot:
systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat
You can check the status of the Apache Tomcat using:
systemctl status tomcat
Configuring Nginx as a Reverse Proxy for Tomcat
Now, you’ll need to create an Nginx as a reverse proxy for Apache Tomcat. First, install the Nginx web server:
apt-get install nginx-y
Now, create an Nginx virtual host configuration file:
nano /etc/nginx/conf.d/tomcat.conf
Add the following lines:
server{ listen80; server_name tomcat.example.com; access_log/var/log/nginx/tomcat-access.log; error_log/var/log/nginx/tomcat-error.log; location/{ proxy_set_headerX-Forwarded-Host $host; proxy_set_headerX-Forwarded-Server $host; proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080/; } }
Verify the Nginx for any syntax error and restart the Nginx service:
nginx-t
systemctl restart nginx
Accessing Apache Tomcat
Finally, open your web browser and access the Apache Tomcat web interface using the URL http://tomcat.example.com
. You should see the Tomcat dashboard. Click on the Manager App and provide your admin username and password.
Conclusion
Congrats! You’ve successfully deployed Apache Tomcat with Nginx as a reverse proxy on Ubuntu 22.04. You’re now ready to host your first Java application using Apache Tomcat. Feel free to test this deployment with Shape.host Cloud Instances. If you have any questions, don’t hesitate to ask.