DEV Community

Cover image for How to Deploy Applications Using Tomcat on a Web Server
Aaditya Kediyal
Aaditya Kediyal

Posted on

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:

  • JDK (Java Development Kit) installed on your machine.
  • Apache Tomcat downloaded.
  • A web application (a WAR file) ready to deploy.

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:

  • server.xml: Main configuration file for Tomcat.
  • web.xml: Default web application deployment descriptor.
  • context.xml: Default Context elements.

Step 2: Modify server.xml

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

<Server port="8005" shutdown="SHUTDOWN">
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Host name="localhost" appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/path/to/your/app" />
      </Host>
    </Engine>
  </Service>
</Server>
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:

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

    cp /path/to/yourapp.war $CATALINA_HOME/webapps/
    
  2. Using Tomcat Manager: Tomcat includes a web-based application manager that allows you to upload and deploy WAR files through a web interface.

  3. Automated Deployment: Configure Tomcat to automatically deploy applications by placing them in the webapps directory.

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:

  1. Open a web browser and navigate to:

    http://localhost:8080/manager/html
    
  2. Log in with the Tomcat manager credentials. By default, the manager role is not assigned to any user. You need to add a user with the manager-gui role in the tomcat-users.xml file located in the conf directory:

    <role rolename="manager-gui"/>
    <user username="admin" password="admin" roles="manager-gui"/>
    
  3. Restart Tomcat for the changes to take effect:

    $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

  • Logs: Check the Tomcat logs located in the logs directory. The catalina.out file is especially useful for debugging startup issues.

  • Permissions: Ensure that the user running Tomcat has the necessary permissions to read/write the web application files.

  • Port Conflicts: Ensure that the ports configured in server.xml are not being used by other applications.

  • Memory Issues: Adjust the JVM memory settings in the setenv.sh (or setenv.bat on Windows) file:

    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!

Top comments (0)