Apache Tomcat is a robust, open-source web server and Servlet container, ideal for hosting web applications developed in Java. Favored by web developers for its reliability in building and maintaining dynamic websites, Tomcat is an open-source project driven by the Apache Software Foundation. It enhances a web server’s capability to manage dynamic Java-based web content.
This guide provides step-by-step instructions to install Apache Tomcat on Ubuntu 22.04.
Prerequisites
- A server running Ubuntu 22.04.
- A valid domain name linked to your server’s IP.
- A root password configured on the server.
Install Java JDK
As Apache Tomcat operates as a Java-based application, ensure Java is installed on your server. If not, install it using:
apt install default-jdk -y
Verify the Java installation with:
java -version
Your output should resemble:
openjdk version "11.0.15" 2022-04-19 OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1) OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)
Install Apache Tomcat on Ubuntu 22.04
It’s best to run Tomcat under a distinct user. Create this user with:
useradd -m -d /opt/tomcat -U -s /bin/false tomcat
Download the latest Apache Tomcat version using:
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.20/bin/apache-tomcat-10.0.20.tar.gz
Extract it in the /opt directory:
tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1
Set appropriate ownership and permissions for the Tomcat directory:
chown -R tomcat:tomcat /opt/tomcat/ chmod -R u+x /opt/tomcat/bin
Create Tomcat Administrative User
To secure Tomcat, enable authentication and create an administrative user by editing:
nano /opt/tomcat/conf/tomcat-users.xml
Add before </tomcat-users>:
<role rolename="admin-gui" /> <user username="admin" password="yourpassword" roles="manager-gui,admin-gui" />
Enable Tomcat Remote Access
For remote access to Tomcat, modify context.xml files:
Edit and remove specific lines in:
nano /opt/tomcat/webapps/manager/META-INF/context.xml
nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
Create a Service File for Apache Tomcat
To manage Tomcat with systemd, create a service file:
nano /etc/systemd/system/tomcat.service
Include these configurations:
[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
Save the file and reload the systemd daemon:
systemctl daemon-reload
Start and enable Tomcat service:
systemctl start tomcat systemctl enable tomcat
Check if Apache Tomcat is running:
systemctl status tomcat
Configure Nginx as a Reverse Proxy for Tomcat
To set up Nginx as a reverse proxy, first install it:
apt-get install nginx -y
Create an Nginx virtual host configuration:
nano /etc/nginx/conf.d/tomcat.conf
server { listen 80; server_name tomcat.example.com; access_log /var/log/nginx/tomcat-access.log; error_log /var/log/nginx/tomcat-error.log; location / { proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080/; } }
Verify and restart Nginx:
nginx -t
systemctl restart nginx
Check Nginx status:
systemctl status nginx
Access Apache Tomcat
Access the Tomcat web interface using http://tomcat.example.com. The dashboard will display:
Select Manager App and authenticate:
You will see the Manager App dashboard:
To manage hosts, click Host Manager:
Click Server Status for Tomcat’s current status:
Conclusion
Well done! You’ve successfully installed Apache Tomcat, configured Nginx as a reverse proxy on Ubuntu 22.04, and are now set to deploy your Java applications. Reach out if you require assistance.
Frequently Asked Questions
Why use Tomcat instead of a full Java EE server?
Tomcat is lighter, starts faster, and is perfect for applications that don’t require the full EE stack. It provides essential web container functionalities including servlets and JSP.
Can Tomcat and Nginx run on the same server?
Absolutely, they can coexist as they use different ports. Tomcat typically uses port 8080, while Nginx listens on port 80 or 443 for HTTPS.
Is it mandatory to use Nginx as a reverse proxy?
Not strictly necessary, but recommended. A reverse proxy can offer additional security, SSL termination, and efficiently balance loads across multiple Tomcat servers.
How do I update Apache Tomcat?
Download the latest release, back up your configuration and webapps, replace the old binaries with the new ones, and then restore your configurations.