How to Deploy Applications Using Tomcat on a Web Server

Deploying applications using Apache Tomcat is a staple in the Java development world. Tomcat, an open-source implementation of Java Servlet, JavaServer Pages, and Java Expression Language technologies, provides a robust platform for running your Java applications. In this blog, we'll walk you through the process of deploying a web application using Tomcat, covering installation, configuration, deployment, and some troubleshooting tips.

Table of Contents

  1. Introduction to Tomcat
  2. Prerequisites
  3. Installing Tomcat
  4. Configuring Tomcat
  5. Deploying a Web Application
  6. Accessing the Deployed Application
  7. Managing Applications with Tomcat Manager
  8. Automating Deployment
  9. Troubleshooting Tips
  10. Conclusion

1. Introduction to Tomcat

Apache Tomcat is a widely-used web server and servlet container that provides a "pure Java" HTTP web server environment for Java code to run in. Tomcat is typically used to run Java Servlets and JavaServer Pages (JSP), which are often utilized to create dynamic web content.

2. Prerequisites

Before you begin, ensure you have the following:

3. Installing Tomcat

Step 1: Download Tomcat

First, download the latest version of Tomcat from the official Apache Tomcat website. Choose the version that suits your needs, typically the latest stable release.

Step 2: Extract the Archive

After downloading, extract the Tomcat archive to a directory of your choice. For example:

tar -xvf apache-tomcat-9.0.58.tar.gz mv apache-tomcat-9.0.58 /usr/local/tomcat9 
Enter fullscreen mode

Exit fullscreen mode

Step 3: Set Environment Variables

Set the CATALINA_HOME environment variable to the Tomcat installation directory. Add the following lines to your .bashrc or .bash_profile :

export CATALINA_HOME=/usr/local/tomcat9 export PATH=$CATALINA_HOME/bin:$PATH 
Enter fullscreen mode

Exit fullscreen mode

Apply the changes:

source ~/.bashrc 
Enter fullscreen mode

Exit fullscreen mode

4. Configuring Tomcat

Step 1: Configuration Files

Tomcat configuration files are located in the conf directory under your Tomcat installation directory. The primary configuration files include:

Step 2: Modify server.xml

The server.xml file configures the core server settings. A typical setup might look like this:

 port="8005" shutdown="SHUTDOWN">  name="Catalina">  port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />  name="Catalina" defaultHost="localhost">  name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">  path="" docBase="/path/to/your/app" />     
Enter fullscreen mode

Exit fullscreen mode

5. Deploying a Web Application

Step 1: Preparing the WAR File

Ensure your web application is packaged as a WAR (Web Application Archive) file. The WAR file should contain all the necessary Java classes, libraries, and resources for your application.

Step 2: Deploy the WAR File

There are several ways to deploy a WAR file to Tomcat:

    Manual Deployment: Copy the WAR file to the webapps directory.

cp /path/to/yourapp.war $CATALINA_HOME/webapps/ 

6. Accessing the Deployed Application

Once deployed, you can access your application through a web browser. If Tomcat is running on localhost and using the default port 8080 , and your application is named yourapp , you would access it at:

http://localhost:8080/yourapp 
Enter fullscreen mode

Exit fullscreen mode

7. Managing Applications with Tomcat Manager

Tomcat provides a web-based manager that allows you to manage your web applications. To access the Tomcat Manager:

    Open a web browser and navigate to:

http://localhost:8080/manager/html 
 rolename="manager-gui"/>  username="admin" password="admin" roles="manager-gui"/> 
$CATALINA_HOME/bin/shutdown.sh $CATALINA_HOME/bin/startup.sh 

8. Automating Deployment

For continuous integration and deployment (CI/CD), you can automate the deployment process using tools like Jenkins, GitHub Actions, or GitLab CI. A typical Jenkins pipeline might look like this:

pipeline  agent any stages  stage('Build')  steps  sh 'mvn clean package' > > stage('Deploy')  steps  sh ''' cp target/yourapp.war $CATALINA_HOME/webapps/ $CATALINA_HOME/bin/shutdown.sh $CATALINA_HOME/bin/startup.sh ''' > > > > 
Enter fullscreen mode

Exit fullscreen mode

9. Troubleshooting Tips

export JAVA_OPTS="-Xms512m -Xmx1024m" 

10. Conclusion

Deploying a web application using Tomcat involves several steps, from installation and configuration to deployment and management. By following this guide, you should be able to set up a Tomcat server, deploy your applications, and manage them effectively. Remember to refer to the official Apache Tomcat documentation for more detailed information and advanced configurations. Happy deploying!

Feel free to comment below if you have any questions or run into any issues. Happy coding!