This article explores Apache Tomcat, detailing its installation, directory structure, and web application deployment on a CentOS machine.
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.
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.
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:
Copy
Ask AI
<!-- A "Connector" represents an endpoint for receiving requests and returning responses. For details on connectors, see the [Java HTTP Connector documentation](/docs/connectors/) and the [Java AJP Connector documentation](/docs/apr/).--><Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /><!-- A "Connector" using the shared executor --><Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
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).
To confirm deployment, review the Tomcat logs located in the logs directory. Check the contents of catalina.out with:
Copy
Ask AI
cat ~/apache-tomcat-8.5.53/logs/catalina.out
Example log output:
Copy
Ask AI
18-Mar-2020 10:43:32.769 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/home/vagrant/apache-tomcat-8.5.53/webapps/ROOT] has finished in [13] ms18-Mar-2020 10:43:32.783 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/home/vagrant/apache-tomcat-8.5.53/webapps/manager] has finished in [14] ms18-Mar-2020 10:43:32.789 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]18-Mar-2020 10:43:32.789 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 407 ms18-Mar-2020 11:21:23.560 INFO [localhost-startStop-2] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/home/vagrant/apache-tomcat-8.5.53/webapps/app.war] has finished in [57] ms
Once deployed, access your application by navigating to:http://your-hostname:8080/appFor example, if your application displays a “Hello, World” message, its homepage will appear similar to the following:
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.