Apache Tomcat is a free, open-source Java servlet that provides an HTTP web server environment for running Java applications. It is simple, easy to use, and one of the most widely used web servers in the world. Tomcat acts as a development server that can be used to test and build applications that use JSF 2, servlets/JSP, or other Java-based dynamic web technologies.
In this tutorial, we will explain how to install the latest version of Apache Tomcat using an Ubuntu 24.04 Cloud server.
Requirements
- A Cloud VPS server running Ubuntu 24.04 or later.
- A static IP configured on your server.
- A root password configured on your VPS server in Spain.
Install Java
Apache Tomcat requires Java version 8 or later. If you do not have it installed, you can install it with the following command:
# apt install default-jdk -yOnce installed, check the Java version with the following command:
# java --versionYou should see output similar to this:
openjdk 21.0.3 2024-04-16
OpenJDK Runtime Environment (build 21.0.3+9-Ubuntu-1ubuntu1)
OpenJDK 64-Bit Server VM (build 21.0.3+9-Ubuntu-1ubuntu1, mixed mode, sharing)Download Tomcat
Before you begin, install the unzip extraction utility:
# apt update && apt install unzipYou will need to create a user and assign the appropriate permissions:
# useradd -m -d /opt/tomcat -U -s /bin/false tomcatNext, download your desired version of Apache Tomcat. You can check the available versions on the official Tomcat website. Here is an example using the latest available version:
# wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.23/bin/apache-tomcat-10.1.23.zipOnce downloaded, extract the archive with the following command:
# unzip apache-tomcat-10.1.23.zip && mv apache-tomcat-10.1.23/* /opt/tomcatThen, change the owner of the /opt/tomcat directory to Tomcat with the following command:
# chown -R tomcat:tomcat /opt/tomcat/
# chmod -R 755 /opt/tomcat/Open the Tomcat configuration file:
# nano /opt/tomcat/conf/tomcat-users.xmlAdd the following lines above the </tomcat-users> line:
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="your-password" roles="manager-gui,admin-gui"/>You will now have an administrator account for accessing Tomcat.
Create the systemd Service File for Tomcat
Next, you will need to create a systemd service file to manage the Tomcat service. You can create it with the following command:
# nano /etc/systemd/system/tomcat.serviceAdd the following lines:
[Unit]
Description=Tomcat servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/default-java"
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.targetSave and close the file. Then, reload the systemd daemon and start the Tomcat service with the following commands:
# systemctl daemon-reload
# systemctl start tomcatYou can also check the Tomcat service with the following command:
# systemctl status tomcatYou should see the following output:
● tomcat.service - Tomcat servlet container
Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; preset: enabled)
Active: active (running) since Thu 2024-05-09 10:27:00 CEST; 6s ago
Process: 3926 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 3933 (java)
Tasks: 30 (limit: 4614)
Memory: 122.0M (peak: 122.4M)
CPU: 3.583s
CGroup: /system.slice/tomcat.service
└─3933 /usr/lib/jvm/default-java/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.propert>
May 09 10:27:00 amb-ubu24 systemd[1]: Starting tomcat.service - Tomcat servlet container...
May 09 10:27:00 amb-ubu24 startup.sh[3926]: Tomcat started.
May 09 10:27:00 amb-ubu24 systemd[1]: Started tomcat.service - Tomcat servlet container.Access the Tomcat Web Interface
Note: you will need to open port 8080 in the client panel firewall.
Tomcat is now installed and running on port 8080. Open your web browser and go to the URL http://your-server-ip:8080. You will be redirected to the Tomcat dashboard as shown below:
If you try to browse any of the sections, access will be denied with a 403 error. This is because Apache Tomcat can only be managed from the local machine by default, rather than remotely. We will resolve this by configuring nginx as a reverse proxy.
Configure NGINX as a Reverse Proxy
You can also configure NGINX as a reverse proxy for the Tomcat server. Nginx will then accept requests on port 80 in front of the Tomcat server.
First, install the NGINX web server with the following command:
# apt install nginx -yOnce installed, create a new virtual host configuration file for Tomcat.
# nano /etc/nginx/sites-available/tomcat.confAdd the following lines:
upstream tomcat {
server 127.0.0.1:8080 weight=100 max_fails=5 fail_timeout=5;
}
server {
listen 80;
server_name tu-dominio.com;
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://tomcat/;
}
}Save and close the file. Then, enable the NGINX web server's virtual host file to apply the configuration:
# ln -s /etc/nginx/sites-available/tomcat.conf /etc/nginx/sites-enabled/
# systemctl restart nginxOpen your web browser and go to the URL http://tu-dominio.com. Now, click Manager App or Host Manager. You will be prompted to enter a username and password as shown on the following screen:
Enter your admin username and click the Sign In button. You will be redirected to the Manager App or Host Manager dashboard:
Obtain a Let's Encrypt SSL Certificate
If you now need an SSL certificate, simply install Let's Encrypt using this command:
# apt install certbot python3-certbot-nginxThen configure the certificate with this command:
# certbot --nginx -d ejemplo.com -d www.ejemplo.comYou can find more information in our article: Obtain Let's Encrypt Certificates with Certbot
Congratulations! You can now access the Tomcat server through the URL http://ejemplo.com without specifying Tomcat port 8080.
Remember, if you have any questions about this or any other issue related to your servers on Clouding, feel free to email soporte@clouding.io We're here to help with whatever you need. Get in touch!