⚙️Demystifying Apache Tomcat: A Developer’s Path to Manual Deployment and DevOps Readiness 💻
Apache Tomcat is one of the most popular open-source web servers used for deploying Java-based web applications. It is widely adopted in the developer community for its reliability, scalability, and cross-platform capabilities. In this guide, we’ll walk you through the basics of the Apache Tomcat server, explore its folder structure, and provide step-by-step instructions for setting up and working with it, particularly within a Linux environment.
What is Apache Tomcat?
Apache Tomcat is an open-source and free web server developed by the Apache Software Foundation. It is specifically designed to serve Java web applications and offers a robust environment for running Java Servlets and JavaServer Pages (JSP). By default, the Tomcat server runs on port 8080, though this port can be configured as needed.
Folder Structure of Apache Tomcat
When you install Apache Tomcat, its core directories are organized into several important folders. Here’s a brief overview:
bin
: Contains scripts to start and stop the Tomcat server.config
: Stores configuration files required to define server settings.lib
: Includes external libraries (JAR files) used by the server.logs
: Holds server log files. These logs can help in troubleshooting issues.temp
: Stores temporary files created during server execution. These files can be removed, if necessary.webapps
: The deployment directory where your Java web application files are stored.
Setting Up Apache Tomcat on a Linux Environment
Follow these steps to install Apache Tomcat on a Linux-based EC2 instance:
Login to AWS and Create an EC2 Instance
- Access the AWS Management Console and set up an EC2 instance using the Amazon Linux AMI.
Connect to the EC2 Instance
- Use tools like MobaXterm or Putty to log into your EC2 instance.
Install Java
Apache Tomcat requires Java to run. Install Java using the following command:
bash
$ sudo yum install java-1.8.0-openjdk
Verify the Java version installed:
$ java -version
If multiple Java versions exist on your machine, switch to a specific version using:
$ alternatives --config java
4. Download Apache Tomcat
Visit the official Apache Tomcat website to locate the latest version.
Copy the URL of the tar.gz file and download it with the following command:
$ wget <tomcat-tar-file-url>
Example URL:
"https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.34/bin/apache-tomcat-10.1.34.tar.gz"
Extract the Tar File
Once the file is downloaded, extract it using the command:
$ tar -xvf <tomcat-tar-file-name>
Explore the Folder Structure
After extraction, navigate to the Tomcat folder:
$ cd <tomcat-folder-name> $ ls -ltr
6. Start Apache Tomcat Server
Navigate to the
bin
directory and execute the startup script:bash
$ cd bin $ ./startup.sh
By default, Tomcat listens on port 8080, so ensure this port is enabled in the security group of your EC2 instance.
Enable the port in your Security Group:
Type: Custom TCP
Protocol: TCP
Port Range: 8080
Source: 0.0.0.0/0
- Access Apache Tomcat
Open your browser and navigate to:http://<Public-IP-of-EC2-Instance>:8080
You should see the Tomcat home page.
Modifying Default Access Settings
The Host Manager is restricted to local access by default. To allow access from other systems:
Edit the
context.xml
file located at:
text<tomcat-folder>/webapps/manager/META-INF/context.xml
Change the
<Valve>
tag configuration to:<Context antiResourceLocking="false" priviledged="true"> <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow=".*"/> </Context>
Adding Users to Tomcat Configuration
To grant access to administrative features, modify the file:
tomcat/conf/tomcat-users.xml
Here is an example configuration:<role rolename="manager-gui" /> <user username="tomcat" password="tomcat" roles="manager-gui" /> <role rolename="admin-gui" /> <user username="admin" password="admin" roles="manager-gui, admin-gui" />
Deploying Maven Web Applications on Apache Tomcat
To deploy a Maven web application, follow these steps:
Create a Maven Web Application
Develop your Java-based web application using Maven. Modify theindex.jsp
file as desired.Package the Application
Run the following Maven command to package the application into a WAR file:bash
$ mvn clean package
Deploy the WAR File
Access the Manager App on the Tomcat Admin Dashboard.
Upload the WAR file and click on the Deploy button.
Run the Application
Once deployed, your application will be listed in the deployed applications. Click on its path to open it in the browser.Changing the Default Port of Apache Tomcat
To change the default port (8080) on which Tomcat runs, edit the following file:
<tomcat-folder>/conf/server.xml
Locate the below section in the configuration file:
xml
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
Modify the port value to your desired port, then save the changes. Don’t forget to enable the new port in the Security Group of your EC2 instance.
Conclusion
Once you have successfully set up Apache Tomcat, don’t forget to stop the server and the EC2 instance when not in use to avoid incurring unnecessary charges:
bash
$ ./shutdown.sh
Stop the EC2 instance via the AWS Management Console.
Apache Tomcat remains a preferred server technology for developers building and deploying Java-based applications. Its flexibility, ease of use, and open-source nature make it an essential tool for modern software development.