Skip to main content
In this article, we will explore Apache Tomcat, a popular web server and servlet container primarily used to host Java-based web applications. Before beginning, ensure that Java is installed on your system.
Ensure that Java is installed on your system before proceeding with the Tomcat installation.

Installing Apache Tomcat on CentOS

Follow these steps to install Apache Tomcat on a CentOS machine. In a production environment, it’s recommended to create dedicated users for Tomcat and configure it as a service.
  1. Install Java:
  2. Download the Tomcat package:
  3. Extract the package:
  4. Start Tomcat using the startup script in the bin directory:
After the server starts, open your web browser and navigate to port 8080 on your host. You should see the default Apache Tomcat welcome page:
The image shows a browser displaying the Apache Tomcat 8.5.53 welcome page, indicating successful installation on localhost:8080.

Exploring the Tomcat Directory Structure

After extracting the Tomcat package, examine its directory structure with the following command:
Example output:

Key Directories

  • bin: Contains scripts for starting and stopping Tomcat. Windows users should use the .bat files, while Unix/Linux users should use the .sh scripts.
  • conf: Contains essential configuration files such as server.xml and web.xml. The server.xml file defines the “Connector” element that sets the port (default is 8080) on which Tomcat listens. Any changes require a restart of the service.
  • logs: Stores server logs.
  • webapps: The default deployment directory for web applications. Placing a WAR file here triggers automatic extraction and deployment.
  • lib, temp, and work: Manage libraries, temporary files, and work data respectively.
Below is an excerpt from the server.xml file demonstrating how connectors are configured:

Deploying a Web Application

Deploying a web application on Tomcat is straightforward. Follow these steps:

1. Package Your Application

First, package your application’s source code into a WAR file. You can use the jar utility or build tools such as Maven or Gradle:

2. Deploy the WAR File

Copy the WAR file (e.g., app.war) to the webapps directory of your Tomcat server. If Tomcat is running, it will automatically detect the new file, extract it, and deploy the application into a folder corresponding to the WAR file name (for example, /app).

3. Verify the Deployment

To confirm deployment, review the Tomcat logs located in the logs directory. Check the contents of catalina.out with:
Example log output:
Once deployed, access your application by navigating to: http://your-hostname:8080/app For example, if your application displays a “Hello, World” message, its homepage will appear similar to the following:
A browser displays a "Sample 'Hello, World' Application" webpage with links to a JSP page and a servlet, featuring a cat logo.

Conclusion

This guide provided an overview of installing Apache Tomcat, exploring its directory structure, and deploying a web application. Make sure to implement additional security measures, performance tuning, and service management configurations when deploying in production environments.
In production, always review and apply best practices for security and performance optimization to ensure a robust deployment.
Happy deploying, and see you in the next lesson!

Watch Video

Practice Lab