
Apache Tomcat is an open-source web server application that allows developers to run Java applications in a secure and efficient environment. Tomcat implements Java Servlet and JavaServer Pages (JSP) specifications to deploy and manage Java-based web applications on a server.
Apache Tomcat supports load balancing, clustering, and high availability features to run applications with different workloads.
This article explains how to install Apache Tomcat on Ubuntu 24.04 to run dynamic web applications on a server.
Before you begin:
limited user login feature.tomcat.example.com.Apache Tomcat requires the Java Development Kit (JDK) version 17 or later. Follow the steps below to install the required OpenJDK version and create a new dedicated user to run Tomcat on your server.
Install OpenJDK 17 on your server.
View the installed Java version.
Output:
Create a new tomcat group to use with the Apache Tomcat service.
Create a new tomcat user with /opt/tomcat as the home directory and a member of the tomcat group.
Apache Tomcat is not available in the default package repositories on Ubuntu. Follow the steps below to download the latest Apache Tomcat release file and install the application on your server.
Visit the Apache Tomcat releases page and download the latest Apache Tomcat 11 release file.
The above command downloads the Tomcat release version 11.0.1 on your server. Ensure to download the latest version to install the latest application features and configurations.
Create a new tomcat directory in a system-wide location such as /opt to extract the Tomcat package contents.
Extract files from the downloaded Tomcat archive to the /opt/tomcat directory.
The --strip-components=1 option in the above command removes the top-level directory in the archive and extracts all Tomcat package contents to the /opt/tomcat directory.
Remove the downloaded archive file to free up the server's disk space.
Grant the tomcat user and group ownership privileges to the /opt/tomcat directory.
Grant the tomcat group read privileges to the conf directory.
Grant the tomcat group execute permissions to the /opt/tomcat/conf directory.
Apache Tomcat requires specific user accounts with administrative privileges to access the manager and host-manager applications in the web management dashboard. Follow the steps below to create privileged users and remove IP address restrictions to enable access to the manager and host-manager applications.
Open the tomcat-users.xml user configuration file using a text editor such as nano.
Add the following configurations above the </tomcat-users> directive. Replace manager_password and admin_password with your desired administrative user passwords.
Save and close the file.
The above configuration creates a new manager and admin user with manager, and administrator privileges respectively to access the Tomcat web management dashboard.
Open the manager context.xml file to remove restrictions to the manager application.
Find and disable the following Valve directive using a <!-- --> comment.
Save and close the file.
Open the host manager context.xml file to remove restrictions to the host manager application.
Find and disable the following Valve directive.
Save and close the file.
Follow the steps below to create a new system service to run and manage the Apache Tomcat application processes on your server.
Run the following command to view and note the Java installation path in your output.
Output:
Create a new tomcat.service system service file.
Add the following configurations to the file. Modify the JAVA_HOME value to include your actual Java installation path if different.
Save and close the file.
The above system service configuration creates a new tomcat service that runs the startup.sh and shutdown.sh scripts in the Apache Tomcat project directory to manage the application's processes.
Reload the systemd daemon to apply the new service configuration.
Enable the Apache Tomcat service to start at boot.
Start the Apache Tomcat service.
View the Apache Tomcat service status and verify that it's running.
Output:
Apache Tomcat listens for incoming connections using the insecure HTTP port 8080 and the HTTPS port 8443 for secure connections. Follow the steps below to generate trusted Let's Encrypt SSL certificates using the tomcat.example.com domain to enable secure HTTPS connections to the Apache Tomcat service.
View the UFW status and verify that the firewall is active.
If the status is inactive, allow the SSH port 22 and enable UFW using the command below.
Allow HTTP connections through the firewall.
Reload UFW to apply the firewall configuration changes.
Install the Snapd package.
Install the Certbot Let's Encrypt client using Snap.
Generate a new SSL certificate to use with Apache Tomcat. Replace tomcat.example.com and admin@example.com with your actual details.
Your output should be similar to the one below when successful.
Copy your domain's Let's Encrypt SSL certificate files to the /opt/tomcat/conf/ Tomcat configurations directory. Replace tomcat.example.com with your actual domain.
Grant the Apache Tomcat user and group full privileges to all .pem certificate files in the /opt/tomcat/conf/ directory.
Allow network connections to the Apache Tomcat HTTP port 8080 and HTTPS port 8443 through the firewall.
Reload UFW to apply the firewall configuration changes.
View the UFW status and verify all available firewall rules.
Output:
Open the server.xml file to enable the SSL files in the Apache Tomcat configuration.
Add the following configuration before the <Connector section to load your SSL certificate files.
Save and close the file.
The above configuration enables Apache Tomcat to accept HTTPS network connections on port 8443 using the .pem certificate files in the /opt/tomcat/conf directory.
Restart the Apache Tomcat service to apply the configuration changes.
Follow the steps below to access the Apache Tomcat web management dashboard to view and manage web applications on your server.
Visit your Apache Tomcat domain on port 8443 using a web browser such as Chrome.
Verify that the default Apache Tomcat page displays in your web browser.
Click Manager App to access the Apache Tomcat manager application, and enter your manager username and password when prompted to Sign In.
Verify that the Apache Tomcat manager application loads correctly.
Click Host Manager from the list of applications to access the host manager interface. Enter your administrator user credentials when prompted to log in.
Follow the steps below to create a sample Java web application with the following structure and run it using Apache Tomcat on your server.
Create a new example-app project directory.
Switch to the example-app directory.
Create a WEB-INF subdirectory in the example-app project directory.
Create a classes subdirectory inside the WEB-INF directory.
Create a new GreetingsServlet.java Java web application file.
Add the following contents to the GreetingsServlet.java file.
Save and close the file.
The above configuration creates a new GreetingsServlet that extends the HttpServlet and displays Hello, World! Greetings from Vultr message when it runs.
Create a new web.xml file in the WEB-INF directory.
Add the following contents to the web.xml file.
Save and close the file.
The above XML configuration maps the GreetingsServlet to the /helloworld URL pattern to enable Apache Tomcat to run the example-app Java web application using the /helloworld path.
Compile the GreetingsServlet.java application file using the servlet-api.jar Apache Tomcat library to create a GreetingsServlet.class file.
Move the GreetingsServlet.class file to the WEB-INF/classes directory.
List files in the classes subdirectory and verify that a new GreetingsServerlet.class file is available.
Output:
Switch to your parent directory.
Move the example-app project to the /opt/tomcat/webapps directory to enable the new web application.
Restart Apache Tomcat to apply the configuration changes.
Open the Apache Tomcat web management interface and click Manager App to view all available web applications.
Verify that the example-app web application is available on the list of applications.
Modify your Apache Tomcat URL and include the /example-app/helloworld path to verify that the example-app web application runs correctly.
You have installed Apache Tomcat on Ubuntu 24.04 and secured the server with trusted Let's Encrypt SSL certificates to run Java-based web applications. You can use Apache Tomcat to deploy multiple web applications and run specific services to match your development needs. For more information and configuration options, visit the Tomcat documentation.
0 Comments
Be the first to comment and share your perspective with the community.